我有这个代码

private static int seconds = 0 ;

        public void start(Stage stage) throws IOException{
       //some code
      //then add a timer because i want to change this stage's scene after 7 seconds
       final Timeline timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler() {
            @Override public void handle(Event event) {

               seconds++
               if(seconds>=7){

                   AnotherScene mainmenu = new AnotherScene ();
                   try {
                       AnotherScene .startScene(stage);
//i have startScene methode in AnotherScene  classthat give this stage and load another fxml file
                   } catch (IOException ex) {
                       Logger.getLogger(startUpScene.class.getName()).log(Level.SEVERE, null, ex);
                   }
               }
            }
             }),
            new KeyFrame(Duration.seconds(1.0)));
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();//start timer
//some code
stage.show();
}


我可以更改舞台的场景,但是我有一个问题,我看到此计时器适用于我的AnotherScene,因为我在AnotherScene中添加了一个静态int'a'并在AnotherSceneController中打印了'a',并且每秒钟看到此整数打印出任何退出的想法计时器?谢谢

编辑:

我将timeline.setCycleCount(Timeline.INDEFINITE);更改为timeline.setCycleCount(8);,问题解决了,但是有更好的主意吗?

最佳答案

做就是了

PauseTransition delay = new PauseTransition(Duration.seconds(7));
delay.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        // do whatever you needed to do after the seven second pause
    }
});
delay.play();

09-25 22:13