-
The SWT event-processing cycle
1. Once an SWT application begins running, its Display class sorts through this queue using its readAndDispatch() method and msg field, which acts as a handle to the underlying OS message queue.
2. If it finds anything relevant, it sends the event to its top-level Shell object, which determines which widget should receive the event.
3. The Shell then sends the event to the widget that the user acted on, which transfers this information to an associated interface called a listener.
4. One of the listener’s methods performs the necessary processing or invokes another method to handle the user’s action, called an event handler.
所在包:org.eclipse.swt.events
??
-
typed listeners and events
typed listeners--只對某一類的用戶事件起作用,繼承TypedListener類
typed events--與此類特定動作相關的事件,繼承TypedEvent類
可通過add...Listener()method with the typed listener as the argument來將listener附加到widget。
完整的typed events和listeners列表,如下:
?
TypedEvent類包含了一些member field,他們提供與事件發生相關的一些信息,這些信息可以在event handler中使用來獲得與環境相關的信息。下圖為繼承自TypedEvent及EventObject類的fields:

除此之外,很多的event類還有其他用來提供更多用戶動作信息的fields,如MouseEvent類的button field
要將listener加入到code中,有兩個主要的方法:
第一個是在component的add...Listener()中創建一個匿名接口,這樣可以使listener的作用域僅限于此component,示例代碼如下:
Button button = new Button(shell, SWT.PUSH | SWT.CENTER);//創建了一個button,并將其加入到shell中
button.addMouseListener(new MouseListener() //創建了一個匿名MouseListener接口,并將其與button關聯
{
public void mouseDown(MouseEvent e)
{
clkdwnEventHandler();
}
public void mouseUp(MouseEvent e)
{
clkupEventHandler();
}
public void mouseDoubleClick(MouseEvent e)
{
dblclkEventHandler(); //此接口中必須被實現的三個方法。一旦鼠標按下,放開或雙擊,就會有一個MouseEvent被發送到這三個方法中的一個,然后,此方法再調用相關聯的event-handling方法(即下文中的三個)。
}
});
static void dblclkEventHandler()
{
System.out.println("Double click.");
}
static void clkdwnEventHandler()
{
System.out.println("Click - down.");
}
static void clkupEventHandler()
{
System.out.println("Click - up.");//event-handlers通過發送message到console來完成事件處理
}
上一類方法的缺點是此listener僅限于此component內,而第二種方法便可解決這種問題--獨立的聲明一個繼承MouseListener的接口,示例代碼如下:
Button button = new Button(shell, SWT.PUSH | SWT.CENTER);
button.addMouseListener(ExampleMouseListener);
MouseListener ExampleMouseListener = new MouseListener()
{
public void mouseDoubleClick(MouseEvent e)
{
System.out.println("Double click.");
}
public void mouseDown(MouseEvent e)
{
System.out.println("Click - down.");
}
public void mouseUp(MouseEvent e)
{
System.out.println("Click - up.");
}
};
使用MouseListener的缺點就是哪怕你只關心鼠標雙擊事件,卻仍要聲明其接口中所包含的所有方法。?
Adapter是繼承了Listener接口并提供了所有required方法的實現的abstract類。也就是說如果你使用adapter而不是listener的話,你只需要寫你感興趣的方法
只有那些listener有多個成員方法的event才有adapter,其完整列表見下圖:

adapter同樣是通過add...Listener()方法來創建的,與listener類似,同樣也可通過匿名類和本地類兩種方法,下例為匿名類方法:
button.addMouseListener(new MouseAdapter()
{
public void mouseDoubleClick(MouseEvent e)
{
dblclkEventHandler();
}
)};
static void dblclkEventHandler()
{
System.out.println("Double click.");
}
任何時候只要key被按下,就會創建KeyEvent,它有兩個子類:TraverseEvent 和VerifyEvent.
TraverseEvent--當按下arrow key或tab key來focus on text widget時
VerifyEvent--fires when the user enters text that the program needs to check before taking further action.
除了繼承自TypedEvent和EventObject的field,KeyEvent還包括三個member field來提供那些與觸發事件的key相關的信息,具體如下:
character--代表被按下key的char
stateMask--Returns an integer representing the state of the keyboard modifier keys.By examining this integer, a program can determine whether any of the Alt, Ctrl, Shift, and Command keys are currently pressed.
keyCode--Provides the SWT public constant corresponding to the typed key. KeyCode列表,見下圖:

TraverseEvent中有兩個fields:
1. doit--返回布爾值,若為真則允許在各個component間切換focus,若為假則不允許切換focus
2. detail--It’s an integer that represents the identity of the key that caused the event. For example, if the user presses the Tab key to switch to a new component, the detail field will contain the SWT constant TRAVERSE_TAB_NEXT.
每個類型的Control對于所給的traversal key都有不同的默認behavior,如果設doit為true,則override了其默認的設置。
VerifyEvent的field:
1.start和end--設定了輸入的范圍
2.text--contains the input String under examination.
3.doit--Having looked at the user’s text, you set the boolean doit field to allow (TRUE) or disallow (FALSE) the action.
更靈活,但不安全,不推薦使用。
當一個代表著非類型化監聽器的監聽器類和GUI的某一組件相聯系時,它就能接受該組件所能發送的任一類事件。因此,你需要操控這由Event類代表的捕獲的全部事件,決定用戶執行的那個動作。然后,正確的事件處理方法就被調用。
不是被包含在org.eclipse.swt.events包中,而是被包含在org.eclipse.swt.widgets包中。
代碼示例如下:
Listener listener = new Listener ()
{
public void handleEvent (Event event)
{
switch (event.type)
{
case SWT.KeyDown:
if (event.character == 'b')
System.out.println("Key"+event.character);
break;
case SWT.MouseDown:
if (event.button == 3)
System.out.println("Right click");
break;
case SWT.MouseDoubleClick:
System.out.println("Double click");
break;
}
}
};
Button button = new Button(shell, SWT.CENTER);
button.addListener(SWT.KeyDown, listener);
button.addListener(SWT.MouseDown, listener);
button.addListener(SWT.MouseDoubleClick, listener);
Event類包含了所有typed event中的所有field,此外還有一個type field,其所有的值列表如下:
posted on 2006-03-23 16:32
JOO 閱讀(819)
評論(0) 編輯 收藏 所屬分類:
SWT & JFace IN ACTION