如何在JavaFX 8 Alert对话框中更改yes / no按钮的文本

时间:2016-03-30 12:49:28

标签: javafx dialog

我有这段代码:

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;
}

上面,我要求两个按钮标题为&#34;是&#34;和#34;没有。&#34;但是,我希望改变它们。

2 个答案:

答案 0 :(得分:30)

您可以定义自己的按钮类型。在这个例子中按钮&#39;文字为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;
}

答案 1 :(得分:3)

((Button) dialog.getDialogPane().lookupButton(ButtonType.OK)).setText("Not OK Anymore");
((Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL)).setText("Not Cancel Anymore");