java 如何更改 JavaFX 8 警报对话框中是/否按钮的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36309385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:15:08  来源:igfitidea点击:

How to change the text of yes/no buttons in JavaFX 8 Alert dialogs

javajavafxdialog

提问by coderodde

I have this code snippet:

我有这个代码片段:

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

Above, I request two buttons titled "Yes" and "No." However, I wish to change them both.

在上面,我请求了两个标题为“是”和“否”的按钮。但是,我想改变它们。

回答by fabian

You can define your own button types. In this example the buttons' text is fooand bar:

您可以定义自己的按钮类型。在这个例子中,按钮的文本是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;
}

回答by M. S.

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