JavaFX:如何在特定时间内禁用按钮?

6

我希望在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();
4个回答

6
您可以采用简单的方法,使用一个线程提供相关的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();

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

5

您也可以使用时间轴Timeline

  final Button myButton = new Button("Wait for " + delayTime + " seconds.");
  myButton.setDisable(true);

  final Timeline animation = new Timeline(
            new KeyFrame(Duration.seconds(delayTime),
            new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent actionEvent) {
                    myButton.setDisable(false);
                }
            }));
  animation.setCycleCount(1);
  animation.play();

嗨,Uluk,我的代码不起作用。请查看更新的问题,我所做的更改并没有起作用。 - Rohan
它是否抛出异常?尝试交换 nextButton.setDisable(false) 和 nextButton.setDisable(true) 这两行代码。 - Uluk Biy
没有抛出任何异常。按照您的建议更改代码后,按钮没有被禁用。 - Rohan

2

禁用JavaFX控件的方法是:

myButton.setDisable(true);

你可以以任何你想要的方式在程序中实现时间逻辑,可以通过轮询计时器或者响应某些事件来调用该方法。
如果你是通过FXML在SceneBuilder中创建了该按钮实例,那么你应该为该按钮分配一个fx:id,这样在加载场景图时,它的引用就会自动注入到你的控制器对象中。这将使你在控制器代码中更容易地处理它。
如果你是通过编程方式创建了该按钮,则你已经在代码中拥有了它的引用。

我已经使用了myButton.setDisable(true);它是有效的,但是如何应用时间逻辑呢?我尝试了Thread.sleep,但整个应用程序在特定的时间内都会挂起。您能提供一个小示例吗?不久我将在问题中更新代码。 - Rohan

0

或者您可以使用一个服务,并将其运行属性绑定到您想要禁用的按钮的disableProperty。

public void start(Stage stage) throws Exception {
    VBox vbox = new VBox(10.0);
    vbox.setAlignment(Pos.CENTER);      

    final Button button = new Button("Your Button Name");
    button.setOnAction(new EventHandler<ActionEvent>() {            
        @Override
        public void handle(ActionEvent event) {
            Service<Void> service = new Service<Void>() {                   
                @Override
                protected Task<Void> createTask() {
                    return new Task<Void>() {
                        @Override
                        protected Void call() throws Exception {
                            Thread.sleep(5000);//Waiting time
                            return null;
                        }
                    };
                }
            };
            button.disableProperty().bind(service.runningProperty());               
            service.start();
        }
    });     

    vbox.getChildren().addAll(button);
    Scene scene = new Scene(vbox, 300, 300);
    stage.setScene(scene);
    stage.show();
}

但是由Uluk Biy提供的时间轴解决方案看起来更加优雅。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接