原文地址:http://jamesblog.iteye.com/blog/849506
创建了一个display对象和一些用户界面部件,启动应用程序的消息循环,当有事件发生时,应用程序从消息队列中读取到事件并分发给部件。这就是SWT程序的运行机制。大部分应用程序的逻辑都是在处理用户事件。
基本的模式是为小部件加上监听器(listener),当对应的事件被触发时,监听器就会被执行,下面是一个简单的列子:
Display display = new Display ();
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.CENTER);
...
shell.addControlListener (new ControlAdapter () {
public void controlResized (ControlEvent e) {
label.setBounds (shell.getClientArea ());
}
});
对于每一种监听器(listener),对应一个定义监听器的接口(XyzListener),一个提供事件信息的类(XyzEvent),和一个添加监听器的API方法(addXyzListener),如果监听器定义了多于一个的方法,会有一个实现了监听器接口的适配器(XyzAdapter),适配器提供了空方法。所有的这些事件(events)、监听器(listeners)和适配器(adapters)都定义在包org.eclipse.swt.events 中。
事件可以分为两类,一类是高层事件,表示发生在控件上的逻辑操作,一类是底层事件,描述更为具体的用户交互动作,高层的事件可能是由多个平台相异的事件表示的,一般情况下,底层事件应该仅用于自定制的用户界面部件的实现中。
下面列出了SWT中的事件类型:
High level Events:
Activate, Deactivate
Generated when a Control is activated or deactivated.
控件被激活或失去激活状态时产生的事件
Arm
A MenuItem is armed (highlighted and ready to be selected).
菜单项获得焦点时触发的事件
Close
A Shell is about to close as requested by the window manager.
关闭shell窗口触发的事件
DefaultSelection
The user selects an item by invoking a default selection action. For example, by hitting Enter or double clicking on a row in a Table.
当用户执行一个默认的选择动作是触发的事件,例如按下回车键或双击表格中的一行。
Dispose
A widget is about to be disposed, either programmatically or by user.
程序或者用户销毁小部件时触发的事件
DragDetect
The user has initiated a possible drag operation.
当用户做拖动操作触发的事件
EraseItem
A TableItem or TreeItem is about to have its background drawn.
绘制表格的行或这树的节点的背景时触发的事件
Expand, Collapse
An item in a Tree is expanded or collapsed.
展开或销毁树的节点时触发的事件
Help
The user has requested help for a widget. For example, this occurs when the F1 key is pressed under Windows.
用户请求帮助时触发的事件,例如在Windows系统中按下F1键。
Iconify, Deiconify
A Shell has been minimized, maximized, or restored.
最小化、最大化、恢复 shell窗口时触发的事件
ImeComposition
Allows custom text editors to implement in-line editing of international text.
允许定制文本编辑器实现行内编辑多国文本内容。
MeasureItem
The size of a custom drawn TableItem or TreeItem is being requested.
获取自定制的表格的行或树的节点的尺寸时触发的事件
MenuDetect
The user has requested a context menu.
用户请求上下文菜单时触发
Modify
The widget's text has been modified.
修改文本内容时触发
Move, Resize
A control has changed position or has been resized, either programmatically or by user.
通过程序或者用户直接移动控件位置,或改变控件大小时触发的事件,
Movement
An updated caret offset is needed in response to a user action in a StyledText.
指针偏移时触发
OpenDocument
The operating system has requested that a document be opened.
操作系统请求打开文档时触发
OrientationChange
The orientation of a Text control is changing.
文本控件改变方向时触发
PaintItem
A TableItem or TreeItem is about to have its foreground drawn.
改变表格的行或树的节点前景时触发
Selection
The user selects an item in the control. For example, by single clicking on a row in a Table or by keyboard navigating through the items.
用户选择控件的一个项时触发,例如点击表格中的一行,或者用键盘的方向键选择UI项时
SetData
Data needs to be set on a TableItem when using a virtual table.
为控件设置数据时触发
Settings
An operating system property, such as a system font or color, has been changed.
操作系统属性,例如系统字体和颜色,发生变化是触发
Show, Hide
A control's visibility has changed.
控件显示状态发生变化时触发
Traverse
The user is trying to traverse out of the control using a keystroke. For example, the escape or tab keys are used for traversal.
使用escape键改变状态时 或 使用tab键使得控件焦点发生变化时触发的事件
Verify
A widget's text is about to be modified. This event gives the application a chance to alter the text or prevent the modification.
当修改文本框的内容时,这个事件使得应用程序有机会能够修改文本内容或阻止内容的修改。
Low level events:
FocusIn, FocusOut
A control has gained or lost focus.
控件获得或失去焦点时触发的事件
KeyDown, KeyUp
The user has pressed or released a keyboard key when the control has keyboard focus.
控件拥有键盘焦点,同时用户按下或释放键盘键时触发的事件
MouseDown, MouseUp, MouseDoubleClick
The user has pressed, released, or double clicked the mouse over the control.
用户在控件上按下、释放或双击鼠标时触发的事件
MouseMove
The user has moved the mouse above the control.
用户在控件上移动鼠标时触发的事件
MouseEnter, MouseExit, MouseHover
The mouse has entered, exited, or hovered over the control.
鼠标进入、离开或悬浮在控件上时触发的事件
MouseHorizontalWheel, MouseVerticalWheel, MouseWheel
The mouse wheel has been rotated.
鼠标滚轮滚动时触发的控件
Paint
The control has been damaged and requires repainting.
控件重绘时触发的事件
附: