有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我不知道如何关闭javafxml中的主窗口

我不知道如何在JavaFXML中关闭主窗口

这部分代码位于Main类中:

public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    Parent root2 = FXMLLoader.load(getClass().getResource("2ndwin.fxml"));
    Scene scene  = new Scene(root);
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(scene);
    primaryStage.show();

    Scene scene2 = new Scene(root2);
    secondaryStage.setScene(scene2);
}

public void show(){
    secondaryStage.show();
}

我有这个。在控制器中,我这样做:

Main m = new Main();
m.show();`

但我仍然不知道如何关闭primaryStage

请帮助我或告诉我如何创建新窗口并关闭旧窗口。我想这就是我想做的——这是不正确的,但我自己想出来的


共 (1) 个答案

  1. # 1 楼答案

    我通过在你想要关闭的阶段中使用一个对象来获得窗口

        Window currentStage = OBJECTINSCENE.getScene().getWindow();
    

    (将“OBJECTINSCENE”替换为场景中任何对象的id)。这将为您提供已打开阶段的参考。然后打电话

    currentStage.hide();
    

    在需要时关闭舞台

    所以你的表演功能如下

    public void show(){
        Window currentStage = OBJECTINSCENE.getScene().getWindow();
        secondaryStage.show();
        currentStage.hide();
    }