尝试删除形状时出现java JavaFX错误
我试图从我的窗口中删除一个矩形,如果它被移动到屏幕的某个部分
这是我得到的错误:
Exception in thread "Thread-1539" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-1539 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:238) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:400) at javafx.scene.Parent$1.onProposedChange(Parent.java:245) at com.sun.javafx.collections.VetoableObservableList.remove(VetoableObservableList.java:172) at com.sun.javafx.collections.ObservableListWrapper.remove(ObservableListWrapper.java:263) at com.sun.javafx.collections.VetoableObservableList.remove(VetoableObservableList.java:179) at MovementSample$HandListener.onFrame(MovementSample.java:136) at com.leapmotion.leap.LeapJNI.SwigDirector_Listener_onFrame(LeapJNI.java:495)
这是导致问题的代码片段:
if(areOverlapping(sauceRectangle, pizzaInside)) {
if(isHolding == null) {
Group g = (Group) scene.getRoot().getChildrenUnmodifiable().get(1);
g.getChildren().remove(sauceRectangle);
}
}
其中areOverlapping()只是一个检查某些逻辑的方法——问题不在这里
我的问题是:如果我有场景,如何从屏幕上删除矩形。还有,我在代码中做错了什么
# 1 楼答案
错误说明了这一点
您正在尝试执行一个应该在JavaFX应用程序线程上执行的操作,但您不在该线程上
为了在} 包围它们
JavaFX Application thread
上执行操作,用^{有关Modifying UI components in JavaFX的详细信息