按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
F i l e D i a l o g ContainerEvent; WindowEvent; FocusEvent; KeyEvent; MouseEvent;
C o m p o n e n t E v e n t
F r a m e ContainerEvent; WindowEvent; FocusEvent; KeyEvent; MouseEvent;
C o m p o n e n t E v e n t
L a b e l F o c u s E v e n t ; K e y E v e n t ; M o u s e E v e n t ; C o m p o n e n t E v e n t
L i s t ActionEvent; FocusEvent; KeyEvent; MouseEvent; ItemEvent; ponentEvent
M e n u A c t i o n E v e n t
M e n u I t e m A c t i o n E v e n t
P a n e l ContainerEvent; FocusEvent; KeyEvent; MouseEvent; ponentEvent
P o p u p M e n u Act i o n E v e n t
S c r o l l b a r AdjustmentEvent; FocusEvent; KeyEvent; MouseEvent; ponentEvent
S c r o l l P a n e ContainerEvent; FocusEvent; KeyEvent; MouseEvent; ponentEvent
T e x t A r e a TextEvent; FocusEvent; KeyEvent; MouseEvent; ponentEvent
T e x t C o m p o n e n t T e x t E v e n t; FocusEvent; KeyEvent; MouseEvent; ponentEvent
T e x t F i e l d ActionEvent; TextEvent; FocusEvent; KeyEvent; MouseEvent; ponentEvent
W i n d o w ContainerEvent; WindowEvent; FocusEvent; KeyEvent; MouseEvent;
C o m p o n e n t E v e n t
一旦知道了一个特定的组件支持哪些事件,就不必再去寻找任何东西来响应那个事件。只需简单地:
(1) 取得事件类的名字,并删掉其中的“Event”字样。在剩下的部分加入“Listener ”字样。这就是在我们
的内部类里需要实现的接收器接口。
(2) 实现上面的接口,针对想要捕获的事件编写方法代码。例如,假设我们想捕获鼠标的移动,所以需要为
MouseMotiionListener 接口的 mouseMoved()方法编写代(当然还必须实现其他一些方法,但这里有捷径可
循,马上就会讲到这个问题)。
(3) 为步骤 2 中的接收器类创建一个对象。随自己的组件和方法完成对它的注册,方法是在接收器的名字里
加入一个前缀“add”。比如addMouseMotionListener()。
下表是对接收器接口的一个总结:
接收器接口 接口中的方法
Listener interface M e t h o d s i n i n t e r f a c e
w / a d a p t e r
A c t i o n L i s t e n e r actionPerformed(ActionEvent)
AdjustmentListener a d j u s t m e n t V a l u e C h a n g e d (
A d j u s t m e n t E v e n t )
ponentListener ponentHidden(ponentEvent)
ponentAdapter ponentShown(ponentEvent)
ponentMoved(ponentEvent)
ponentResized(ponentEvent)
ContainerListener ponentAdded(ContainerEvent)
ContainerAdapter ponentRemoved(ContainerEvent)
F o c u s L i s t e n e r f o c u s G a i n e d ( F o c u s E v e n t )
412
…………………………………………………………Page 414……………………………………………………………
F o c u s A d a p t e r f o c u s L o s t ( F o c u s E v e n t )
K e y L i s t e n e r k e y P r e s s e d ( K e y E v e n t )
K e y A d a p t e r k e y R e l e a s e d ( K e y E v e n t )
k e y T y p e d ( K e y E v e n t )
M o u s e L i s t e n e r m o u s e C l i c k e d ( M o u s e E v e n t )
M o u s e A d a p t e r m o u s e E n t e r e d ( M o u s e E v e n t )
m o u s e E x i t e d ( M o u s e E v e n t )
m o u s e P r e s s e d ( M o u s e E v e n t )
m o u s e R e l e a s e d ( M o u s e E v e n t )
MouseMotionListener m o u s e D r a g g e d ( M o u s e E v e n t )
MouseMotionAdapter m o u s e M o v e d ( M o u s e E v e n t )
W i n d o w L i s t e n e r w i n d o w O p e n e d ( W i n d o w E v e n t )
W i n d o w A d a p t e r windowClosing(WindowEvent)
w i n d o w C l o sed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
I t e m L i s t e n e r itemStateChanged(ItemEvent)
T e x t L i s t e n e r textValueChanged(TextEvent)
1。 用接收器适配器简化操作
在上面的表格中,我们可以注意到一些接收器接口只有唯一的一个方法。它们的执行是无轻重的,因为我们
仅当需要书写特殊方法时才会执行它们。然而,接收器接口拥有多个方法,使用起来却不太友好。例如,我
们必须一直运行某些事物,当我们创建一个应用程序时对帧提供一个WindowListener,以便当我们得到
windowClosing()事件时可以调用 System。exit(0)以退出应用程序。但因为WindowListener 是一个接口,我
们必须执行其它所有的方法即使它们不运行任何事件。这真令人讨厌。
为了解决这个问题,每个拥有超过一个方法的接收器接口都可拥有适配器,它们的名我们可以在上面的表格
中看到。每个适配器为每个接口方法提供默认的方法。(WindowAdapter 的默认方法不是windowClosing(),
而是System。exit(0)方法。)此外我们所要做的就是从适配器处继承并过载唯一的需要变更的方法。例如,
典型的WindowListener 我们会像下面这样的使用。
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System。exit(0);
}
}
适配器的全部宗旨就是使接收器的创建变得更加简便。
但所谓的“适配器”也有一个缺点,而且较难发觉。假定我们象上面那样写一个 WindowAdapter :
class MyWindowListener extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
System。exit(0);
}
}
表面上一切正常,但实际没有任何效果。每个事件的编译和运行都很正常——只是关闭窗口不会退出程序。
您注意到问题在哪里吗?在方法的名字里:是 WindowClosing(),而不是windowClosing()。大小写的一个简
单失误就会造成一个崭新的方法。但是,这并非我们关闭窗口时调用的方法,所以当然没有任何效果。
413
…………………………………………………………Page 415……………………………………………………………
13。16。3 用 Java 1。1 AWT 制作窗口和程序片
我们经常都需要创建一个类,使其既可作为一个窗口调用,亦可作为一个程序片调用。为做到这一点,只需
为程序片简单地加入一个main()即可,令其在一个 Frame (帧)里构建程序片的一个实例。作为一个简单的
示例,下面让我们来看看如何对Button2New。java 作一番修改,使其能同时作为应用程序和程序片使用:
//: Button2NewB。java
// An application and an applet
import java。awt。*;
import java。awt。event。*; // Must add this
import java。applet。*;
public class Button2NewB extends Applet {
Button
b1 = new Button(〃Button 1〃);
b2 = new Button(〃Button 2〃);
TextField t = new TextField(20);
public void init() {
b1。addActionListener(new B1());
b2。addActionListener(new B2());
add(b1);
add(b2);
add(t);
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t。setText(〃Button 1〃);
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t。setText(〃Button 2〃);
}
}
// To close the application:
static class WL extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System。exit(0);
}
}
// A main() for the application:
public static void main(String'' args) {
Button2NewB applet = new Button2NewB();
Frame aFrame = new Frame(〃Button2NewB〃);
aFrame。addWindowListener(new WL());
aFrame。add(applet; BorderLayout。CENTER);
aFrame。setSize(300;200);
applet。init();
applet。start();
aFrame。setVisible(true);
}
} ///:~
414
…………………………………………………………Page 416……………………………………………………………
内部类WL 和main()方法是加入程序片的唯一两个元素,程序片剩余的部分则原封未动。事实上,我们通常
将WL 类�