GUI说起来多,其实用起来很方便。只是要怎么布局美观的问题,现在一般都用IDE插件拖动来完成这个工作,但学习下原理还是很有必要的,毕竟现在的插件动不动就卡住,有时所见非所得的情况很多。AWT提供的基本组件有以下几个,
Button
Canvas
Checkbox
CheckboxGroup
Choice
Frame
Label
List
Panel
Scrollbar
ScrollPane(带滚动条的Panel)
TextArea(多行文本域)
TextField(单行文本域)
这些没什么好说的,我举个例子就能说明一切,eg
- public class CommonComponent
- {
- Frame f = new Frame("测试");
- //定义一个按钮
- Button ok = new Button("确认");
- CheckboxGroup cbg = new CheckboxGroup();
- //定义一个单选框(处于cbg一组),初始处于被选中状态
- Checkbox male = new Checkbox("男" , cbg , true);
- //定义一个单选框(处于cbg一组),初始处于没有选中状态
- Checkbox female = new Checkbox("女" , cbg , false);
- //定义一个复选框,初始处于没有选中状态
- Checkbox married = new Checkbox("是否已婚?" , false);
- //定义一个下拉选择框
- Choice colorChooser = new Choice();
- //定义一个列表选择框
- List colorList = new List(6, true);
- //定义一个5行、20列的多行文本域
- TextArea ta = new TextArea(5, 20);
- //定义一个50列的单行文本域
- TextField name = new TextField(50);
- public void init()
- {
- colorChooser.add("红色");
- colorChooser.add("绿色");
- colorChooser.add("蓝色");
- colorList.add("红色");
- colorList.add("绿色");
- colorList.add("蓝色");
- //创建一个装载了文本框、按钮的Panel
- Panel bottom = new Panel();
- bottom.add(name);
- bottom.add(ok);
- f.add(bottom , BorderLayout.SOUTH);
- //创建一个装载了下拉选择框、三个Checkbox的Panel
- Panel checkPanel = new Panel();
- checkPanel.add(colorChooser);
- checkPanel.add(male);
- checkPanel.add(female);
- checkPanel.add(married);
- //创建一个垂直排列组件的Box,盛装多行文本域、Panel
- Box topLeft = Box.createVerticalBox();
- topLeft.add(ta);
- topLeft.add(checkPanel);
- //创建一个垂直排列组件的Box,盛装topLeft、colorList
- Box top = Box.createHorizontalBox();
- top.add(topLeft);
- top.add(colorList);
- //将top Box容器添加到窗口的中间
- f.add(top);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new CommonComponent().init();
- }
- }
然后讲下对话框,分为两种:模态对话框(打开时主对话框锁定)和非模态对话框(打开时主对话框可以激活)eg
- public class TestDialog
- {
- Frame f = new Frame("测试");
- Dialog d1 = new Dialog(f, "模式对话框" , true);
- Dialog d2 = new Dialog(f, "非模式对话框" , false);
- Button b1 = new Button("打开模式对话框");
- Button b2 = new Button("打开非模式对话框");
- public void init()
- {
- d1.setBounds(20 , 30 , 300, 400);
- d2.setBounds(20 , 30 , 300, 400);
- b1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- d1.setVisible(true);
- }
- });
- b2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- d2.setVisible(true);
- }
- });
- f.add(b1);
- f.add(b2 , BorderLayout.SOUTH);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new TestDialog().init();
- }
- }
文件对话框,eg
- public class TestFileDialog
- {
- Frame f = new Frame("测试");
- FileDialog d1 = new FileDialog(f, "选择需要打开文件" , FileDialog.LOAD);
- FileDialog d2 = new FileDialog(f, "选择保存文件的路径" , FileDialog.SAVE);
- Button b1 = new Button("打开文件");
- Button b2 = new Button("保存文件");
- public void init()
- {
- b1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- d1.setVisible(true);
- System.out.println(d1.getDirectory() + d1.getFile());
- }
- });
- b2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- d2.setVisible(true);
- System.out.println(d2.getDirectory() + d2.getFile());
- }
- });
- f.add(b1);
- f.add(b2 , BorderLayout.SOUTH);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new TestFileDialog().init();
- }
- }
AWT菜单由以下几个部分组成:
MenuBar
Menu
PopupMenu
MenuItem
CheckboxMenuItem
MenuShortcut
这些都可以顾名思义,当要记住一个菜单的分隔线的使用 new MenuItem("-"),这里举个例子让大家来了解下,eg
- public class SimpleMenu
- {
- private Frame f = new Frame("测试");
- private MenuBar mb = new MenuBar();
- Menu file = new Menu("文件");
- Menu edit = new Menu("编辑");
- MenuItem newItem = new MenuItem("新建");
- MenuItem saveItem = new MenuItem("保存");
- //创建exitItem菜单项,指定使用 Ctrl+X 快捷键
- MenuItem exitItem = new MenuItem("退出" , new MenuShortcut(KeyEvent.VK_X));
- CheckboxMenuItem autoWrap = new CheckboxMenuItem("自动换行");
- MenuItem copyItem = new MenuItem("复制");
- MenuItem pasteItem = new MenuItem("粘贴");
- Menu format = new Menu("格式");
- //创建commentItem菜单项,指定使用 Ctrl+Shift+/ 快捷键
- MenuItem commentItem = new MenuItem("注释" ,
- new MenuShortcut(KeyEvent.VK_SLASH , true));
- MenuItem cancelItem = new MenuItem("取消注释");
- private TextArea ta = new TextArea(6 , 40);
- public void init()
- {
- //以匿名内部类的形式创建菜单监听器
- ActionListener menuListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- String cmd = e.getActionCommand();
- ta.append("单击“" + cmd + "”菜单" + "/n");
- if (cmd.equals("退出"))
- {
- System.exit(0);
- }
- }
- };
- //为commentItem、exitItem两个菜单项添加了事件监听器。
- commentItem.addActionListener(menuListener);
- exitItem.addActionListener(menuListener);
- //为file菜单添加菜单项
- file.add(newItem);
- file.add(saveItem);
- file.add(exitItem);
- //为edit菜单添加菜单项
- edit.add(autoWrap);
- //使用addSeparator方法来添加菜单分隔线
- edit.addSeparator();
- edit.add(copyItem);
- edit.add(pasteItem);
- //为format菜单添加菜单项
- format.add(commentItem);
- format.add(cancelItem);
- //使用添加new MenuItem("-")的方式添加菜单分隔线
- edit.add(new MenuItem("-"));
- //将format菜单组合到edit菜单中,从而形成二级菜单
- edit.add(format);
- //将file、edit菜单添加到mb菜单条中
- mb.add(file);
- mb.add(edit);
- //为f窗口设置菜单条
- f.setMenuBar(mb);
- //以匿名内部类的形式来创建事件监听器对象
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- ta.append("用户试图关闭窗口!/n");
- System.exit(0);
- }
- });
- f.add(ta);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new SimpleMenu().init();
- }
- }
右键菜单
- public class TestPopupMenu
- {
- private TextArea ta = new TextArea(4 , 30);
- private Frame f = new Frame("测试");
- PopupMenu pop = new PopupMenu();
- CheckboxMenuItem autoWrap = new CheckboxMenuItem("自动换行");
- MenuItem copyItem = new MenuItem("复制");
- MenuItem pasteItem = new MenuItem("粘贴");
- Menu format = new Menu("格式");
- //创建commentItem菜单项,指定使用 Ctrl+Shift+/ 快捷键
- MenuItem commentItem = new MenuItem("注释" ,
- new MenuShortcut(KeyEvent.VK_SLASH , true));
- MenuItem cancelItem = new MenuItem("取消注释");
- public void init()
- {
- //以匿名内部类的形式创建菜单监听器
- ActionListener menuListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- String cmd = e.getActionCommand();
- ta.append("单击“" + cmd + "”菜单" + "/n");
- if (cmd.equals("退出"))
- {
- System.exit(0);
- }
- }
- };
- //为commentItem、exitItem两个菜单项添加了事件监听器。
- commentItem.addActionListener(menuListener);
- //为pop菜单添加菜单项
- pop.add(autoWrap);
- //使用addSeparator方法来添加菜单分隔线
- pop.addSeparator();
- pop.add(copyItem);
- pop.add(pasteItem);
- //为format菜单添加菜单项
- format.add(commentItem);
- format.add(cancelItem);
- //使用添加new MenuItem("-")的方式添加菜单分隔线
- pop.add(new MenuItem("-"));
- //将format菜单组合到pop菜单中,从而形成二级菜单
- pop.add(format);
- final Panel p = new Panel();
- p.setPreferredSize(new Dimension(300, 160));
- //向p窗口中添加PopupMenu对象
- p.add(pop);
- p.addMouseListener(new MouseAdapter()
- {
- public void mouseReleased(MouseEvent e)
- {
- if (e.isPopupTrigger())
- {
- pop.show(p , e.getX() , e.getY());
- }
- }
- });
- f.add(p);
- f.add(ta , BorderLayout.NORTH);
- //以匿名内部类的形式来创建事件监听器对象
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- ta.append("用户试图关闭窗口!/n");
- System.exit(0);
- }
- });
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new TestPopupMenu().init();
- }
- }
但是遗憾的是AWT不支持带有图标的菜单,如要使用请用Swing来解决。
Java语言将每一个键盘或鼠标的操作定义为一个“事件”。在编程中只需定义每个特定事件发生时程序应该做出何种响应即可。这就是图形用户界面中的“事件”和“事件响应”。事件处理的原理其实很简单,当程序启动时awt起个线程监听键盘,鼠标等的事件,当发生时产生个事件源,事件监听器负责监听事件,然后得到Event对象,然后根据Event对象来自己处理不同的事件。
下面看个最简单的事件处理,eg
- public class EventQs
- {
- private Frame f = new Frame("测试事件");
- private Button ok = new Button("确定");
- private TextField tf = new TextField(30);
- public void init()
- {
- //注册事件监听器
- ok.addActionListener(new OkListener());
- f.add(tf);
- f.add(ok , BorderLayout.SOUTH);
- f.pack();
- f.setVisible(true);
- }
- //定义事件监听器类
- class OkListener implements ActionListener
- {
- //下面定义的方法就是事件处理器,用于响应特定的事件
- public void actionPerformed(ActionEvent e)
- {
- System.out.println("用户单击了ok按钮");
- tf.setText("Hello World");
- }
- }
- public static void main(String[] args)
- {
- new EventQs().init();
- }
- }
下面来看下事件的继承关系
↑
AWT Event
↑
Component Event | Action Event | Adjustment Event | Item Event
↑
Input Event | Focus Event | Paint Event | Window Event
↑
Mouse Event | Key Event
↑
MouseWheel Event
EventObject类有一个子类AWTEvent,它是所有AWT事件类的父类.
有些Swing组件将生成其他事件类型的事件对象;它们都真接扩展于EventObject,而不是AWTEvent.
事件对象封装了事件源与监听器彼此通信的事件信息.在必要的时候,可以对传给监听器对象的事件对象过行分析.
- public class TestWindowListener
- {
- private Frame f = new Frame("测试");
- private TextArea ta = new TextArea(6 , 40);
- public void init()
- {
- f.addWindowListener(new MyListener());
- f.add(ta);
- f.pack();
- f.setVisible(true);
- }
- class MyListener implements WindowListener
- {
- public void windowActivated(WindowEvent e)
- {
- ta.append("窗口被激活!/n");
- }
- public void windowClosed(WindowEvent e)
- {
- ta.append("窗口被成功关闭!/n");
- }
- public void windowClosing(WindowEvent e)
- {
- ta.append("用户试图关闭窗口!/n");
- System.exit(0);
- }
- public void windowDeactivated(WindowEvent e)
- {
- ta.append("窗口失去焦点!/n");
- }
- public void windowDeiconified(WindowEvent e)
- {
- ta.append("窗口被恢复!/n");
- }
- public void windowIconified(WindowEvent e)
- {
- ta.append("窗口被最小化!/n");
- }
- public void windowOpened(WindowEvent e)
- {
- ta.append("窗口初次被打开!/n");
- }
- }
- public static void main(String[] args)
- {
- new TestWindowListener().init();
- }
- }
- public class TestWindowAdapter
- {
- private Frame f = new Frame("测试");
- private TextArea ta = new TextArea(6 , 40);
- public void init()
- {
- f.addWindowListener(new MyListener());
- f.add(ta);
- f.pack();
- f.setVisible(true);
- }
- class MyListener extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- ta.append("用户试图关闭窗口!/n");
- System.exit(0);
- }
- }
- public static void main(String[] args)
- {
- new TestWindowAdapter().init();
- }
- }