事件和采取者典范(java)[Java编程]
本文“事件和采取者典范(java)[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
全部AWT组件都被改变成包含addXXXListener()和removeXXXListener()办法,因此特定的接纳器范例可从每个组件中增添和删除.
我们会注意到“XXX”在每个场所中一样表示自变量的办法,比方,addFooListener(FooListener fl).
下面这张表格总结了通过供应addXXXListener()和removeXXXListener()办法,从而支持那些特定事件的相关事件、接纳器、办法以及组件.
事件,接纳器接口及增添和删除办法 支持这个事件的组件
Event, listener interface and add- and remove-methods |
Components supporting this event |
---|---|
ActionEvent |
Button, List, TextField, MenuItem, and its derivatives including CheckboxMenuItem, Menu, and PopupMenu |
AdjustmentEvent |
Scrollbar |
ComponentEvent |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
ContainerEvent |
Container and its derivatives, including Panel, Applet, ScrollPane, Window, Dialog, FileDialog, and Frame |
FocusEvent |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame
Label, List, Scrollbar, TextArea, and TextField |
KeyEvent |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
MouseEvent (for both clicks and motion) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
MouseEvent[55]
(for both clicks and motion) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
WindowEvent |
Window and its derivatives, including Dialog, FileDialog, and Frame |
ItemEvent |
Checkbox, CheckboxMenuItem, Choice, List, and anything that implements the ItemSelectable interface |
TextEvent |
Anything derived from TextComponent, including TextArea and TextField |
⑤:即便表面上如此,但实际上并没有MouseMotiionEvent(鼠标运动事件).单击和运动都合成到MouseEvent里,所以MouseEvent在表格中的这种另类行为并非一个错误.
可以看到,每种范例的组件只为特定范例的事件供应了支持.这有助于我们发现由每种组件支持的事件,以下表所示:
组件范例 支持的事件
Component type |
Events supported by this component |
---|---|
Adjustable |
AdjustmentEvent |
Applet |
ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Button |
ActionEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Canvas |
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Checkbox |
ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
CheckboxMenuItem |
ActionEvent, ItemEvent |
Choice |
ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Component |
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Container |
ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Dialog |
ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
FileDialog |
ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Frame |
ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Label |
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
List |
ActionEvent, FocusEvent, KeyEvent, MouseEvent, ItemEvent, ComponentEvent |
Menu |
ActionEvent |
MenuItem |
ActionEvent |
Panel |
ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
PopupMenu |
ActionEvent |
Scrollbar |
AdjustmentEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
ScrollPane |
ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextArea |
TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextComponent |
TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextField |
ActionEvent, TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Window |
ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
一旦知道了一个特定的组件支持哪些事件,就没必要再去探求任何东西来呼应那个事件.只需简单地:
(1) 获得事件类的名字,并删掉此中的“Event”字样.在剩下的部份加入“Listener”字样.这就是在我们的内部类里需求实现的接纳器接口.
(2) 实现上面的接口,针对想要捕捉的事件编写办法代码.比方,假定我们想捕捉鼠标的移动,所以需求为MouseMotiionListener接口的mouseMoved()办法编写代(当然还必须实现其他一些办法,但这里有捷径可循,即刻就会讲到这个问题).
(3) 为步骤2中的接纳器类成立一个对象.随自己的组件和办法完成对它的注册,办法是在接纳器的名字里加入一个前缀“add”.比方addMouseMotionListener().
下表是对接纳器接口的一个总结:
接纳器接口 接口中的办法
Listener interface |
Methods in interface |
---|---|
ActionListener |
actionPerformed(ActionEvent) |
AdjustmentListener |
adjustmentValueChanged( |
ComponentListener |
componentHidden(ComponentEvent) |
ContainerListener |
componentAdded(ContainerEvent) |
FocusListener |
focusGained(FocusEvent) |
KeyListener |
keyPressed(KeyEvent) |
MouseListener |
mouseClicked(MouseEvent) |
MouseMotionListener |
mouseDragged(MouseEvent) |
WindowListener |
windowOpened(WindowEvent) |
ItemListener |
itemStateChanged(ItemEvent) |
TextListener |
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().大小写的一个简单失误就会造成一个崭新的办法.但是,这并非我们关闭窗口时调用的办法,所以当然没有任何效果.
以上是“事件和采取者典范(java)[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |