I have Label label
在我的 FXML 应用程序中。
我希望这个标签每秒改变一次。目前我使用这个:
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
int i = 0;
while (true) {
lbl_tokenValid.setText(""+i);
i++;
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
然而什么也没有发生。
我没有收到任何错误或异常。
我不需要在主 GUI 线程中将标签更改为的值,因此我看不到其中的意义updateMessage
or updateProgress
方法。
怎么了?
您需要在 JavaFX UI 线程上对场景图进行更改。
像这样:
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
int i = 0;
while (true) {
final int finalI = i;
Platform.runLater(new Runnable() {
@Override
public void run() {
label.setText("" + finalI);
}
});
i++;
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)