The Goal
Keep walking……
posts - 23,  comments - 1,  trackbacks - 0
  • 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

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.");
}

  • Keyboard events

任何时候只要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.

  • untyped events

更灵活,但不安全,不推荐使用。
当一个代表着非类型化监听器的监听器类和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 阅读(805) 评论(0)  编辑  收藏 所属分类: SWT & JFace IN ACTION

只有注册用户登录后才能发表评论。


网站导航:
 
Hit the target!

<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类(23)

随笔档案(22)

文章档案(1)

相册

Neighbor

搜索

  •  

最新评论

阅读排行榜

评论排行榜