我正在尝试用 JavaFX 中的事件处理来做一些越野的事情。我需要能够确定手动触发事件后是否已消耗该事件。
在以下示例中,正确接收了合成鼠标事件,但调用 Consumer() 不会更新该事件。
我对此进行了调试,发现 JavaFX 实际上创建了一个新的事件实例,因此原始事件实例没有改变
public class EventManipulation extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button();
button.setOnMouseDragged(event -> {
System.out.println("dragged");
event.consume();
});
primaryStage.setScene(new Scene(new HBox(button), 400, 300));
primaryStage.show();
MouseEvent event = new MouseEvent(MouseEvent.MOUSE_DRAGGED, 0, 0, 0, 0, MouseButton.PRIMARY, 1, false, false,
false, false, false, false, false, false, false, false, null);
Event.fireEvent(button, event);
System.out.println(event.isConsumed()); // <== prints false
}
}
Run Code Online (Sandbox Code Playgroud)
我发现了 EventDispatchChain,但是我不知道如何让它工作。该按钮可以生成一个事件调度链,但需要一个开始...以下失败,因为我不知道如何创建初始尾部。
Event result = button.buildEventDispatchChain(null).dispatchEvent(event);
System.out.println(result.isConsumed());
Run Code Online (Sandbox Code Playgroud)
我对此唯一的解决方案是实现EventDispatchChain接口。一个相当小的界面如下。不幸的是,javafx 使用的内置版本位于不可访问的包中 -com.sun.javafx.event.EventDispatchChainImpl
private class SimpleChain implements EventDispatchChain {
private Deque<EventDispatcher> dispatchers = new LinkedList<>();
@Override
public EventDispatchChain append(EventDispatcher eventDispatcher) {
dispatchers.addLast(eventDispatcher);
return this;
}
@Override
public EventDispatchChain prepend(EventDispatcher eventDispatcher) {
dispatchers.addFirst(eventDispatcher);
return this;
}
@Override
public Event dispatchEvent(Event event) {
if (dispatchers.peekFirst() != null) {
Event result = dispatchers.removeFirst().dispatchEvent(event, this);
if (result != null) {
return result;
} else {
event.consume();
return event;
}
} else {
return event;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样使用时会产生预期的结果
Event result = button.buildEventDispatchChain(new SimpleChain()).dispatchEvent(event);
System.out.println(result.isConsumed());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
718 次 |
| 最近记录: |