例如,与键盘事件KeyEvent相对应的接口是:
public interface KeyListener extends EventListener {
public void keyPressed(KeyEvent ev);
public void keyReleased(KeyEvent ev);
public void keyTyped(KeyEvent ev);
}
注意到在本接口中有三个方法,那么java运行时系统何时调用哪个方法?其实根据这三个方法的方法名就能够知道应该是什么时候调用哪个方法执行了。当键盘刚按下去时,将调用keyPressed( )方法执行,当键盘抬起来时,将调用keyReleased( )方法执行,当键盘敲击一次时,将调用keyTyped( )方法执行。
又例如窗口事件接口:
public interface WindowListener extends EventListener{
public void windowClosing(WindowEvent e);
//把退出窗口的语句写在本方法中
public void windowOpened(WindowEvent e);
//窗口打开时调用
public void windowIconified(WindowEvent e);
//窗口图标化时调用
public void windowDeiconified(WindowEvent e);
//窗口非图标化时调用
public void windowClosed(WindowEvent e);
//窗口关闭时调用
public void windowActivated(WindowEvent e);
//窗口激活时调用
public void windowDeactivated(WindowEvent e);
//窗口非激活时调用
}
AWT的组件类中提供注册和注销监听器的方法:
◇ 注册监听器:
public void add<ListenerType> (<ListenerType>listener);
◇ 注销监听器:
public void remove<ListenerType> (<ListenerType>listener);
例如Button类:(查API)
public class Button extends Component {
……
public synchronized void addActionListener(ActionListener l);
public synchronized void removeActionListener(ActionListener l);
……}
5.2.3 AWT事件及其相应的监听器接口(1)
表5.1列出了所有AWT事件及其相应的监听器接口,一共10类事件,11个接口。下面这张表应能牢牢记住。
表5.1
事件类别 |
描述信息 |
接口名 |
方法 |
ActionEvent |
激活组件 |
ActionListener |
actionPerformed(ActionEvent) |
ItemEvent |
选择了某些项目 |
ItemListener |
itemStateChanged(ItemEvent) |
MouseEvent |
鼠标移动 |
MouseMotionListener |
mouseDragged(MouseEvent) mouseMoved(MouseEvent) |
鼠标点击等 |
MouseListener |
mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) |
KeyEvent |
键盘输入 |
KeyListener |
keyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) |
FocusEvent |
组件收到或失去焦点 |
FocusListener |
focusGained(FocusEvent) focusLost(FocusEvent) |
AdjustmentEvent |
移动了滚动条等组件 |
AdjustmentListener |
adjustmentValueChanged(AdjustmentEvent) |
ComponentEvent |
对象移动缩放显示隐藏等 |
ComponentListener |
componentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) |
WindowEvent |
窗口收到窗口级事件 |
WindowListener |
windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) |
ContainerEvent |
容器中增加删除了组件 |
ContainerListener |
componentAdded(ContainerEvent) componentRemoved(ContainerEvent) |
TextEvent |
文本字段或文本区发生改变 |
TextListener |
textValueChanged(TextEvent) |
5.2.3 AWT事件及其相应的监听器接口(2)
例5.10说明事件处理模型的应用。
例5.10
import java.awt.*;
import java.awt.event.*;
public class ThreeListener implements MouseMotionListener,MouseListener,WindowListener {
//实现了三个接口
private Frame f;
private TextField tf;
public static void main(String args[])
{
ThreeListener two = new ThreeListener();
two.go(); }
public void go() {
f = new Frame("Three listeners example");
f.add(new Label("Click and drag the mouse"),"North");
tf = new TextField(30);
f.add(tf,"South"); //使用缺省的布局管理器
f.addMouseMotionListener(this); //注册监听器MouseMotionListener
f.addMouseListener(this); //注册监听器MouseListener
f.addWindowListener(this); //注册监听器WindowListener
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged (MouseEvent e) {
//实现mouseDragged方法
String s = "Mouse dragging : X="+e.getX()+"Y = "+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
//对其不感兴趣的方法可以方法体为空
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
String s = "The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s = "The mouse has left the building";
tf.setText(s);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){ }
public void windowClosing(WindowEvent e) {
//为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法
System.exit(1);
}
public void windowOpened(WindowEvent e) {}
//对其不感兴趣的方法可以方法体为空
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) {}
}
上例中有如下几个特点:
1.可以声明多个接口,接口之间用逗号隔开。
……implements MouseMotionListener, MouseListener, WindowListener;
2.可以由同一个对象监听一个事件源上发生的多种事件:
f.addMouseMotionListener(this);
f.addMouseListener(this);
f.addWindowListener(this);
则对象f 上发生的多个事件都将被同一个监听器接收和处理。
3.事件处理者和事件源处在同一个类中。本例中事件源是Frame f,事件处理者是类ThreeListener,其中事件源Frame f是类ThreeListener的成员变量。
4.可以通过事件对象获得详细资料,比如本例中就通过事件对象获得了鼠标发生时的坐标值。
public void mouseDragged(MouseEvent e) {
String s="Mouse dragging :X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
Java语言类的层次非常分明,因而只支持单继承,为了实现多重继承的能力,Java用接口来实现,一个类可以实现多个接口,这种机制比多重继承具有更简单、灵活、更强的功能。在AWT中就经常用到声明和实现多个接口。记住无论实现了几个接口,接口中已定义的方法必须一一实现,如果对某事件不感兴趣,可以不具体实现其方法,而用空的方法体来代替。但却必须所有方法都要写上。
5.2.4 事件适配器
Java语言为一些Listener接口提供了适配器(Adapter)类。可以通过继承事件所对应的Adapter类,重写需要方法,无关方法不用实现。事件适配器为我们提供了一种简单的实现监听器的手段, 可以缩短程序代码。但是,由于java的单一继承机制,当需要多种监听器或此类已有父类时,就无法采用事件适配器了。
1.事件适配器--EventAdapter
下例中采用了鼠标适配器:
import java.awt.*;
import java.awt.event.*;
public class MouseClickHandler extends MouseAdaper{
public void mouseClicked(MouseEvent e) //只实现需要的方法
{ ……}
}
java.awt.event包中定义的事件适配器类包括以下几个:
1.ComponentAdapter( 组件适配器)
2.ContainerAdapter( 容器适配器)
3.FocusAdapter( 焦点适配器)
4.KeyAdapter( 键盘适配器)
5.MouseAdapter( 鼠标适配器)
6.MouseMotionAdapter( 鼠标运动适配器)
7.WindowAdapter( 窗口适配器)
2. 用内部类实现事件处理
内部类(inner class)是被定义于另一个类中的类,使用内部类的主要原因是由于:
◇ 一个内部类的对象可访问外部类的成员方法和变量,包括私有的成员。
◇ 实现事件监听器时,采用内部类、匿名类编程非常容易实现其功能。
◇ 编写事件驱动程序,内部类很方便。
因此内部类所能够应用的地方往往是在AWT的事件处理机制中。
例5.11
import java.awt.* ;
import java.awt.event.*;
public class InnerClass{
private Frame f;
private TextField tf;
public InnerClass(){
f=new Frame("Inner classes example");
tf=new TextField(30);
}
public voidi launchFrame(){
Label label=new Label("Click and drag the mouse");
f.add(label,BorderLayout.NORTH);
f.add(tf,BorderLayout.SOUTH);
f.addMouseMotionListener(new MyMouseMotionListener());/*参数为内部类对象*/
f.setSize(300,200);
f.setVisible(true);
}
class MyMouseMotionListener extends MouseMotionAdapter{ /*内部类开始*/
public void mouseDragged(MouseEvent e) {
String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
tf.setText(s); }
} ;
public static void main(String args[]) {
InnerClass obj=new InnerClass();
obj.launchFrame();
}
}//内部类结束
}
3.匿名类(Anonymous Class)
当一个内部类的类声名只是在创建此类对象时用了一次,而且要产生的新类需继承于一个已有的父类或实现一个接口,才能考虑用匿名类,由于匿名类本身无名,因此它也就不存在构造方法,它需要显示地调用一个无参的父类的构造方法,并且重写父类的方法。所谓的匿名就是该类连名字都没有,只是显示地调用一个无参的父类的构造方法。
例5.12
import java.awt.* ;
import java.awt.event.*;
public class AnonymousClass{
private Frame f;
private TextField tf;
public AnonymousClass(){
f=new Frame("Inner classes example");
tf=new TextField(30);
}
public void launchFrame(){
Label label=new Label("Click and drag the mouse");
f.add(label,BorderLayout.NORTH);
f.add(tf,BorderLayout.SOUTH);
f.addMouseMotionListener(new MouseMotionAdapter(){ //匿名类开始
public void mouseDragged(MouseEvent e){
String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
tf.setText(s); }
} ); //匿名类结束
f.setSize(300,200);
f.setVisible(true);
}
public static void main(String args[]) {
AnonymousClass obj=new AnonymousClass();
obj.launchFrame();
}
}
其实大家仔细分析一下,例5.11和5.12实现的都是完全一样的功能,只不过采取的方式不同。5.11中的事件处理类是一个内部类,而5.12的事件处理类是匿名类,可以说从类的关系来说是越来越不清楚,但是程序也越来越简练。熟悉这两种方式也十分有助于大家编写图形界面的程序。