【发布时间】:2017-06-04 13:48:39
【问题描述】:
我正在构建一个应用程序,它显示一个窗口,询问用户是否要通过两个按钮选项暂停计算机,其中一个是“是”,PC 暂停。
另一个名为“稍后”的按钮应该隐藏窗口,一个小时后它再次出现并提出相同的问题。
“稍后按钮”的代码
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
Gui gui = new Gui();
try {
gui.start(classStage);
} catch (Exception e) {
e.printStackTrace();
}
});
您在代码中看到的布尔值是 bc,这是我认为我可以控制的方式,相信我,我尝试了不同的方式,但没有人只是帮助我解决这个问题,这是 GUI 类的代码
public class Gui extends Application {
public Stage classStage = new Stage();
public static boolean Done=true;
public static boolean flag=true;
public Gui() {
}
@Override
public void start(Stage primaryStage) throws Exception {
Done = Controller.isDone();
classStage = primaryStage;
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
primaryStage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
primaryStage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
primaryStage.setAlwaysOnTop(true);
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
primaryStage.setTitle("Alerta suspencion de equipo");
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UNDECORATED);
if (Controller.isDone() == true) {
primaryStage.show();
} else if(Controller.isDone() == false) {
primaryStage.hide();
Platform.exit(); // this is the only way that the windows close
}
}
我知道 Platform.exit();杀死程序但是当我只使用 .hide();舞台没有任何反应,窗口永远不会关闭,最糟糕的是,当我使用 Platform.exit() 命令时,我无法让框架再次出现......
有人知道在特定时间后更容易隐藏和显示窗口的方法吗?也许我做错了。
问候。
【问题讨论】:
-
您可以使用
Thread或PauseTransition。推荐第二个。 -
为什么要实例化
Application子类?假设您已经有另一个Application子类,因为您的 JavaFX 应用程序已经在运行。在 JavaFX 程序中您应该只有一个Application实例,并且它应该是在您启动 FX 应用程序时为您创建的实例。 -
尚不清楚为什么
classStage.hide()不起作用:这与您发布的代码无关。创建一个重现问题的minimal reproducible example 和edit 你的问题以包含它。您应该能够用您所描述的功能(两个按钮等)编写一个完整的示例,只需几十行代码,这完全在此处的问题可接受的范围内。 -
好的,我会做的,请稍等。 @James_D