【发布时间】:2015-04-30 05:19:18
【问题描述】:
为什么 JavaFX 警报对话框会触发 Platform.exit();即使警报对话框中的焦点按钮是取消,当我按下 Enter 键时?
soaStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
@Override
public void handle(WindowEvent event)
{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirm");
alert.setHeaderText("Are you sure you want to exit?");
alert.setContentText("Press OK to exit, or Cancel to stay.");
alert.initOwner(soaStage);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK)
{
Platform.exit();
}
else
{
event.consume();
}
}
});
【问题讨论】:
-
我相信要激活焦点按钮,您必须按空格键。这是真的(我认为)不仅适用于 Java/JavaFX。例如,在 Windows 原生提示中,我使用 tab 来快速导航并使用空格来激活所选按钮,而不是 Enter。这是根据我的经验,我不知道这是否是一个既定的指导方针。
-
我猜你是对的,但我只是在 C# 中尝试了 MessageBoxButtons.YesNo,我们可以通过 Enter 键或空格键激活焦点按钮。
-
我明白了。我的评论更多是个人观点,不管它值多少钱。将其读作“我,作为用户,按空格键激活焦点按钮,而不是回车”。