JavaFX中的警告框在按下"x"按钮时不会关闭。

4

嗨,针对另一个JavaFX应用程序,我一直在测试警报功能,但唯一无法正常工作的是按下警报框上的“X”按钮。

我在下面添加了代码,但如果您没有时间在此运行它,这里有一个GIF来解释我的警报框问题: https://giant.gfycat.com/GeneralUntimelyBluewhale.webm

很抱歉我不太确定如何将gif上传到实际帖子中。

有没有办法解决这个问题?

谢谢

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Playground extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(100);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        Button button = new Button("Alert");
        button.setOnAction(event -> {
            ButtonType goodButton = new ButtonType("Good");
            ButtonType badButton = new ButtonType("Bad");
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", goodButton, badButton);
            alert.showAndWait();

            if (alert.getResult().equals(goodButton)) {
                System.out.println("Good");
            } else if (alert.getResult().equals(badButton)) {
                System.out.println("Bad");
            }
        });

        // Add the buttons to the layout
        root.getChildren().addAll(button);

        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}
3个回答

6
根据Dialog API文档中的“对话框关闭规则”,默认的“X”按钮仅在至少一个按钮为“CANCEL”类型时正常工作。因此,将您的任何一个按钮更改为ButtonType.CANCEL应在单击“X”时关闭对话框。
如果您不想使用内置按钮,则必须根据您的要求显式处理对话框的关闭请求。
            ButtonType goodButton = new ButtonType("Good");
            ButtonType badButton = new ButtonType("Bad");
            Alert alert = new Alert(Alert.AlertType.ERROR,"",goodButton,badButton);
            Window window = alert.getDialogPane().getScene().getWindow();
            window.setOnCloseRequest(e -> alert.hide());
            Optional<ButtonType> result = alert.showAndWait();
            result.ifPresent(res->{
                if (res.equals(goodButton)) {
                    System.out.println("Good");
                } else if (res.equals(badButton)) {
                    System.out.println("Bad");
                }
            });

1

另外,如果您希望警告窗口的关闭“X”按钮默认为其中一个警报按钮,则可以设置其 ButtonData 类型:

ButtonType goodButton = new ButtonType("Good");
ButtonType badButton = new ButtonType("Bad", ButtonBar.ButtonData.CANCEL_CLOSE);

关闭警报后,将相当于按下“坏”的按钮。

1

为了补充Sai Dandem的答案,这里是Dialog的相关javadoc:

...

对话框关闭规则

了解对话框关闭时会发生什么以及如何关闭对话框非常重要,特别是在异常关闭情况下(例如当单击对话框标题栏中的“X”按钮或输入特定于操作系统的键盘快捷键(例如Windows上的alt-F4)时)。幸运的是,在这些情况下,结果是明确的,并且可以最好地总结为以下要点:

  • JavaFX对话框只能在两种情况下“异常”关闭(如上所定义):
    1. 对话框只有一个按钮时,或
    2. 对话框有多个按钮,只要其中一个满足以下要求之一:
      1. 该按钮具有ButtonType,其ButtonBar.ButtonData类型为ButtonBar.ButtonData.CANCEL_CLOSE。
      2. 该按钮具有ButtonType,并且调用ButtonBar.ButtonData.isCancelButton()时,ButtonBar.ButtonData返回true。
  • 在所有其他情况下,对话框将拒绝响应所有关闭请求,保持打开状态,直到用户单击对话框DialogPane区域中可用的按钮之一。
  • 如果对话框被异常关闭,并且如果对话框包含满足上述两个条件之一的按钮,则对话框将尝试将result属性设置为使用第一个匹配的ButtonType调用result converter返回的任何值。
  • 如果由于任何原因结果转换器返回null,或者当只有一个非取消按钮存在时关闭对话框,则result属性将为null,并且showAndWait()方法将返回Optional.empty()。后一点意味着,如果您使用本类文档中提供的选项2或选项3,则Optional.ifPresent(java.util.function.Consumer) lambda永远不会被调用,代码将继续执行,就好像对话框根本没有返回任何值一样。
通常情况下,使用 AlertType.CONFIRMATION 时,已经有了一个取消按钮。然而,在你的 Alert 构造函数中声明自己的按钮会覆盖默认按钮。 Alert(AlertType,String,ButtonType...) 的 Javadoc:
通过传递可变数量的 ButtonType 参数,开发人员直接覆盖将在对话框中显示的默认按钮,用 varargs 数组中指定的任何内容替换预定义的按钮。
你的所有按钮都不是取消按钮。由于你没有指定 ButtonData,它们都具有 ButtonBar.ButtonData.OTHER

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接