我有这样的代码:
public void handle(MouseEvent event) {
//if (event.getEventType() == MouseEvent.MOUSE_CLICKED) {
if ( ( event.getEventType() == MouseEvent.MOUSE_PRESSED )&&
(event.isPrimaryButtonDown()) ) {
x1 = x2 = event.getX();
y1 = y2 = event.getY();
line = new BoundLine(pane, event, x1, y1, x2, y2);
pane.getChildren().add(line);
//line.getEventDispatcher().dispatchEvent(event, null);
}
}
是否可以立即将“MouseEvent 事件”传播到javafx.scene.shape.Line
(代码中的行)对象?我试过line.getEventDispatcher().dispatchEvent(event, null);
了,但我收到了这个消息错误:
testjavafxapplication.LinesEditCtx02$MouseHandler.handle(LinesEditCtx02.java:243) 的 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 的线程“JavaFX 应用程序线程”java.lang.NullPointerException 异常。 LinesEditCtx02$MouseHandler.handle(LinesEditCtx02.java:105) 在 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 在 com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 在 com.sun.javafx.event.BasicEventDispatcher。dispatchEvent(BasicEventDispatcher.java:37) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun .javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 在 com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 在 com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java :33) 在 javafx.scene.Scene$MouseHandler.process(Scene.java:3324) 在 javafx.scene.Scene$MouseHandler.process(Scene.java: 3164) 在 javafx.scene.Scene.Scene$MouseHandler.access$1900(Scene.java:3119) 在 javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559) 在 javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261) 在 com.sun.glass.ui.View.handleMouseEvent(View.java:528) 在 com.sun.glass.ui .View.notifyMouse(View.java:922) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java: 82) 在 java.lang.Thread.run(Thread.java:722)