我想在JavaFX应用程序中禁用按钮特定时间。有什么选择吗?如果没有,是否有任何解决方法?
下面是我在应用程序中的代码。我尝试了Thread.sleep
,但是我知道这不是阻止用户单击下一步按钮的好方法。
nextButton.setDisable(true);
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(delayTime),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
nextButton.setDisable(false);
}
}));
animation.setCycleCount(1);
animation.play();
最佳答案
您可以使用提供相关GUI调用的线程的简单方法(当然通过runLater()
):
new Thread() {
public void run() {
Platform.runLater(new Runnable() {
public void run() {
myButton.setDisable(true);
}
}
try {
Thread.sleep(5000); //5 seconds, obviously replace with your chosen time
}
catch(InterruptedException ex) {
}
Platform.runLater(new Runnable() {
public void run() {
myButton.setDisable(false);
}
}
}
}.start();
这也许不是实现它的最简洁的方法,但是可以安全地工作。
关于javafx-2 - JavaFX : How to disable a button for a specific amount of time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919727/