从jdk 8u40开始,我正在使用新的javafx.scene.control.AlertAPI来显示确认对话框.在下面的示例中,默认情况下"是"按钮而不是"否"按钮:
public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
final Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
final Optional<ButtonType> result = alert.showAndWait();
return result.get() == ButtonType.YES;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何改变它.
编辑:
这里是默认情况下聚焦"是"按钮的结果屏幕截图:

我已经尝试了下面的代码,它适用于鼠标事件,但当我使用键事件,即任何按钮上的ENTER键,而不是显示结果.
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(null);
alert.setHeaderText(null);
alert.setGraphic(null);
alert.setContentText("Choose your option.");
ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne) {
System.out.println("One");
} else if (result.get() == buttonTypeTwo) {
System.out.println("Two");
} else if (result.get() == buttonTypeThree) {
System.out.println("Three");
}
Run Code Online (Sandbox Code Playgroud)