【问题标题】:JavaFX stage issue with hide and show隐藏和显示的 JavaFX 阶段问题
【发布时间】: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() 命令时,我无法让框架再次出现......

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

问候。

【问题讨论】:

  • 您可以使用 ThreadPauseTransition 。推荐第二个。
  • 为什么要实例化Application 子类?假设您已经有另一个 Application 子类,因为您的 JavaFX 应用程序已经在运行。在 JavaFX 程序中您应该只有一个 Application 实例,并且它应该是在您启动 FX 应用程序时为您创建的实例。
  • 尚不清楚为什么classStage.hide() 不起作用:这与您发布的代码无关。创建一个重现问题的minimal reproducible exampleedit 你的问题以包含它。您应该能够用您所描述的功能(两个按钮等)编写一个完整的示例,只需几十行代码,这完全在此处的问题可接受的范围内。
  • 好的,我会做的,请稍等。 @James_D

标签: java javafx javafx-8 fxml


【解决方案1】:

目前还不清楚您问题中的代码中发生了什么。底线是你永远不应该自己创建Application 的实例;唯一的 Application 实例应该是为您创建的实例。

我实际上并不认为需要为您展示的功能设置一个单独的类(尽管您当然可以)。如果按下 no 按钮,您需要做的就是隐藏 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();
}

请注意,默认情况下,如果最后一个窗口关闭,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();
}); 

【讨论】:

  • 实际上最大的问题之一是classStage.hide方法不起作用,舞台永远不会隐藏。
  • @ChristopherAngel 那么您的代码中还有其他问题,基本上与您发布的所有内容分开。
猜你喜欢
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
相关资源
最近更新 更多