如何在一定时间后关闭舞台 JavaFX [英] How to close a stage after a certain amount of time JavaFX

查看:33
本文介绍了如何在一定时间后关闭舞台 JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用两个控制器类.

I'm currently working with two controller classes.

在 Controller1 中,它创建了一个在主舞台之上打开的新舞台.

In Controller1 it creates a new stage that opens on top of the main one.

Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Controller2.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

现在一旦该阶段打开,我希望它在关闭之前保持打开状态大约 5 秒钟.

Now once that stage is open, I want it to stay open for about 5 seconds before closing itself.

在 Controller2 中,我尝试实现类似的东西

Within Controller2, I've tried implementing something like

long mTime = System.currentTimeMillis();
long end = mTime + 5000; // 5 seconds 

while (System.currentTimeMillis() > end) 
{
      //close this stage  
} 

但我不知道在while循环中放什么来关闭它.我尝试了各种方法,但没有任何效果.

but I have no idea what to put inside the while loop to close it. I've tried all sorts and nothing works.

推荐答案

使用 PauseTransition:

PauseTransition delay = new PauseTransition(Duration.seconds(5));
delay.setOnFinished( event -> stage.close() );
delay.play();

这篇关于如何在一定时间后关闭舞台 JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆