为什么当我使用这样的简单线程时:
Thread t = new Thread(new Runnable() {
public void run(){
while(true){
.....
idLabel.setText(Date.toString);
Thread.sleep(1000);`
}
t.start();
我收到此错误:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
但是如果我使用输入文本(如 idInputText)而不是标签,我就不会出现错误?
最佳答案
JavaFX 的所有 UI 操作都应在 FX 应用程序线程上执行。您正在创建一个新的Thread t
,它不是 FX 应用程序线程。因此出现异常消息:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
您需要使用Platform#runLater()此类操作的方法如下:
while(true){
.....
Platform.runLater(new Runnable() {
@Override
public void run() {
idLabel.setText(Date.toString);
}
});
Thread.sleep(1000);`
}
关于JavaFx2 IllegalStateException 与 Label.setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124442/