我不知道如何在java fxml中关闭主窗口。
这部分代码位于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();
}
我有这个。在 Controller 中我这样做了:
Main m = new Main();
m.show();`
但我仍然不知道如何关闭primaryStage
。
请帮助我或告诉我如何创建新窗口并关闭旧窗口。我认为这就是我想做的 - 它不正确,但我自己想出了它。
最佳答案
我通过使用您想要关闭的舞台中的对象来获取窗口
Window currentStage = OBJECTINSCENE.getScene().getWindow();
(将“OBJECTINSCENE”替换为场景中任何内容的 ID)。这为您提供了已打开阶段的引用。然后调用
currentStage.hide();
在需要时关闭舞台。
所以你的显示函数如下
public void show(){
Window currentStage = OBJECTINSCENE.getScene().getWindow();
secondaryStage.show();
currentStage.hide();
}
关于java - 我不知道如何在java fxml中关闭主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60799669/