我在GUI应用程序中使用JavaFX和Swing。 更详细的说,我在AnchorPane之上使用SwingNode来添加一个JPanel,并在其上添加一些地图(ARCGIS JMap - 只在秋千上运行),它会一直刷新。
当我在两个显示屏幕之间拖动我的应用程序时,有时候我有一个NullPointerException
。
这是它发生的java代码:
MouseInfo.getPointerInfo().getLocation()
在JavaFX元素上执行拖动(事件是JavaFX事件):
private void setDraggable(Parent root) {
if (root != null) {
root.setOnMousePressed((event) -> {
xOffset = mainStage.getX() - event.getScreenX();
yOffset = mainStage.getY() - event.getScreenY();
});
root.setOnMouseDragged((event) -> {
mainStage.setX(event.getScreenX() + xOffset);
mainStage.setY(event.getScreenY() + yOffset);
});
}
但是这个例外发生在AWT线程上 正如我所说,它所代码的代码是(请注意这是内部java代码,而不是我的代码):
MouseInfo.getPointerInfo().getLocation()
我该怎么做才能防止这种情况发生?
另一个问题,也许会给出一个线索 - 我检查过,我唯一的Swing组件(JPanel和JMap)都是JComponent的子组件。但是JLightweightFrame中发生异常。那怎么可能呢?
以下是异常的完整描述:
Message = Thread AWT-EventQueue-0 (Id = 45) throw exception: null Stack-Trace = java.lang.NullPointerException
at sun.swing.JLightweightFrame.updateClientCursor(JLightweightFrame.java:473)
at sun.swing.JLightweightFrame.access$000(JLightweightFrame.java:79)
at sun.swing.JLightweightFrame$1.updateCursor(JLightweightFrame.java:112)
at sun.awt.windows.WLightweightFramePeer.updateCursorImmediately(WLightweightFramePeer.java:92)
at java.awt.Component.updateCursorImmediately(Component.java:3150)
at java.awt.Container.validate(Container.java:1642)
at java.awt.Window.dispatchEventImpl(Window.java:2748)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)