隐藏和显示的 JavaFX 阶段问题

JavaFX stage issue with hide and show

我正在构建一个显示 window 的应用程序,该应用程序询问用户是否要使用两个按钮选项暂停计算机,其中一个是“是”,PC 将暂停。

另一个名为 "Later" 的按钮本应隐藏 window,一个小时后它再次出现并询问相同的问题。

代码 "later buttton"

 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 的代码class

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(); Stage 没有任何反应,window 从未关闭,最糟糕的是,当我使用 Platform.exit() 命令时,我无法让框架再次出现...

有谁知道在特定时间后更容易隐藏和显示 window 的方法吗?也许我做错了。

问候。

不太清楚你问题中的代码是怎么回事。最重要的是,您永远不应该自己创建 Application 的实例;唯一的 Application 个实例应该是为您创建的实例。

我实际上认为没有必要为您展示的功能单独设置 class(当然您可以)。如果按下否按钮,您需要做的就是隐藏classStage,并在一个小时后再次打开它:

noButton.setOnAction(event -> {


    Done=false; //boolean to close and open the frame

    classStage.hide();
    PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
    oneHourPause.setOnFinished(e -> showUI(classStage));
    oneHourPause.play();
}); 

// ...

private void showUI(Stage stage) {

    Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
    stage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
    stage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
    stage.setAlwaysOnTop(true);
    Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
    stage.setTitle("Alerta suspencion de equipo");
    stage.setScene(new Scene(root));
    stage.setResizable(false);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.show();
}

请注意,默认情况下,如果关闭最后一个 window,FX 应用程序将退出。因此,您应该在 init()start() 方法中调用 Platform.setImplicitExit(false);

我假设您正在重新加载 FXML 文件,因为 UI 可能在之前加载后发生了变化。显然,如果不是这样,您只需按原样重新显示舞台即可:

noButton.setOnAction(event -> {


    Done=false; //boolean to close and open the frame

    classStage.hide();
    PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
    oneHourPause.setOnFinished(e -> classStage.show());
    oneHourPause.play();
});