疯狂JAVA讲义---第十一章(中):AWT编程-常用组件和事件处理

GUI说起来多,其实用起来很方便。只是要怎么布局美观的问题,现在一般都用IDE插件拖动来完成这个工作,但学习下原理还是很有必要的,毕竟现在的插件动不动就卡住,有时所见非所得的情况很多。AWT提供的基本组件有以下几个,

Button

Canvas

Checkbox

CheckboxGroup

Choice

Frame

Label

List

Panel

Scrollbar

ScrollPane(带滚动条的Panel)

TextArea(多行文本域)

TextField(单行文本域)

这些没什么好说的,我举个例子就能说明一切,eg

  1. public class CommonComponent
  2. {
  3.     Frame f = new Frame("测试");
  4.     //定义一个按钮
  5.     Button ok = new Button("确认");
  6.     CheckboxGroup cbg = new CheckboxGroup();
  7.     //定义一个单选框(处于cbg一组),初始处于被选中状态
  8.     Checkbox male = new Checkbox("男" , cbg , true);
  9.     //定义一个单选框(处于cbg一组),初始处于没有选中状态
  10.     Checkbox female = new Checkbox("女" , cbg , false);
  11.     //定义一个复选框,初始处于没有选中状态
  12.     Checkbox married = new Checkbox("是否已婚?" , false);
  13.     //定义一个下拉选择框
  14.     Choice colorChooser = new Choice();
  15.     //定义一个列表选择框
  16.     List colorList = new List(6true);
  17.     //定义一个5行、20列的多行文本域
  18.     TextArea ta = new TextArea(520);
  19.     //定义一个50列的单行文本域
  20.     TextField name = new TextField(50);
  21.     public void init()
  22.     {
  23.         colorChooser.add("红色");
  24.         colorChooser.add("绿色");
  25.         colorChooser.add("蓝色");
  26.         colorList.add("红色");
  27.         colorList.add("绿色");
  28.         colorList.add("蓝色");
  29.         //创建一个装载了文本框、按钮的Panel
  30.         Panel bottom = new Panel();
  31.         bottom.add(name);
  32.         bottom.add(ok);
  33.         f.add(bottom , BorderLayout.SOUTH);
  34.         //创建一个装载了下拉选择框、三个Checkbox的Panel
  35.         Panel checkPanel = new Panel();
  36.         checkPanel.add(colorChooser);
  37.         checkPanel.add(male);
  38.         checkPanel.add(female);
  39.         checkPanel.add(married);
  40.         //创建一个垂直排列组件的Box,盛装多行文本域、Panel
  41.         Box topLeft = Box.createVerticalBox();
  42.         topLeft.add(ta);
  43.         topLeft.add(checkPanel);
  44.         //创建一个垂直排列组件的Box,盛装topLeft、colorList
  45.         Box top = Box.createHorizontalBox();
  46.         top.add(topLeft);
  47.         top.add(colorList);
  48.         //将top Box容器添加到窗口的中间
  49.         f.add(top); 
  50.         f.pack();
  51.         f.setVisible(true);
  52.     }
  53.     public static void main(String[] args) 
  54.     {
  55.         new CommonComponent().init();
  56.     }
  57. }

然后讲下对话框,分为两种:模态对话框(打开时主对话框锁定)和非模态对话框(打开时主对话框可以激活)eg

  1. public class TestDialog
  2. {
  3.     Frame f = new Frame("测试");  
  4.     Dialog d1 = new Dialog(f, "模式对话框" , true);
  5.     Dialog d2 = new Dialog(f, "非模式对话框" , false);
  6.     Button b1 = new Button("打开模式对话框");
  7.     Button b2 = new Button("打开非模式对话框");
  8.     public void init()
  9.     {
  10.         d1.setBounds(20 , 30 , 300400);
  11.         d2.setBounds(20 , 30 , 300400);
  12.         b1.addActionListener(new ActionListener()
  13.         {
  14.             public void actionPerformed(ActionEvent e)
  15.             {
  16.                 d1.setVisible(true);
  17.             }
  18.         });
  19.         b2.addActionListener(new ActionListener()
  20.         {
  21.             public void actionPerformed(ActionEvent e)
  22.             {
  23.                 d2.setVisible(true);
  24.             }
  25.         });
  26.         f.add(b1);
  27.         f.add(b2 , BorderLayout.SOUTH);
  28.         f.pack();
  29.         f.setVisible(true);
  30.     }
  31.     public static void main(String[] args) 
  32.     {
  33.         new TestDialog().init();
  34.     }
  35. }

文件对话框,eg

  1. public class TestFileDialog
  2. {
  3.     Frame f = new Frame("测试");  
  4.     FileDialog d1 = new FileDialog(f, "选择需要打开文件" , FileDialog.LOAD);
  5.     FileDialog d2 = new FileDialog(f, "选择保存文件的路径" , FileDialog.SAVE);
  6.     Button b1 = new Button("打开文件");
  7.     Button b2 = new Button("保存文件");
  8.     public void init()
  9.     {
  10.         b1.addActionListener(new ActionListener()
  11.         {
  12.             public void actionPerformed(ActionEvent e)
  13.             {
  14.                 d1.setVisible(true);
  15.                 System.out.println(d1.getDirectory() + d1.getFile());
  16.             }
  17.         });
  18.         b2.addActionListener(new ActionListener()
  19.         {
  20.             public void actionPerformed(ActionEvent e)
  21.             {
  22.                 d2.setVisible(true);
  23.                 System.out.println(d2.getDirectory() + d2.getFile());
  24.             }
  25.         });
  26.         f.add(b1);
  27.         f.add(b2 , BorderLayout.SOUTH);
  28.         f.pack();
  29.         f.setVisible(true);
  30.     }
  31.     public static void main(String[] args) 
  32.     {
  33.         new TestFileDialog().init();
  34.     }
  35. }

AWT菜单由以下几个部分组成:

MenuBar

Menu

PopupMenu

MenuItem

CheckboxMenuItem

MenuShortcut

这些都可以顾名思义,当要记住一个菜单的分隔线的使用 new MenuItem("-"),这里举个例子让大家来了解下,eg

  1. public class SimpleMenu
  2. {
  3.     private Frame f = new Frame("测试");
  4.     private MenuBar mb = new MenuBar();
  5.     Menu file = new Menu("文件");
  6.     Menu edit = new Menu("编辑");
  7.     MenuItem newItem = new MenuItem("新建");
  8.     MenuItem saveItem = new MenuItem("保存");
  9.     //创建exitItem菜单项,指定使用 Ctrl+X 快捷键
  10.     MenuItem exitItem = new MenuItem("退出" , new MenuShortcut(KeyEvent.VK_X));   
  11.     CheckboxMenuItem autoWrap = new CheckboxMenuItem("自动换行");
  12.     MenuItem copyItem = new MenuItem("复制");
  13.     MenuItem pasteItem = new MenuItem("粘贴");
  14.     Menu format = new Menu("格式");
  15.     //创建commentItem菜单项,指定使用 Ctrl+Shift+/ 快捷键
  16.     MenuItem commentItem = new MenuItem("注释" , 
  17.         new MenuShortcut(KeyEvent.VK_SLASH , true));
  18.     MenuItem cancelItem = new MenuItem("取消注释");
  19.     private TextArea ta = new TextArea(6 , 40);
  20.     public void init()
  21.     {
  22.         //以匿名内部类的形式创建菜单监听器
  23.         ActionListener menuListener = new ActionListener()
  24.         {
  25.             public void actionPerformed(ActionEvent e)
  26.             {
  27.                 String cmd = e.getActionCommand();
  28.                 ta.append("单击“" + cmd + "”菜单" + "/n");
  29.                 if (cmd.equals("退出"))
  30.                 {
  31.                     System.exit(0);
  32.                 }
  33.             }
  34.         };
  35.         //为commentItem、exitItem两个菜单项添加了事件监听器。
  36.         commentItem.addActionListener(menuListener);
  37.         exitItem.addActionListener(menuListener);
  38.         //为file菜单添加菜单项
  39.         file.add(newItem);
  40.         file.add(saveItem);
  41.         file.add(exitItem);
  42.         //为edit菜单添加菜单项
  43.         edit.add(autoWrap);
  44.         //使用addSeparator方法来添加菜单分隔线
  45.         edit.addSeparator();
  46.         edit.add(copyItem);
  47.         edit.add(pasteItem);
  48.         //为format菜单添加菜单项
  49.         format.add(commentItem);
  50.         format.add(cancelItem);
  51.         //使用添加new MenuItem("-")的方式添加菜单分隔线
  52.         edit.add(new MenuItem("-"));
  53.         //将format菜单组合到edit菜单中,从而形成二级菜单
  54.         edit.add(format);
  55.         //将file、edit菜单添加到mb菜单条中
  56.         mb.add(file);
  57.         mb.add(edit);
  58.         //为f窗口设置菜单条
  59.         f.setMenuBar(mb);
  60.         //以匿名内部类的形式来创建事件监听器对象
  61.         f.addWindowListener(new WindowAdapter()
  62.         {
  63.             public void windowClosing(WindowEvent e) 
  64.             {
  65.                 ta.append("用户试图关闭窗口!/n");
  66.                 System.exit(0);
  67.             }
  68.         });
  69.         f.add(ta);
  70.         f.pack();
  71.         f.setVisible(true);
  72.     }
  73.     public static void main(String[] args) 
  74.     {
  75.         new SimpleMenu().init();
  76.     }
  77. }

右键菜单

  1. public class TestPopupMenu
  2. {
  3.     private TextArea ta = new TextArea(4 , 30);
  4.     private Frame f = new Frame("测试");
  5.     PopupMenu pop = new PopupMenu();
  6.     CheckboxMenuItem autoWrap = new CheckboxMenuItem("自动换行");
  7.     MenuItem copyItem = new MenuItem("复制");
  8.     MenuItem pasteItem = new MenuItem("粘贴");
  9.     Menu format = new Menu("格式");
  10.     //创建commentItem菜单项,指定使用 Ctrl+Shift+/ 快捷键
  11.     MenuItem commentItem = new MenuItem("注释" , 
  12.         new MenuShortcut(KeyEvent.VK_SLASH , true));
  13.     MenuItem cancelItem = new MenuItem("取消注释");
  14.     public void init()
  15.     {
  16.         //以匿名内部类的形式创建菜单监听器
  17.         ActionListener menuListener = new ActionListener()
  18.         {
  19.             public void actionPerformed(ActionEvent e)
  20.             {
  21.                 String cmd = e.getActionCommand();
  22.                 ta.append("单击“" + cmd + "”菜单" + "/n");
  23.                 if (cmd.equals("退出"))
  24.                 {
  25.                     System.exit(0);
  26.                 }
  27.             }
  28.         };
  29.         //为commentItem、exitItem两个菜单项添加了事件监听器。
  30.         commentItem.addActionListener(menuListener);
  31.         //为pop菜单添加菜单项
  32.         pop.add(autoWrap);
  33.         //使用addSeparator方法来添加菜单分隔线
  34.         pop.addSeparator();
  35.         pop.add(copyItem);
  36.         pop.add(pasteItem);
  37.         //为format菜单添加菜单项
  38.         format.add(commentItem);
  39.         format.add(cancelItem);
  40.         //使用添加new MenuItem("-")的方式添加菜单分隔线
  41.         pop.add(new MenuItem("-"));
  42.         //将format菜单组合到pop菜单中,从而形成二级菜单
  43.         pop.add(format);
  44.         final Panel p = new Panel();
  45.         p.setPreferredSize(new Dimension(300160));
  46.         //向p窗口中添加PopupMenu对象
  47.         p.add(pop);
  48.         p.addMouseListener(new MouseAdapter()
  49.         {
  50.             public void mouseReleased(MouseEvent e)
  51.             {
  52.                 if (e.isPopupTrigger())
  53.                 {
  54.                     pop.show(p , e.getX() , e.getY());
  55.                 }
  56.             }
  57.         });
  58.         f.add(p);
  59.         f.add(ta , BorderLayout.NORTH);
  60.         //以匿名内部类的形式来创建事件监听器对象
  61.         f.addWindowListener(new WindowAdapter()
  62.         {
  63.             public void windowClosing(WindowEvent e) 
  64.             {
  65.                 ta.append("用户试图关闭窗口!/n");
  66.                 System.exit(0);
  67.             }
  68.         });
  69.         f.pack();
  70.         f.setVisible(true);
  71.     }
  72.     public static void main(String[] args) 
  73.     {
  74.         new TestPopupMenu().init();
  75.     }
  76. }

但是遗憾的是AWT不支持带有图标的菜单,如要使用请用Swing来解决。

 

Java语言将每一个键盘或鼠标的操作定义为一个“事件”。在编程中只需定义每个特定事件发生时程序应该做出何种响应即可。这就是图形用户界面中的“事件”和“事件响应”。事件处理的原理其实很简单,当程序启动时awt起个线程监听键盘,鼠标等的事件,当发生时产生个事件源,事件监听器负责监听事件,然后得到Event对象,然后根据Event对象来自己处理不同的事件。

下面看个最简单的事件处理,eg

  1. public class EventQs
  2. {
  3.     private Frame f = new Frame("测试事件");
  4.     private Button ok = new Button("确定"); 
  5.     private TextField tf = new TextField(30);
  6.     public void init()
  7.     {
  8.         //注册事件监听器
  9.         ok.addActionListener(new OkListener());
  10.         f.add(tf);
  11.         f.add(ok , BorderLayout.SOUTH);
  12.         f.pack();
  13.         f.setVisible(true);
  14.     }
  15.     //定义事件监听器类
  16.     class OkListener implements ActionListener
  17.     {
  18.         //下面定义的方法就是事件处理器,用于响应特定的事件
  19.         public void actionPerformed(ActionEvent e)
  20.         {
  21.             System.out.println("用户单击了ok按钮");
  22.             tf.setText("Hello World");
  23.         }
  24.     }
  25.     public static void main(String[] args) 
  26.     {
  27.         new EventQs().init();
  28.     }
  29. }

下面来看下事件的继承关系

EventObject

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.
事件对象封装了事件源与监听器彼此通信的事件信息.在必要的时候,可以对传给监听器对象的事件对象过行分析.
 
事件监听器,这个太好用了,但要记住什么事件用什么监听器。eg
  1. public class TestWindowListener
  2. {
  3.     private Frame f = new Frame("测试");
  4.     private TextArea ta = new TextArea(6 , 40);
  5.     public void init()
  6.     {
  7.         f.addWindowListener(new MyListener());
  8.         f.add(ta);
  9.         f.pack();
  10.         f.setVisible(true);
  11.     }
  12.     class MyListener implements WindowListener
  13.     {
  14.         public void windowActivated(WindowEvent e)
  15.         {
  16.             ta.append("窗口被激活!/n");
  17.         }
  18.         public void windowClosed(WindowEvent e) 
  19.         {
  20.             ta.append("窗口被成功关闭!/n");
  21.         }
  22.         public void windowClosing(WindowEvent e) 
  23.         {
  24.             ta.append("用户试图关闭窗口!/n");
  25.             System.exit(0);
  26.         }
  27.         public void windowDeactivated(WindowEvent e) 
  28.         {
  29.             ta.append("窗口失去焦点!/n");
  30.         }
  31.         public void windowDeiconified(WindowEvent e) 
  32.         {
  33.             ta.append("窗口被恢复!/n");
  34.         }
  35.         public void windowIconified(WindowEvent e)
  36.         {
  37.             ta.append("窗口被最小化!/n");
  38.         }
  39.         public void windowOpened(WindowEvent e) 
  40.         {
  41.             ta.append("窗口初次被打开!/n");
  42.         }
  43.     }
  44.     public static void main(String[] args) 
  45.     {
  46.         new TestWindowListener().init();
  47.     }
  48. }
事件适配器--实现监听器接口,每个方法都提供了空方法。这样简化了代码。eg
  1. public class TestWindowAdapter
  2. {
  3.     private Frame f = new Frame("测试");
  4.     private TextArea ta = new TextArea(6 , 40);
  5.     public void init()
  6.     {
  7.         f.addWindowListener(new MyListener());
  8.         f.add(ta);
  9.         f.pack();
  10.         f.setVisible(true);
  11.     }
  12.     class MyListener extends WindowAdapter
  13.     {
  14.         public void windowClosing(WindowEvent e) 
  15.         {
  16.             ta.append("用户试图关闭窗口!/n");
  17.             System.exit(0);
  18.         }
  19.     }
  20.     public static void main(String[] args) 
  21.     {
  22.         new TestWindowAdapter().init();
  23.     }
  24. }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值