在JavaFX中创建多个模态窗口时如何防止系统蜂鸣?

时间:2015-06-16 20:06:53

标签: javafx-8

我遇到了JavaFX 8u40的问题,在关闭子窗口时,创建两个或多个模态窗口会导致系统在Mac OS X上发出蜂鸣声。

下面的示例创建了一个模态窗口,该窗口是另一个模态窗口的子窗口。当我关闭子模态窗口时,我的系统会发出蜂鸣声(就像我试图点击父模态窗口时发出的蜂鸣声一样)。

问题似乎与stage.initOwner(...)有关,在showAndWait()循环完成之前,关闭子模态窗口会尝试访问父模态窗口。

我只想在提交错误报告之前确认我没有做错任何事。

public class TestDialog extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Button showModalButton = new Button("Show Modal Window");

        showModalButton.setOnAction(e -> {
            Stage modalStage = new Stage();

            modalStage.initModality(Modality.WINDOW_MODAL);
            modalStage.initOwner(stage);

            Button createInnerModalButton = new Button("Create inner modal window");

            createInnerModalButton.setOnAction(e2 -> {
                Stage innerModalStage = new Stage();

                innerModalStage.initModality(Modality.WINDOW_MODAL);
                innerModalStage.initOwner(modalStage);

                Button closeInnerModalButton = new Button("Close");

                closeInnerModalButton.setOnAction(e3 -> {
                    innerModalStage.close();
                });

                VBox innerVBox = new VBox(
                    new Label("This is an inner modal window"), closeInnerModalButton);

                innerModalStage.setScene(new Scene(innerVBox));

                innerModalStage.showAndWait();
            });

            VBox vBox = new VBox(
                new Label("This is a modal window"), createInnerModalButton);

            modalStage.setScene(new Scene(vBox));

            modalStage.showAndWait();
        });

        stage.setScene(new Scene(showModalButton));

        stage.show();
    }
}

0 个答案:

没有答案
相关问题