0

我有这个代码,它显示退出应用程序的确认对话框。

    public class DialogPanels
{

    public void initClosemainAppDialog(final Stage primaryStage)
    {

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent event)
            {
                event.consume(); // Do nothing on close request

                // Dialog Stage init
                final Stage dialog = new Stage();
                dialog.initModality(Modality.APPLICATION_MODAL);

                // Frage - Label
                Label label = new Label("Exit from the program");


                // Button "Yes"
                Button okBtn = new Button("Yes");
                okBtn.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent event)
                    {
                        //primaryStage.close();  
                        //dialog.close();
                        //Platform.exit();     
                        System.exit(0);
                    }
                });

                // Button "No"
                Button cancelBtn = new Button("No");
                cancelBtn.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent event)
                    {
                        primaryStage.show();
                        dialog.close();
                    }
                });

                // Layout for the Button
                HBox hbox = new HBox();
                hbox.setSpacing(10);
                hbox.setAlignment(Pos.CENTER);
                hbox.getChildren().add(okBtn);
                hbox.getChildren().add(cancelBtn);

                // Layout for the Label and hBox
                VBox vbox = new VBox();
                vbox.setAlignment(Pos.CENTER);
                vbox.setSpacing(10);
                vbox.getChildren().add(label);
                vbox.getChildren().add(hbox);

                // Stage
                Scene scene = new Scene(vbox);
                dialog.setScene(scene);
                dialog.show();
            }
        });

    }
}

问题是,当关闭主应用程序时,会显示对话框并且隐藏主阶段。我想在主舞台前面显示对话框。你能帮我纠正这个吗?

更新

我测试了这段代码,它正在工作,但是当显示对话框时,主舞台不负责(冻结)。显示对话框时如何让主舞台负责?

4

2 回答 2

1

您需要在 primaryStage 和 dialog stage 之间设置适当的关系。这里有一个提示可以帮助您:

...
dialog.initOwner(primaryStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();

您可以在 Oracle 的JavaFX 2 JavaDocs中找到更多信息。

更多示例代码(编辑)

我正在使用 setOnHiding(..)而不是setOnCloseRequest(..)

stage.setOnHiding(new AskUserIfHeReallyWantsToQuitWindowHandler(stage));

我将您的代码提取到一个单独的事件处理程序类中并修复了我提到的问题(对不起,我现在时间有点短):

public class AskUserIfHeReallyWantsToQuitWindowHandler implements EventHandler<WindowEvent> {
    private final Stage primaryStage;

    public AskUserIfHeReallyWantsToQuitWindowHandler(final Stage primaryStage) {
                    Objects.requireNonNull(primaryStage);
        this.primaryStage = primaryStage;
    }

    @Override
    public void handle(final WindowEvent event) {

        event.consume();

        final Stage dialog = new Stage();

        final Button okBtn = new Button("Yes");
        okBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(final ActionEvent event) {
                dialog.close();
                primaryStage.close();
            }
        });

        // Button "No"
        final Button cancelBtn = new Button("No");
        cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(final ActionEvent event) {
                dialog.close();

                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        primaryStage.show();
                    }
                });
            }
        });

        // Layout for the Button
        final HBox hbox = new HBox();
        hbox.setSpacing(10);
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(okBtn);
        hbox.getChildren().add(cancelBtn);

        // Layout for the Label and hBox
        final VBox vbox = new VBox();
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(10);
        vbox.getChildren().add(new Label("Do your really want to exit?"));
        vbox.getChildren().add(hbox);

        // Stage
        final Scene scene = new Scene(vbox);
        dialog.setScene(scene);
        dialog.initOwner(primaryStage);
        dialog.initModality(Modality.NONE);

        dialog.showAndWait();
    }
}
于 2013-06-06T12:25:39.557 回答
1

如果您不想在窗口最小化时看到另一个窗口,请使用关闭事件并设置舞台的所有者:

@Override
public void handle(WindowEvent event)
{
    event.consume(); // Do nothing on close request

    // Dialog Stage init
    final Stage dialog = new Stage();
    dialog.initModality(Modality.APPLICATION_MODAL);
    dialog.initOwner(primaryStage);

    // other stuff
}
});
于 2013-06-06T12:29:18.980 回答