如何打开窗口必须先关闭然后再回到主窗口? [英] How to make the opened window must close at first before back to the main?

查看:227
本文介绍了如何打开窗口必须先关闭然后再回到主窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让第二个窗口必须先关闭,就像警告对话框一样。
当点击Button时,我应该向此代码添加什么:

I want to make that second window must close first like alert dialog. what should I add to this code when Button clicked:

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)允许这种情况发生。

There's a property called stage.initOwner(Stage stg) that allows this to happen.

示例:

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

Oracle Documentation on Modality: https://docs.oracle.com/javafx/2/api/javafx/stage/Modality.html

这篇关于如何打开窗口必须先关闭然后再回到主窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆