按下 "x" 按钮时,Javafx 中的警报不会关闭
Alerts in Javafx do not close when "x" button is pressed
您好,对于不同的 javafx 应用程序,我一直在测试警报,唯一不起作用的是按下警报框的 "X" 按钮。
我在下面添加了一段代码,但如果您没有时间 运行,这里有一张 GIF,用于解释我在警告框方面遇到的问题:
https://giant.gfycat.com/GeneralUntimelyBluewhale.webm
我不太确定如何将 gif 文件上传到实际 post 很抱歉。
有什么办法可以解决这个问题吗?
谢谢
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Playground extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(100);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
Button button = new Button("Alert");
button.setOnAction(event -> {
ButtonType goodButton = new ButtonType("Good");
ButtonType badButton = new ButtonType("Bad");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", goodButton, badButton);
alert.showAndWait();
if (alert.getResult().equals(goodButton)) {
System.out.println("Good");
} else if (alert.getResult().equals(badButton)) {
System.out.println("Bad");
}
});
// Add the buttons to the layout
root.getChildren().addAll(button);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
根据 Dialog API 文档中的 "Dialog Closing Rules",默认 "X" 按钮只有在至少一个按钮的类型为 "CANCEL" 时才能正常工作。因此,将任何一个按钮更改为 ButtonType.CANCEL 应该会在单击 "X".
时关闭对话框
如果您对使用内置按钮不感兴趣,则必须根据您的要求明确处理对话框的关闭请求。
ButtonType goodButton = new ButtonType("Good");
ButtonType badButton = new ButtonType("Bad");
Alert alert = new Alert(Alert.AlertType.ERROR,"",goodButton,badButton);
Window window = alert.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(e -> alert.hide());
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(res->{
if (res.equals(goodButton)) {
System.out.println("Good");
} else if (res.equals(badButton)) {
System.out.println("Bad");
}
});
要添加到 ,这里是来自 Dialog
的相关 javadoc:
...
Dialog Closing Rules
It is important to understand what happens when a Dialog is closed, and also how a Dialog can be closed, especially in abnormal closing situations (such as when the 'X' button is clicked in a dialogs title bar, or when operating system specific keyboard shortcuts (such as alt-F4 on Windows) are entered). Fortunately, the outcome is well-defined in these situations, and can be best summarised in the following bullet points:
- JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:
- When the dialog only has one button, or
- When the dialog has multiple buttons, as long as one of them meets one of the following requirements:
- 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.
- In all other situations, the dialog will refuse to respond to all close requests, remaining open until the user clicks on one of the available buttons in the DialogPane area of the dialog.
- If a dialog is closed abnormally, and if the dialog contains a button which meets one of the two criteria above, the dialog will attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType.
- If for any reason the result converter returns null, or if the dialog is closed when only one non-cancel button is present, the result property will be null, and the showAndWait() method will return Optional.empty(). This later point means that, if you use either of option 2 or option 3 (as presented earlier in this class documentation), the Optional.ifPresent(java.util.function.Consumer) lambda will never be called, and code will continue executing as if the dialog had not returned any value at all.
正常情况下,使用AlertType.CONFIRMATION
时,已经有取消按钮了。但是,您在 Alert
的构造函数中声明了自己的按钮,它覆盖了默认按钮。
Alert(AlertType,String,ButtonType...)
的 Javadoc:
...
By passing in a variable number of ButtonType arguments, the developer is directly overriding the default buttons that will be displayed in the dialog, replacing the pre-defined buttons with whatever is specified in the varargs array.
...
而您的 none 个按钮是取消按钮。由于您没有指定 ButtonData
他们都有 ButtonBar.ButtonData.OTHER
.
您好,对于不同的 javafx 应用程序,我一直在测试警报,唯一不起作用的是按下警报框的 "X" 按钮。
我在下面添加了一段代码,但如果您没有时间 运行,这里有一张 GIF,用于解释我在警告框方面遇到的问题: https://giant.gfycat.com/GeneralUntimelyBluewhale.webm
我不太确定如何将 gif 文件上传到实际 post 很抱歉。
有什么办法可以解决这个问题吗?
谢谢
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Playground extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(100);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
Button button = new Button("Alert");
button.setOnAction(event -> {
ButtonType goodButton = new ButtonType("Good");
ButtonType badButton = new ButtonType("Bad");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", goodButton, badButton);
alert.showAndWait();
if (alert.getResult().equals(goodButton)) {
System.out.println("Good");
} else if (alert.getResult().equals(badButton)) {
System.out.println("Bad");
}
});
// Add the buttons to the layout
root.getChildren().addAll(button);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
根据 Dialog API 文档中的 "Dialog Closing Rules",默认 "X" 按钮只有在至少一个按钮的类型为 "CANCEL" 时才能正常工作。因此,将任何一个按钮更改为 ButtonType.CANCEL 应该会在单击 "X".
时关闭对话框如果您对使用内置按钮不感兴趣,则必须根据您的要求明确处理对话框的关闭请求。
ButtonType goodButton = new ButtonType("Good");
ButtonType badButton = new ButtonType("Bad");
Alert alert = new Alert(Alert.AlertType.ERROR,"",goodButton,badButton);
Window window = alert.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(e -> alert.hide());
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(res->{
if (res.equals(goodButton)) {
System.out.println("Good");
} else if (res.equals(badButton)) {
System.out.println("Bad");
}
});
要添加到 Dialog
的相关 javadoc:
...
Dialog Closing Rules
It is important to understand what happens when a Dialog is closed, and also how a Dialog can be closed, especially in abnormal closing situations (such as when the 'X' button is clicked in a dialogs title bar, or when operating system specific keyboard shortcuts (such as alt-F4 on Windows) are entered). Fortunately, the outcome is well-defined in these situations, and can be best summarised in the following bullet points:
- JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:
- When the dialog only has one button, or
- When the dialog has multiple buttons, as long as one of them meets one of the following requirements:
- 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.
- In all other situations, the dialog will refuse to respond to all close requests, remaining open until the user clicks on one of the available buttons in the DialogPane area of the dialog.
- If a dialog is closed abnormally, and if the dialog contains a button which meets one of the two criteria above, the dialog will attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType.
- If for any reason the result converter returns null, or if the dialog is closed when only one non-cancel button is present, the result property will be null, and the showAndWait() method will return Optional.empty(). This later point means that, if you use either of option 2 or option 3 (as presented earlier in this class documentation), the Optional.ifPresent(java.util.function.Consumer) lambda will never be called, and code will continue executing as if the dialog had not returned any value at all.
正常情况下,使用AlertType.CONFIRMATION
时,已经有取消按钮了。但是,您在 Alert
的构造函数中声明了自己的按钮,它覆盖了默认按钮。
Alert(AlertType,String,ButtonType...)
的 Javadoc:
...
By passing in a variable number of ButtonType arguments, the developer is directly overriding the default buttons that will be displayed in the dialog, replacing the pre-defined buttons with whatever is specified in the varargs array.
...
而您的 none 个按钮是取消按钮。由于您没有指定 ButtonData
他们都有 ButtonBar.ButtonData.OTHER
.