1

我在 SWT.MouseUp 和 SWT.MouseDown 事件上为 Shell 附加了一个侦听器,但 handleEvent 方法永远不会被触发。我尝试单击窗口上的许多地方,但它甚至没有到达下面代码中的 System.out.println(..) ...

你发现这里有什么错误吗?

谢谢!

//c is a Composite.

final Listener l = new Listener(){
public void handleEvent(Event event) {
System.out.println("Got event. "+event);
Rectangle rect = c.getBounds();
if (rect.contains(event.x, event.y)){
    if((Boolean)c.getData("selected")){
        c.setData("selected", Boolean.FALSE);
    }else{
        c.setData("selected", Boolean.TRUE);
    }
}
}
};
c.getShell().addListener(SWT.MouseUp, l);
c.getShell().addListener(SWT.MouseDown, l);

(这个组合在一个使用 Forms Toolkit 的 Eclipse 编辑器中)

问候,

-帕迪姆纳

4

1 回答 1

2

通过写作

c.getShell().addListener(SWT.MouseUp, l);
c.getShell().addListener(SWT.MouseDown, l);

您只将侦听器添加到外壳!单击 shell 的子级不会触发c.getShell(). 尝试在窗口边界附近单击并注意您的跟踪消息。

If you want to get events for the clicks on c, you have to add listeners to c via c.addListener(.). If you do that, you won't need the condition rect.contains(event.x, event.y) because you know, that the click happened on c.

于 2009-12-22T19:40:32.240 回答