我有以下代码片段:

Alert alert =
        new Alert(AlertType.WARNING,
            "The format for dates is year.month.day. " +
            "For example, today is " + todayToString() + ".",
             ButtonType.OK,
             ButtonType.CANCEL);
alert.setTitle("Date format warning");
Optional<ButtonType> result = alert.showAndWait();

if (result.get() == ButtonType.OK) {
    formatGotIt = true;
}

在上方,我要求两个按钮分别为"is"和“否”。但是,我希望两者都改变​​。

最佳答案

您可以定义自己的按钮类型。在此示例中,按钮的文本为foobar:

ButtonType foo = new ButtonType("foo", ButtonBar.ButtonData.OK_DONE);
ButtonType bar = new ButtonType("bar", ButtonBar.ButtonData.CANCEL_CLOSE);
Alert alert = new Alert(AlertType.WARNING,
        "The format for dates is year.month.day. "
        + "For example, today is " + todayToString() + ".",
        foo,
        bar);

alert.setTitle("Date format warning");
Optional<ButtonType> result = alert.showAndWait();

if (result.orElse(bar) == foo) {
    formatGotIt = true;
}

10-08 13:54