JavaFX 异常关闭警报 Returns 错误结果
JavaFX Abnormally Closing Alert Returns Incorrect Result
我创建了一个 JavaFX Alert
对象,在调用 showAndWait
时该对象 return 出现意外结果。下面的代码说明了我观察到的行为:
package myPackage;
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) {
launch();
}
private static boolean isYes(final Optional<ButtonType> result) {
return (result.isPresent() && result.get().getButtonData() == ButtonData.YES);
}
@Override
public void start(Stage primaryStage) throws Exception {
final Alert alert = new Alert(AlertType.CONFIRMATION,
"This is a test", ButtonType.NO, ButtonType.YES);
System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
}
}
当我运行上面的应用程序时,会显示两个对话框。在第一个对话框中单击 "Yes",然后关闭(通过单击右上角的 "x")第二个。通过执行上述步骤,我希望应用程序打印以下内容:
Yes
No or Closed
但是,我实际看到的打印内容是:
Yes
Yes
Dialog documentation 声明 "abnormal closing condition"(例如单击右上角的小 "x")将 "attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType." 给定此语句的上下文,我将 "matching ButtonType" 解释为一个 ButtonType 要么(直接引用文档):
- The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
- The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.
我对文档的解释不正确,或者这是 JavaFX 中的错误?不管为什么这不像我预期的那样工作,在这种情况下,有什么办法可以强制 "abnormal closing conditions" 到 return ButtonType.NO
?
这是 JavaFX 中的另一个错误。我已将其报告给 Oracle,并为其分配了 Bug ID JDK-8173114。作为 work-around,我只是将以下行添加到我的 JavaFX Alert
子类的构造函数中:
setOnShowing(event -> setResult(null));
以上 work-around 似乎适用于 Alert
、ChoiceDialog
和 TextInputDialog
。
以上解决方法不适用于 java 8 & win 10。但这有效:
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
// alert.setOnShowing(event -> alert.setResult(null));
alert.setTitle("Delete similar files?");
ButtonType okButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(okButton, noButton, cancelButton);
Optional<ButtonType> choose = alert.showAndWait();
// String text = new String("");
// if (choose.get() != null)
// text = choose.get().getText();
// if (text != null && (text.equals("YES") || text.equals("Yes")))
if (choose.get() == okButton){
我创建了一个 JavaFX Alert
对象,在调用 showAndWait
时该对象 return 出现意外结果。下面的代码说明了我观察到的行为:
package myPackage;
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) {
launch();
}
private static boolean isYes(final Optional<ButtonType> result) {
return (result.isPresent() && result.get().getButtonData() == ButtonData.YES);
}
@Override
public void start(Stage primaryStage) throws Exception {
final Alert alert = new Alert(AlertType.CONFIRMATION,
"This is a test", ButtonType.NO, ButtonType.YES);
System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
}
}
当我运行上面的应用程序时,会显示两个对话框。在第一个对话框中单击 "Yes",然后关闭(通过单击右上角的 "x")第二个。通过执行上述步骤,我希望应用程序打印以下内容:
Yes
No or Closed
但是,我实际看到的打印内容是:
Yes
Yes
Dialog documentation 声明 "abnormal closing condition"(例如单击右上角的小 "x")将 "attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType." 给定此语句的上下文,我将 "matching ButtonType" 解释为一个 ButtonType 要么(直接引用文档):
- The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
- The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.
我对文档的解释不正确,或者这是 JavaFX 中的错误?不管为什么这不像我预期的那样工作,在这种情况下,有什么办法可以强制 "abnormal closing conditions" 到 return ButtonType.NO
?
这是 JavaFX 中的另一个错误。我已将其报告给 Oracle,并为其分配了 Bug ID JDK-8173114。作为 work-around,我只是将以下行添加到我的 JavaFX Alert
子类的构造函数中:
setOnShowing(event -> setResult(null));
以上 work-around 似乎适用于 Alert
、ChoiceDialog
和 TextInputDialog
。
以上解决方法不适用于 java 8 & win 10。但这有效:
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
// alert.setOnShowing(event -> alert.setResult(null));
alert.setTitle("Delete similar files?");
ButtonType okButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(okButton, noButton, cancelButton);
Optional<ButtonType> choose = alert.showAndWait();
// String text = new String("");
// if (choose.get() != null)
// text = choose.get().getText();
// if (text != null && (text.equals("YES") || text.equals("Yes")))
if (choose.get() == okButton){