我尝试定期在JavaFX应用程序后台线程中运行,这会修改一些GUI属性.
我想我知道如何使用Task和Service类,javafx.concurrent并且无法弄清楚如何在不使用Thread#sleep()方法的情况下运行这样的周期性任务.如果我可以使用一些Executor来自Executors制造方法(Executors.newSingleThreadScheduledExecutor())会很好
我试图Runnable每5秒运行一次,重启javafx.concurrent.Service但它会立即挂起,service.restart甚至service.getState()被调用.
所以最后我使用Executors.newSingleThreadScheduledExecutor(),它Runnable每隔5秒发射一次并使用以下命令Runnable运行另一个Runnable:
Platform.runLater(new Runnable() {
//here i can modify GUI properties
}
Run Code Online (Sandbox Code Playgroud)
它看起来非常讨厌:(有没有更好的方法来使用Task或Service类?
JavaFX是否包含与java.awt.EventQueue等效的内容?对于我的应用程序,我需要拦截所有GUI相关的事件,如鼠标和键盘输入,到目前为止,我还没有找到一种方法来做到这一点,而无需将侦听器连接到每个GUI元素.