如何在javafx中退出时间线?
我有这个密码如何在javafx中退出时间线?,java,javafx,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(
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();
}
我可以改变我舞台的场景,但我有一个问题,我看到这个计时器适用于我的另一个场景,因为我在另一个场景中添加了一个静态int'a',在另一个场景控制器中打印'a',看到每秒钟打印一次这个整数,有什么想法可以退出计时器吗?谢谢
编辑:
我更改timeline.setCycleCount(timeline.unfinite)代码>到时间线。设置循环计数(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();
PauseTransition delay=新的PauseTransition(持续时间.秒(7));
delay.setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
//在七秒钟的停顿后,做任何你需要做的事情
}
});
延迟。播放();
我没有JavaFX,所以我无法检查它,但您能否从事件中提取时间线对象,然后调用l方法timeline.stop()
?