如何让打开的 window 必须先关闭才能回到主界面?
How to make the opened window must close at first before back to the main?
我想让第二个 window 必须像警报对话框一样先关闭。
单击 Button 时我应该向此代码添加什么:
Parent parent = FXMLLoader.load(getClass().getResource("view/sec_win.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
有一个名为 stage.initOwner(Stage stg) 的 属性 允许这种情况发生。
示例:
public class JavaFXApplication4 extends Application {
@Override
public void start(Stage stage) {
Button jb = new Button("Click");
jb.setOnMouseClicked(new EventHandler() {
@Override
public void handle(Event event) {
makeAnotherStage(stage);
}
});
GridPane gp = new GridPane();
gp.getChildren().add(jb);
Scene s = new Scene(gp);
stage.setScene(s);
stage.show();
}
private void makeAnotherStage(Stage st){
Stage s = new Stage();
GridPane gp = new GridPane();
Label l = new Label("Second Stage");
gp.getChildren().add(l);
Scene sc = new Scene(gp);
s.initOwner(st); <------- initOwner
s.initModality(Modality.WINDOW_MODAL); <------- Modality property
s.setScene(sc);
s.requestFocus();
s.show();
}
}
Oracle 模态文档:https://docs.oracle.com/javafx/2/api/javafx/stage/Modality.html
我想让第二个 window 必须像警报对话框一样先关闭。 单击 Button 时我应该向此代码添加什么:
Parent parent = FXMLLoader.load(getClass().getResource("view/sec_win.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
有一个名为 stage.initOwner(Stage stg) 的 属性 允许这种情况发生。
示例:
public class JavaFXApplication4 extends Application {
@Override
public void start(Stage stage) {
Button jb = new Button("Click");
jb.setOnMouseClicked(new EventHandler() {
@Override
public void handle(Event event) {
makeAnotherStage(stage);
}
});
GridPane gp = new GridPane();
gp.getChildren().add(jb);
Scene s = new Scene(gp);
stage.setScene(s);
stage.show();
}
private void makeAnotherStage(Stage st){
Stage s = new Stage();
GridPane gp = new GridPane();
Label l = new Label("Second Stage");
gp.getChildren().add(l);
Scene sc = new Scene(gp);
s.initOwner(st); <------- initOwner
s.initModality(Modality.WINDOW_MODAL); <------- Modality property
s.setScene(sc);
s.requestFocus();
s.show();
}
}
Oracle 模态文档:https://docs.oracle.com/javafx/2/api/javafx/stage/Modality.html