定时器和 javafx [英] Timers and javafx
问题描述
我正在尝试编写一个代码,使用 javafx 使内容以预定但不规则的间隔出现在屏幕上.我尝试使用计时器(java.util,而不是
I am trying to write a code that will make things appear on the screen at predetermined but irregular intervals using javafx. I tried to use a timer (java.util, not javax.swing) but it turns out you can't change anything in the application if you are working from a separate thread.(Like a Timer) Can anyone tell me how I could get a Timer to interact with the application if they are both separate threads?
推荐答案
如果您接触任何 JavaFX 组件,您必须从平台线程(本质上是 JavaFX 的事件分派线程)执行此操作.您可以通过调用 <代码>Platform.runLater().因此,例如,这样做是完全安全的:
If you touch any JavaFX component you must do so from the Platform thread (which is essentially the event dispatch thread for JavaFX.) You do this easily by calling Platform.runLater()
. So, for instance, it's perfectly safe to do this:
new Thread() {
public void run() {
//Do some stuff in another thread
Platform.runLater(new Runnable() {
public void run() {
label.update();
javafxcomponent.doSomething();
}
});
}
}.start();
这篇关于定时器和 javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!