如何退出javafx中的时间轴?

时间:2014-05-02 09:05:09

标签: java javafx

我有这个代码

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);并解决了我的问题但更好的想法?

1 个答案:

答案 0 :(得分:3)

只做

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