java中的AWT事件处理问题
作者:SongYuLong的博客
事件处理的过程中,主要涉及3类对象
Event Source
(事件源):事件发生的声所,如按钮,窗口,菜单等组件。Event
(事件):事件封装了GUI组件上发生的特定事情(通常是一次用打操作)。Event Listener
(事件监听器):负责监听事件源所发生的事件,并对各种事件做出响应处理。
实现AWT事件处理机制的步骤如下
1.实现事件监听器类,该监听器类是一个特殊的java类,必须实现一个XxxListener接口。
2.创建普通组件(事件源),创建事件监听器对象。
3.调用addXxxListener()方法将事件监听器对象注册给普通组件(事件源)。当事件源上发生指定事件时,AWT会触发事件监听器,由事件监听器调用相应的方法(事件处理器)来处理事件,事件源上所发生的事件会作为参数传入事件处理器。
AWT的事件类都是AWTEvent类的子类,AWTEvent是EventObject的子类。
AWT事件分为两在类:低级事件和高组事件
1.低级事件
低级事件是指基于特定动作的事件.比如进入,点击,拖入等动作的鼠标事件,当组件得到焦点失去焦点时触发的事件.
ComponentEvent
:组件事件,当组件尺寸发生变化,位置发生移动,显示/隐藏状态发生改变时触发该事件.ContainerEvent
:容器事件,当容器里发生添加级件,删除组件时触发该事件。WindowEvent
:窗口事件,当窗口状态发生改变(如打开,关闭,最大化,最小化)时触发该事件。FocusEvent
:焦点事件,当级件得到焦点或失去焦点时触发该事件。KeyEvent
:键盘事件,当按键被按下,松开单击时触发该事件。MouseEvent
:鼠标事件,当进行单击,按下,松开,移动鼠标等动作时触发该事件。PaintEvent
:组件绘制事件,该事件是一件特殊的事件类型,当GUI组件调用update/paint方法来呈现自身时触发该事件,该事件并非专用于事件处理模型。
2.高级事件(语义事件)
高级事件是基于语义的事件,它可以不和特定的动作相关联,而依赖于触发此事件的类。
比如,在TextField中按Enter键会触发ActionEvent事件,在滑动条上称动滑块会触发AdjustmentEvent事件,选中项目列表的某一项就会触发ItemEvent事件。
ActionEvent
:动作事件,当按钮,菜单项被单击,在TextField中按Enter键时触发该事件。AdjustmentEvent
:调节事件,在滑动条上称动滑块以调节数值时触发该事件。ItemEvent
:选项事件,当用户选中某项,或取消选中某项时触发该事件。TextEvent
:文本事件,当文本框,文本域里的文本发生改变时触发该事件。
一件级件被多个监听器监听:
package javaAWT; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; import java.awt.Panel; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MultiListener { private Frame frame = new Frame("MultiListener"); private TextArea textArea = new TextArea(10, 35); private Button btn1 = new Button("one"); private Button btn2 = new Button("two"); public void init() { FirstListener f1 = new FirstListener(); btn1.addActionListener(f1); btn1.addActionListener(new SecondListener()); btn2.addActionListener(f1); frame.add(textArea); Panel panel = new Panel(); panel.add(btn1); panel.add(btn2); frame.add(panel, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } class FirstListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub textArea.append("FirstListener, EventSource :" + e.getActionCommand() + "\n"); } } class SecondListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub textArea.append("Clicked:" + e.getActionCommand() + " Button\n"); } } public static void main(String[] args) { new MultiListener().init(); } }
窗口监听 (WindowListener ):
package javaAWT; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class WindowListenerTest { private Frame frame = new Frame("WindowListener"); private TextArea tArea = new TextArea(6, 40); public void init() { frame.addWindowListener(new MyListener()); frame.add(tArea); frame.pack(); frame.setVisible(true); } class MyListener implements WindowListener { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub tArea.append("窗口初次打开\n"); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub tArea.append("关闭窗口\n"); System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("窗口被关闭"); } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub tArea.append("窗口最小化\n"); } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub tArea.append("窗口恢复\n"); } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub tArea.append("窗口被激活\n"); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub tArea.append("窗口失去焦点\n"); } } public static void main(String[] args) { new WindowListenerTest().init(); } }
事件适配器
事件适配器是监听器接口的空实现–事件适配器实现了监听器接口,并为该接口里的每个方法都提供了实现,这种实现是一种空实现。
监听器接口和事件适配器对应表
监听器接口 | 事件适配器 |
---|---|
ContainerListener | ContainerAdapter |
FocusListener | FocusAdapter |
ComponentListener | ComponentAdapter |
KeyListener | KeyAdapter |
MouseListener | MouseAdapter |
MousMotionListener | MouseMotionAdapter |
WindowListener | WindowAdapter |
实现事件监听器有如下几种形式
1.内部类形式:将事件监听器类定义成当前类的内部类。
2.外部类形式:将事件监听器类定义成一个外部类。
3.类本身作为事件监听器类:让当前类本身实现监听器接口或继承事件适配器。
4.匿名内部类形式:使用匿名内部类创建事件监听器对象。
内部类形式
package javaAWT; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.Image; import java.awt.TextArea; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; public class WindowAdapterTest { private Frame frame = new Frame("WindowAdapterTest"); private TextArea textArea = new TextArea(6, 40); private ImageIcon imageIcon = new ImageIcon(); public void init() { frame.addWindowListener(new MyAdapterListener()); frame.add(textArea); Image image = Toolkit.getDefaultToolkit().getImage("./123.jpg"); frame.setIconImage(image); frame.pack(); frame.setVisible(true); } class MyAdapterListener extends WindowAdapter { @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub //super.windowClosed(e); System.out.println("Closed window"); System.exit(0); } } public static void main(String[] args) { new WindowAdapterTest().init(); } }
外部类形式
外部类监听器
package javaAWT; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MailerListener implements ActionListener { private TextField mailAddress; public MailerListener(TextField mailAddress) { // TODO Auto-generated constructor stub this.mailAddress = mailAddress; } public void setMailAddress(TextField mailAddress) { this.mailAddress = mailAddress; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("goto "+mailAddress.getText() + " send eMail"); } }
使用外部监听器
package javaAWT; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; import java.awt.TextField; import org.omg.CORBA.PRIVATE_MEMBER; public class SendMailer { private Frame frame = new Frame("SendMailer"); private TextField tField = new TextField(40); private Button send = new Button("Send"); public void init() { send.addActionListener(new MailerListener(tField)); frame.add(tField); frame.add(send, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new SendMailer().init(); } }
类本身作为事件监听器类
GUI类通过继承事件适配器方式(extends XxxAdapter),可以只实现需要的监听器事件处理方法。
package javaAWT; import java.awt.Frame; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import org.omg.CosNaming.NamingContextExtPackage.AddressHelper; public class SimpleEventHandler extends WindowAdapter { private Frame frame = new Frame("WindowAdapter"); private TextArea tArea = new TextArea(6, 40); public void init() { //将该类的默认对象作为事件监听对象 frame.addWindowListener(this); frame.add(tArea); frame.pack(); frame.setVisible(true); } // GUI 界面类直接包含事件处理器方法 @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.out.println("Window closed"); System.exit(0); } public static void main(String[] args) { } }
GUI类通过实现事件监听器方式( implements XxxListener),需要实现监听器的所有事件处理方法。
package javaAWT; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.text.TabableView; public class SimpleEventHandlerB implements WindowListener { private Frame f = new Frame("WindowListener"); private TextArea ta = new TextArea(10, 50); public void init() { f.addWindowListener(this); f.add(ta); f.pack(); f.setVisible(true); } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowOpened"+"\n"); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowClosing"+"\n"); System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowClosed"+"\n"); } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowIconified"+"\n"); } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowDeiconified"+"\n"); } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowActivated"+"\n"); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub ta.append("windowDeactivated"+"\n"); } public static void main(String[] args) { new SimpleEventHandlerB().init(); } }
匿名内部类形式
匿名实现XxxxAdapter对象
package javaAWT; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class AnonymousEventHandler { private Frame frame = new Frame("AnonymousEventHandler"); private TextArea textArea = new TextArea(5, 20); public void init() { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); frame.add(textArea); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new AnonymousEventHandler().init(); } }
匿名实现XxxListener对象
package javaAWT; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class AnonymousEventHandlerB { private Frame frame = new Frame("AnonymousEventHandlerB"); private TextArea textArea = new TextArea(5, 50); public void init() { frame.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); frame.add(textArea); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new AnonymousEventHandlerB().init(); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。