除了,上面用到的addMouseListener, Eclipse 还有一些常用的监听器,它们在各组件里的使用方法相同(如果该组件支持此种事件),简单介绍如下:
addSelectionListener: 这个监听器最常用。
widgetSelected 方法:当组件被选择(单击鼠标或按回车键)时触发此方法的事件处理程序。
widgetDefaultSelected 方法: 用于某些很少触发选择事件的组件,例如:文本框回车事件,列表框双击事件等。
addKeyListener:
keyPressed 方法:当前焦点停留在组件时,按下键盘任意键时触发,但对于某些组件(如:button),按回车键无法执行此方法。
keyReleased方法: 按键弹起时触发。
addFocusListener
focusGained方法: 得到焦点时触发。
focusLost方法: 失去焦点时触发。
Table A: Event Types | |
Type | Description |
SWT.Activate | Triggered when the widget(部件) becomes the active window |
SWT.Arm | Triggered when the widget is armed |
SWT.Close | Triggered when the widget is closed |
SWT.Collapse | Triggered when a tree node is collapsed |
SWT.Deactivate | Triggered when the widget is no longer the active window |
SWT.DefaultSelection | Triggered when the default selection occurs |
SWT.Deiconify | Triggered when the widget is restored from being minimized |
SWT.Dispose | Triggered when the widget is disposed |
SWT.DragDetect | Triggered when the widget is dragged |
SWT.Expand | Triggered when a tree node is expanded |
SWT.FocusIn | Triggered when the widget gains focus |
SWT.FocusOut | Triggered when the widget loses focus |
SWT.HardKeyDown | Triggered when a special hardware key, such as on a Pocket PC device, is pressed |
SWT.HardKeyUp | Triggered when a special hardware key, such as on a Pocket PC device, is released |
SWT.Help | Triggered when the user requests help |
SWT.Hide | Triggered when the widget is hidden |
SWT.Iconify | Triggered when the widget is minimized |
SWT.KeyDown | Triggered when the user presses a key |
SWT.KeyUp | Triggered when the user releases a key |
SWT.MenuDetect | Triggered when a menu is selected |
SWT.Modify | Triggered when the text of a widget is modified |
SWT.MouseDoubleClick | Triggered when the mouse is double-clicked |
SWT.MouseDown | Triggered when the mouse button is clicked |
SWT.MouseEnter | Triggered when the mouse pointer enters the widget |
SWT.MouseExit | Triggered when the mouse pointer exits the widget |
SWT.MouseHover | Triggered when the mouse pointer hovers over the widget |
SWT.MouseMove | Triggered when the mouse pointer moves through the widget |
SWT.MouseUp | Triggered when the mouse button is released |
SWT.Move | Triggered when the widget is moved |
SWT.None | Null event |
SWT.Paint | Triggered when the widget is painted |
SWT.Resize | Triggered when the widget is resized |
SWT.Selection | Triggered when the widget is selected |
SWT.Show | Triggered when the widget is shown |
SWT.Traverse | Triggered when the user tabs through the controls |
SWT.Verify | Triggered when the text for the widget is about to change, allowing you to veto the change |
SWT提供了很多事件监听器的接口(Interface)和适配器(Adapter),可以为控件添加相应的监听器。
为一个文本框添加按键监听器代码形如:
txtSample.addKeyListener(new KeyAdapter() { |
为一个按键添加单击事件监听器,代码形如:
btnSample.addSelectionListener(new SelectionAdapter() { |
给文本框添加文本框内容改变的监听器,代码形如:
txtSample.addModifyListener(new ModifyListener() { |
控件的鼠标双击事件也是经常用到的,其添加监听器的代码形如:
shell.addMouseListener(new MouseListener() { |
另外,当程序中要处理数据输入时,SWT包提供的输入验证监听器非常有用,它可以避免用户的无效数据输入,其添加监听器的代码形如:
/*Called when user types into text box,but before the text box gets what the user typed*/ |
最后再给一个如何处理消息框用户按了哪个键的代码例子:
MessageBox msgBox = new MessageBox( shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION ); |
对于每一种监听器,都对应有一个接口定义这种监听器(XyzListener),一个类提供事件信息(XyzEvent),还有一个应用程序接口方法负责添加监听器(addXyzListener)。如果一个监听器接口中定义了不只一个方法,那么相应地会提供一个适配器(XyzAdapter),它实现了监听器接口并提供一些空方法(你可以覆盖之)。所有的事件类,监听器类以及适配器类都定义在包org.eclipse.swt.events.里面。
以下表格总结了有效的事件以及对应的支持事件的部件:
事件类型
|
描述
|
部件
|
Arm
|
当部件处于高亮(将被选择)时发生,如菜单项。
|
MenuItem
|
Control
|
当控件被移动或改变大小时发生
|
Control, TableColumn, Tracker
|
Dispose
|
当部件被销毁时发生,无论销毁是由于用户操作还是程序操作
|
Widget
|
Focus
|
当部件获得或者失去焦点时发生
|
Control
|
Help
|
当用户请求对部件的帮助说明时发生,比如按下F1
|
Control, Menu, MenuItem
|
Key
|
当控件拥有键盘焦点,用户压下或释放按键时发生。
|
Control
|
Menu
|
当菜单被隐藏或显示时发生
|
Menu
|
Modify
|
当部件的文本内容被改变时发生
|
CCombo, Combo, Text, StyledText
|
Mouse
|
当用户在控件上压下、释放或双击鼠标时发生
|
Control
|
MouseMove
|
当用户把鼠标从控件上移过时发生
|
Control
|
MouseTrack
|
当鼠标进入、退出或在控件上悬浮时发生
|
Control
|
Paint
|
当控件需要被重绘时发生
|
Control
|
Selection
|
当用户选择了控件中的一项时发生
|
Button, CCombo, Combo, CoolItem, CTabFolder, List, MenuItem, Sash, Scale, ScrollBar, Slider, StyledText, TabFolder, Table, Table Cursor, TableColumn, TableTree, Text, ToolItem, Tree
|
Shell
|
当Shell窗口被最小化、最大化、激活、不激活或被关闭时发生。
|
Shell
|
Traverse
|
当用户使用键盘按键访问控件时发生
|
Control
|
Tree
|
当用户展开或收缩树时发生
|
Tree, TableTree
|
Verify
|
当部件的文本内容将要发生改变的时候发生,给了应用程序一个机会去修改或防止修改部件的文本内容
|
|
无类型事件
以上描述的有类型事件系统实际上是由低级别的,无类型的部件事件机制实现的。这种机制不打算让应用程序使用,不过你会看到SWT的实现中使用了这种机制,很多工作台向导页的实现也使用了这种机制。
无类型事件机制依赖一个常量去标识事件类型并且为此定义了一个通用的监听器。这允许监听器实现“case样式”的监听模式。在以下的代码片断,我们定义一个通用的事件句柄并把若干监听器附加到Shell窗口。
Shell shell = new Shell ();
Listener listener = new Listener () {
public void handleEvent (Event e) {
switch (e.type) {
case SWT.Resize:
System.out.println ("Resize received");
break;
case SWT.Paint:
System.out.println ("Paint received");
break;
default:
System.out.println ("Unknown event received");
}
}
};
shell.addListener (SWT.Resize, listener);
shell.addListener (SWT.Paint, listener);
-
顶