3
我創建了一個計時器,它在GUI的頂部更新文本圖形。當計時器耗盡時,它會更改一個變量(鏈接到監聽器),並在GUI中添加一個新的圖形「場景/組/節點」。
- 改變我的變量//鏈接到一個ChangeListener - 和添加場景到我的GUI //不是一個聽衆,但增加了新的元素,我的GUI
這些新增的兩個導致我的程序與崩潰這個錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:235)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393)
示例錯誤代碼:
int delay = new Integer(1000); //milliseconds
time_left=time_limit;
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent arg0) {
// TODO Auto-generated method stub
timer.setText(time_left+" SECONDS LEFT");
time_left=time_left-1;
if (time_left<0)
{
//time_left=time_limit;
mytimer.stop();
//mytimer.start();
root.getChildren().get(2).setOpacity(.2);
//root.getChildren().add(3,newScene()); //This causes to crash //Adds a new Scene to my GUI
current_player.setValue(false); //Also causes to crash - current_player has a Listener on it
//System.out.println("TIMER RAN OUT!");
}
}
};
mytimer=new Timer(delay, taskPerformer);
mytimer.start();
我current_player聽衆:
current_player.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//Code in Here
}
{);