定时器和 javafx [英] Timers and javafx

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

问题描述

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

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屋!

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