计时器和javafx [英] Timers and javafx

查看:166
本文介绍了计时器和javafx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码,使用javafx以预定但不规则的间隔在屏幕上显示内容。我尝试使用计时器( java.util ,而不是 javax.swing )但事实证明你无法改变任何东西应用程序,如果你是从一个单独的线程工作。(像一个计时器)任何人都可以告诉我如何让一个Timer与应用程序进行交互,如果它们都是单独的线程?

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组件,则必须从Platform线程(实际上是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆