我发现了很多建议使用Timeline与KeyFrame.onFinished在计划任务的JavaFX。但是AnimationTimer文档说,它的handle方法每秒被调用 60 次。
从文档中不清楚,但似乎在内部Timeline使用AnimationTimer。这是否意味着调度的时间线解决方案强加了 CPU 密集型轮询模式?如果这实际上是 JavaFX 的工作方式,那么推荐其他哪些调度方法?
有没有办法继续监听属性更改几秒钟,然后触发事件(调用方法)?
例如,当用户在文本字段中输入数据时:
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
//before calling a method to do something.. wait for a few seconds ...
}
});
Run Code Online (Sandbox Code Playgroud)
一个场景是根据字符串值触发一个动作。例如,点击“M”进行移动,或点击“MA”进行掩码。在采取行动之前,我想“继续聆听”2 秒钟。
Button button = new Button("Show Text");
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
Platform.runLater(new Runnable(){
@Override
public void run() {
field.setText("START");
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Platform.runLater(new Runnable(){
@Override
public void run() {
field.setText("END");
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
运行上面的代码后,field.setText("START")没有执行,我的意思是textfield没有将其文本设置为"START",为什么?怎么解决这个?