带有Label.setText的JavaFx2 IllegalStateException

为什么我使用这样的简单线程:

Thread t = new Thread(new Runnable() { public void run(){ while(true){ ..... idLabel.setText(Date.toString); Thread.sleep(1000);` } t.start(); 

我有这个错误:

java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-4

但如果我使用输入文本(如idInputText)而不是标签我没有错误?

JavaFX的所有UI操作都应该在FX应用程序线程上执行。 您正在创建一个新的Thread t ,它不是FX应用程序线程。 因此exception消息:

java.lang.IllegalStateException: 不在FX应用程序线程上; currentThread = Thread-4

您需要使用Platform#runLater()方法进行此类操作,如下所示:

 while(true){ ..... Platform.runLater(new Runnable() { @Override public void run() { idLabel.setText(Date.toString); } }); Thread.sleep(1000);` }