使用 JavaFX,点击一个按钮我想这样做:
spinBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
field.setDisable(false);
//Delay for 10 seconds
field.setDisable(true);
}
});
我很快意识到 hibernate 是行不通的,因为它会完全卡住 GUI。我也试过 hibernate 线程来获得一个计时器,但如果在我想要延迟的地方输入,它仍然会卡住 GUI。 (下面的例子)
spinBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
ExampleTimerThread exampleSleepyThread = new ExampleTimerThread();//this extends Thread
exampleSleepyThread.start();
//thread sleeps for 10 secs & sets public static boolean finished = true; after sleep
while(finished == true){
field.setDisable(false);
}
}
});
我该怎么做才能防止此代码卡住 GUI? 我知道在 Swing 中,有一个计时器。 JavaFX 中有类似的东西吗?
最佳答案
使用PauseTransition
来延迟事件:
spinBtn.setOnAction(e -> {
field.setDisabled(false);
PauseTransition pt = new PauseTransition(Duration.seconds(10));
pt.setOnFinished(ev -> {
field.setDisabled(true);
});
pt.play();
});
关于java - 单击按钮后,如何使文本字段可编辑 10 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629244/