JavaFX:如何在特定时间内禁用按钮? [英] JavaFX: How to disable a button for a specific amount of time?

查看:61
本文介绍了JavaFX:如何在特定时间内禁用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaFX应用程序中禁用按钮特定时间.有什么选择吗?如果没有,是否有任何解决方法?

I want to disable a button for a specific time in JavaFX application. Is there any option to do this? If not, is there any work around for this?

下面是我在应用程序中的代码.我尝试了Thread.sleep,但是我知道这不是阻止用户单击下一步按钮的好方法.

Below is my code in application. I tried Thread.sleep, but i know this is not the good way to stop the user from clicking on next button.

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()来实现):

You could use the simple approach of a thread that provides the relevant GUI calls (through runLater() of course):

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();

这也许不是实现它的最简洁的方法,但是可以安全地工作.

It's perhaps not the neatest way of achieving it, but works safely.

这篇关于JavaFX:如何在特定时间内禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆