【问题标题】:How to create a dialog window after countdown/timer in java?java - 如何在java中的倒计时/计时器之后创建一个对话框窗口?
【发布时间】:2018-12-29 20:35:29
【问题描述】:

我希望每隔“n”秒打开一个对话窗口。 我尝试使用“计时器”。我有以下错误:

"Exception in threadTimer-0" java.lang.IllegalStateException: Not on FX application thread; 
currentThread = Timer-0"

由此我了解到,我无法在不是 javaFX 线程的线程上创建其他窗口。

private Integer animationTime;
private void routine(Integer time) throws Exception{
    animationTime = time;
    Timer timer = new Timer();
    timeline = new Timeline (new KeyFrame (Duration.seconds(1), evt -> 
    updateAnimation(time)) );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();

    timer.schedule(new TimerTask() {// repeat over and over 
        @Override
        public void run() {
            alert= new Alert(Alert.AlertType.WARNING);
            alert.setTitle("Alert title");
            alert.setHeaderText("Alert...");
            alert.setContentText("....");
            alert.show();
            Optional<ButtonType> result = alert.showAndWait();
            if(result.get() == ButtonType.OK ){
                try {
                    routine(time);
                }
                catch (Exception e){}
            }
        }
    }, time*1000, time*1000);
}

private void updateAnimation(Integer time){
    if(animationTime.equals(0)){
         timeline.stop();
    }
    textTime.setText("Minutes: " + animationTime.toString());
    animationTime -= 1;
}

我该如何解决?

2018 年 12 月 30 日更新

有一个新错误

timeline.setOnFinished((e)->{

         Alert alert= new Alert(Alert.AlertType.WARNING);
         alert.setTitle("Alert title");
         alert.setHeaderText("Alert...");
         alert.setContentText("....");
         alert.show();
         Optional<ButtonType> result = alert.showAndWait();
         if(result.get() == ButtonType.OK ){
                try {
                    routine(time);
                }
                catch (Exception ex){}
          }
}
     Optional<ButtonType> result = alert.showAndWait();

线程“JavaFX 应用程序线程”中的异常 java.lang.IllegalStateException: showAndWait 期间不允许 动画或布局处理

【问题讨论】:

    标签: java javafx timer countdown


    【解决方案1】:

    无需使用Timer。使用Timeline#setOnFinished方法:

    timeline.setOnFinished((e)->{
    
             Alert alert= new Alert(Alert.AlertType.WARNING);
             alert.setTitle("Alert title");
             alert.setHeaderText("Alert...");
             alert.setContentText("....");
             alert.show();
             Optional<ButtonType> result = alert.showAndWait();
             if(result.get() == ButtonType.OK ){
                    try {
                        routine(time);
                    }
                    catch (Exception ex){}
              }
     });
    

    【讨论】:

    • 我解决了部分问题,现在出现了一个新的错误... Optional result = alert.showAndWait(); // 线程“JavaFX 应用程序线程”中的行错误异常 java.lang.IllegalStateException: 在动画或布局处理期间不允许 showAndWait
    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2015-04-04
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多