在用户从javafx的对话框中选择一个选项之前,如何防止primaryStage关闭?

问题描述

现在,当用户单击关闭窗口按钮时,窗口关闭,然后对话框才出现。 我希望对话框首先出现,并且如果用户选择是或否,则只有这样,窗口才会关闭

这是我的代码

@Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/LoginScreen.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
            primaryStage.setResizable(true);
        } catch(Exception e) {
            e.printstacktrace();
        }
        
        primaryStage.setonHiding(new EventHandler<WindowEvent>() {
             @Override
             public void handle(WindowEvent event) {
                 Platform.runLater(new Runnable() {
                     @Override
                     public void run() {
                         Alert alert = new Alert(AlertType.CONFIRMATION);
                         alert.setTitle("Exist Confirmation Dialog");
                         alert.setHeaderText("Do you want to save your changes?");
                         alert.setContentText("Choose your option.");

                         ButtonType yesButton = new ButtonType("Yes");
                         ButtonType noButton = new ButtonType("No");
                         ButtonType cancelButton = new ButtonType("Cancel",ButtonData.CANCEL_CLOSE);

                         alert.getButtonTypes().setAll(yesButton,noButton,cancelButton);

                         Optional<ButtonType> result = alert.showAndWait();
                         if (result.get() == yesButton){
                            //Serialize();
                             System.exit(0); 
                         } else if (result.get() == noButton) {
                             System.exit(0);
                         } else {
                             // ... user chose CANCEL or closed the dialog
                         }
                         
                     }
                 });
             }
         });

解决方法

您必须捕获窗口关闭事件。

 stage.setOnCloseRequest(event -> {
     //Code that will be called if the user tries to close the window
     //Close the window with a cross,as the user would do. Do not close via IDEA.
 });

如果您不想关闭窗口,请吸收事件。

stage.setOnCloseRequest(event -> {
     event.consume();
});

请注意。我不需要调用System.exit(0);如果我不使用event.consumer();窗口将自行关闭。

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Pane pane = new Pane();
        pane.setPrefSize(400,400);
        stage.setScene(new Scene(pane));

        //window close request
        stage.setOnCloseRequest(event -> {
            Alert alert = getAlert();
            Optional<ButtonType> result = alert.showAndWait();
            if (result.get().getText().equalsIgnoreCase("Yes")){
                //Save the changes
            } else if (result.get().getText().equalsIgnoreCase("No")) {
                //Save the changes
            }else if(result.get().getText().equalsIgnoreCase("Cancel")){
                //Absorb the event,the window will not close
                event.consume();
            }
        });

        stage.show();
    }

    private Alert getAlert(){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Exist Confirmation Dialog");
        alert.setHeaderText("Do you want to save your changes?");
        alert.setContentText("Choose your option.");

        ButtonType yesButton = new ButtonType("Yes");
        ButtonType noButton = new ButtonType("No");
        ButtonType cancelButton = new ButtonType("Cancel",ButtonBar.ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(yesButton,noButton,cancelButton);

        return alert;
    }

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