java GUI 速记

GUI概述

在这里插入图片描述

GUI函数

链接

示例

Example6_13.java

public class Example6_13 {
	public static void main(String[] args) {
		new TextJFrame();
	}
}

TextJFrame

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TextJFrame extends JFrame{
	private JFrame jf;
	private JTextArea text;
	private JComboBox jcb;
	private JCheckBox check_bold, check_italic;
	private JRadioButton radio_red, radio_green, radio_blue;
	private JPopupMenu popmenu;
	
	public TextJFrame() {
		jf = new JFrame("文件编辑器");
		jf.setBounds(100, 100, 500, 200);
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		jf.setVisible(true);
		JMenuBar menubar = new JMenuBar();
		jf.setJMenuBar(menubar);
		String str[] = {"文件", "编辑", "帮助"};
		JMenu menu[] = new JMenu[str.length];
		for(int i = 0; i < str.length; i ++) {
			menu[i] = new JMenu(str[i]);
			menubar.add(menu[i]);
		}
		text = new JTextArea("示例文本");
		jf.getContentPane().add(text);
		JToolBar toolbar = new JToolBar();
		toolbar.setLayout(new FlowLayout());
		jf.getContentPane().add(toolbar, "North");
		jcb = new JComboBox();
		jcb.setEditable(true);
		toolbar.add(jcb);
		String[] str_size = {"20", "30", "40", "50", "60"};
		String str_file[] = {"打开", "保存", "退出"};
		JMenuItem menuitem[] = new JMenuItem[str_file.length];
		for(int j = 0; j < str_file.length; j ++) {
			menuitem[j] = new JMenuItem(str_file[j]);
			menu[0].add(menuitem[j]);
			menu[0].addSeparator();
			menuitem[j].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(e.getActionCommand() == "退出") {
						if(JOptionPane.showConfirmDialog(jf, "你确定退出么?") == 0)
							System.exit(0);
					}
				}
			});
		}
		check_bold = new JCheckBox("粗体");
		check_italic = new JCheckBox("斜体");
		toolbar.add(check_bold);
		toolbar.add(check_italic);
		check_bold.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getActionCommand() == "粗体") {
					Font f = text.getFont();
					text.setFont(new Font(f.getFontName(), Font.BOLD, f.getSize()));
				}
			}
		});
		check_italic.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() instanceof JCheckBox) {
					if(e.getActionCommand() == "斜体") {
						Font f = text.getFont();
						text.setFont(new Font(f.getFontName(), Font.ITALIC, f.getSize()));
					}
				}
			}
		});
		radio_red = new JRadioButton("红色");
		radio_green = new JRadioButton("绿色");
		radio_blue = new JRadioButton("蓝色");
		ButtonGroup bg = new ButtonGroup();
		bg.add(radio_red);
		bg.add(radio_green);
		bg.add(radio_blue);
		toolbar.add(radio_red);
		toolbar.add(radio_green);
		toolbar.add(radio_blue);
		radio_red.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() instanceof JRadioButton) {
					if(e.getActionCommand() == "红色") {
						text.setForeground(Color.red);
					}
				}
			}
		});
		radio_green.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() instanceof JRadioButton) {
					if(e.getActionCommand() == "绿色") {
						text.setForeground(Color.green);
					}
				}
			}
		});
		radio_blue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() instanceof JRadioButton) {
					if(e.getActionCommand() == "蓝色") {
						text.setForeground(Color.blue);
					}
				}
			}
		});
		popmenu = new JPopupMenu();
		String popstr[] = {"剪切", "复制", "粘贴"};
		JMenuItem popmenuitem[] = new JMenuItem[popstr.length];
		for(int i = 0; i < popstr.length; i ++) {
			popmenuitem[i] = new JMenuItem(popstr[i]);
			popmenu.add(popmenuitem[i]);
			popmenuitem[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(e.getActionCommand() == "复制") {
						text.copy();
					}
					if(e.getActionCommand() == "粘贴") {
						text.paste();
					}
					if(e.getActionCommand() == "剪切") {
						text.cut();
					}
				}
			});
		}
		text.add(popmenu);
		text.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				if(e.getModifiers() == MouseEvent.BUTTON3_MASK)
					popmenu.show(text, e.getX(), e.getY());
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
		for(int i = 0; i < str_size.length; i ++) {
			jcb.addItem(str_size[i]);
			jcb.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(e.getSource() instanceof JComboBox) {
						Font f = text.getFont();
						try {
							int i = Integer.parseInt((String)jcb.getSelectedItem());
							text.setFont(new Font(f.getFontName(), f.getStyle(), i));
						}catch(Exception ex) {
							JOptionPane.showMessageDialog(jf, "字号大小不合适,请重新输入");
							jcb.setSelectedItem(String.valueOf(f.getSize()));
						}
						String size = (String)jcb.getSelectedItem();
						int i = 0, n = jcb.getItemCount();
						while(i < n
							&& size.compareTo((String)jcb.getItemAt(i)) >= 0) {
							if(size.compareTo((String)jcb.getItemAt(i)) == 0)
								return;
							i ++;
						}
						jcb.insertItemAt(size, i);
					}
				}
			});
		}
		
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值