打开附加到当前阶段的新FXML - JavaFX [英] Opening a new FXML attached to the current stage - JavaFX

查看:158
本文介绍了打开附加到当前阶段的新FXML - JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 SO问题文件选择器打开并附加到主要阶段。

This is an extension of an SO question in which a file chooser opens and is attached to the primary stage.

如何打开 FXML 附加到主要阶段的阶段?

How would one go about opening an FXML stage attached to the the primary stage?

下面的代码加载 FXML 并替换primaryStage(当前阶段) )。如何将其作为附加窗口加载?

The code below loads the FXML and replaces the primaryStage (current stage). How can I load it as an attached window instead?

            Stage stage = (Stage)((Node) event.getTarget()).getScene().getWindow();
            Parent parent = null;
            try {
                parent = FXMLLoader.load(getClass().getResource("/gui/GUpdater-progress.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene scene = new Scene(parent,600,400);
            stage.setResizable(false);
            stage.setTitle("GUpdater");
            stage.setScene(scene);
            stage.show();


推荐答案

鉴于您之前提出的问题,我想通过附加 你的意思是文件选择器所做的事情(当有父母时,文件选择器会跟随它)。

Given your previous question, I imagine that by "attach" you mean something like what the File Chooser does (when there is a parent, the File Chooser will follow it around).

你需要的第一件事是主要的窗口,你可以通过

The first thing you need is the "main window", which you can get via

       Window ownerWindow = ((Node) event.getTarget()).getScene().getWindow();

接下来就是实际装载你的新舞台。

The next thing is to actually load your new stage.

       Stage stage = new Stage();
       stage.initModality(Modality.APPLICATION_MODAL);
       stage.initOwner(ownerWindow);
       Parent root = FXMLLoader.load(getClass().getResource("/gui/GUpdater-progress.fxml"));
       Scene scene = new Scene(root, 600, 400);
       stage.setTitle("GUpdater");
       stage.setScene(scene);
       stage.show();

关键是 stage.initOwner(ownerWindow)部分。您正在创建的这个新阶段由原始窗口拥有,即 ownerWindow

The key is the stage.initOwner(ownerWindow) part. This new stage you are creating is "owned by" the original window, which is ownerWindow.

此外,如果你想获得更像File Chooser的感觉,你应该使用

In addition, if you want to achieve a feel more like the File Chooser's, you should use

       stage.initStyle(StageStyle.UNDECORATED);

之前调用setScene()。这将删除边框。

这篇关于打开附加到当前阶段的新FXML - JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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