我想让第二个窗口必须像警报对话框一样首先关闭。 单击按钮时我应该向此代码添加什么:
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
关于java - 如何使打开的窗口必须先关闭才能返回主窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247327/