在 JavaFX 上单击两次警报确认对话框

Two clicks for alert confirmation dialog on JavaFX

我在 javafx 应用程序上有一个对话框警报。当我单击任何按钮(是或否)时,我必须单击两次才能执行每个按钮的操作。我只想点击一下!有人可以帮助我吗?

创建提醒的方法:

public class AlertMessages {
    public static boolean alertConfirmation(String header, String text) {
        Alert dialog = new Alert(Alert.AlertType.CONFIRMATION);
        dialog.setHeaderText(header);
        dialog.setContentText(text);
        dialog.setResizable(true);
        dialog.getDialogPane().setPrefSize(350, 200);
        dialog.showAndWait();
        final Optional<ButtonType> result = dialog.showAndWait();
        return result.get() == ButtonType.OK;
    }
}

调用方法:

public class OverviewController {
     ...
     ...

     @FXML
     private void handleButton() {
          ...
          else if (...) {
               header = "...";
               text = "...";
               boolean res = AlertMessages.alertConfirmation(header, text);
               if (res) {
                   ...
               } else {
                   ...
               }
          ...
     }

     ...
     ...
}

您正在呼叫 dialog.showAndWait() 两次:

dialog.showAndWait();
final Optional<ButtonType> result = dialog.showAndWait();

只删除第一个调用。