【问题标题】:How to make the opened window must close at first before back to the main?如何使打开的窗口必须先关闭才能返回主窗口?
【发布时间】:2015-12-13 02:19:24
【问题描述】:

我想让第二个窗口必须像警报对话框一样首先关闭。 单击按钮时我应该在此代码中添加什么:

Parent parent = FXMLLoader.load(getClass().getResource("view/sec_win.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();

【问题讨论】:

    标签: java javafx fxml scenebuilder


    【解决方案1】:

    有一个名为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 2018-12-25
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多