【问题标题】:JavaFX Popup not shown if owner window is not visible如果所有者窗口不可见,则不显示 JavaFX 弹出窗口
【发布时间】:2017-06-30 05:03:08
【问题描述】:

尝试将 Stage 设置为 Popup 的所有者,但如果所有者不是应用程序的主 Stage,则 Popup 不会出现。

public void popup(Window owner, String mensagem) {
    Popup popup = new Popup();
    popup.setAutoHide(true);
    popup.setHideOnEscape(true);

    Label label = new Label(mensagem);
    label.setBackground(new Background(new BackgroundFill(Color.CORNSILK, null, null)));

    popup.getContent().add(label);
    popup.setOnShown((event) -> {
        FadeTransition fade = new FadeTransition(Duration.seconds(5), label);
        fade.setOnFinished((e) -> {
            popup.hide();
        });
        fade.play();
    });

    popup.show(owner);
}

子阶段:

public class JanelaModal extends Stage {

    public JanelaModal(String title) {
        super(StageStyle.DECORATED);
        setTitle(title);
        initOwner(Main.getInstance().getStage());
        initModality(Modality.APPLICATION_MODAL);
        setResizable(false);
    }

    public void setGui(Parent gui) {
        if (getScene() != null) {
            getScene().setRoot(gui);
        } else {
            setScene(new Scene(gui));
        }
    }

}

【问题讨论】:

    标签: java user-interface javafx popup javafx-8


    【解决方案1】:

    在显示弹出窗口之前,您必须在子舞台上调用 show(...)

    使用您的课程JanelaModal 我编写了一个简单的测试Java FX 应用程序。这是主类

    public class Main extends Application {
    
        private static Main instance;
        private Stage stage;
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            instance = this;
            this.stage = stage;
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            primaryStage.setTitle("Hello World");
            primaryStage.setScene(new Scene(root, 300, 275));
            //primaryStage.show(); //Don't show main stage
    
            JanelaModal modal = new JanelaModal("TEST");
            modal.setWidth(300);
            modal.setHeight(300);
            modal.show();
            popup(modal, "Test message");
        }
    
        //Copied and pasted from the question post
        public void popup(Window owner, String mensagem) {
            Popup popup = new Popup();
            popup.setAutoHide(true);
            popup.setHideOnEscape(true);
    
            Label label = new Label(mensagem);
            label.setBackground(new Background(new BackgroundFill(Color.CORNSILK, null, null)));
    
            popup.getContent().add(label);
            popup.setOnShown((event) -> {
                FadeTransition fade = new FadeTransition(Duration.seconds(5), label);
                fade.setOnFinished((e) -> {
                    popup.hide();
                });
                fade.play();
            });
    
            popup.show(owner);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
        public static Main getInstance() {
            return instance;
        }
    
        public Stage getStage() {
            return stage;
        }
    }
    

    这就是结果

    弹出窗口显示在“子”阶段。如果你打电话

    popup(modal, "Test message");
    

    但你不打电话

    modal.show();
    

    弹出窗口(和舞台)不会显示。

    希望我能帮上忙 :)

    【讨论】:

    • 感谢您的回答。显示弹出窗口时,所有者窗口似乎必须可见。在我的情况下,子窗口正在关闭,所以主窗口必须是弹出窗口的所有者。我想知道为什么文档没有提到这一点,它并不那么明显。
    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多