【发布时间】:2015-10-03 14:11:45
【问题描述】:
我试图在 World Wind 中禁用鼠标单击时地球的移动。我希望能够做到:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
ww.addMouseMotionListener(new MyMouseMotionListener());
}
MyMouseMotionListener 使用所有鼠标事件。这不起作用,所以我必须这样做:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
for(MouseMotionListener l : ww.getMouseMotionListeners()) {
if(l.getClass().toString().equals("class gov.nasa.worldwind.awt.AWTInputHandler")) {
ww.removeMouseMotionListener(l);
}
}
}
预计消耗的鼠标事件仍应到达gov.nasa.worldwind.awt.AWTInputHandler 侦听器吗?
更新: WorldWindowGLCanvas 只是在 java.awt.Component 上调用 addMouseMotionListener(),所以显然我不明白消费事件是如何工作的。
更新 2: 尽管与 Swing 共享接口,但使用 AWTInputHandler 作为参数调用 WorldWindowGLCanvas.removeMouseMotionListener() 将阻止所有其他 MouseMotionListeners 接收事件。应该使用 AWTInputHandler 上的 add 和 remove 方法。
【问题讨论】:
-
为什么添加另一个侦听器会阻止任何现有侦听器接收事件?
-
如果我在最近添加的侦听器上消费该事件,那不应该阻止其他侦听器接收同一事件的通知吗?
-
你如何“消费”这个事件?翻阅 java.awt.Component 和 java.awt.AWTEventMulticaster,我看不到任何消费事件的概念。看起来它总是将它们传递给所有注册的听众。
-
使用
MouseEvent.consume()- 这最终会起作用,但只有当侦听器被添加到 World Wind 的AWTInputHandler而不是使用WorldWindowGLCanvas上的方法时。