我目前正在使用两个 Controller 类。
在 Controller1 中,它创建了一个在主阶段之上打开的新阶段。
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Controller2.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
现在,一旦舞台打开,我希望它在关闭前保持打开状态约 5 秒。
在 Controller2 中,我尝试实现类似的东西
long mTime = System.currentTimeMillis();
long end = mTime + 5000; // 5 seconds
while (System.currentTimeMillis() > end)
{
//close this stage
}
但我不知道在 while 循环中放入什么来关闭它。我已经尝试了各种方法,但没有任何效果。
最佳答案
使用 PauseTransition
:
PauseTransition delay = new PauseTransition(Duration.seconds(5));
delay.setOnFinished( event -> stage.close() );
delay.play();
关于java - 如何在一定时间后关闭阶段 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27334455/