gpt4 book ai didi

java - 如何退出 javafx 中的时间线?

转载 作者:行者123 更新时间:2023-11-29 05:24:02 24 4
gpt4 key购买 nike

我有这个代码

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

关于java - 如何退出 javafx 中的时间线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com