我尝试定期在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类?
我正在尝试在应用程序GUI中模拟基本恒温器.
我想用新的温度值每2秒更新一个标签盒值.
例如,我的初始温度将显示为68度并更新为69,至70等,直到每2秒75次.
这是我用Java fx编写的一段代码. controlpanel是标签盒所在的te形式的对象.它仅将最终值更新为75.它不会每2秒更新一次.我写了一个方法暂停,导致2秒的延迟.所有标签都会更新其最终值,但不会每2秒更新一次.当我调试时,我可以看到值每2秒增加一个.此代码是在按钮onClick事件中编写的
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
Timer asd = new Timer(1000,null);
asd.setDelay(1000);
while(i < 10)
{
jTextField1.setText(Integer.toString(i));
i++;
asd.start();
}
}
Run Code Online (Sandbox Code Playgroud)