我有一个带有菜单栏的程序,该程序带有一个“关于”按钮来显示有关该应用程序的一些信息。
事实是,当我使用AlertType.INFORMATION时,我可以单击确定按钮来关闭警报,但是当我按下关闭窗口按钮时使用无时,什么也没发生。我已经尝试过将setOnCloseAction(e-> close());但也不会关闭。
谢谢!
public class RootLayoutController {
private MainApp main;
@FXML
private MenuItem loadFiles;
@FXML
private MenuItem about;
@FXML
private void displayAbout() {
Alert alert = new Alert(AlertType.NONE);
alert.initStyle(StageStyle.UTILITY);
alert.initOwner(main.getPrimaryStage());
alert.setTitle("Organiz3r");
alert.setHeaderText("Organiz3r v1.0");
alert.setContentText("Developed at BitBucket");
alert.showAndWait();
}
@FXML
private void handleLoad() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Files");
List<File> files = fileChooser.showOpenMultipleDialog(main.getPrimaryStage());
main.loadFiles(files);
}
public RootLayoutController() {
// TODO Auto-generated constructor stub
}
public void setMain(MainApp main) {
this.main = main;
}
[Main在主类中设置为
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
RootLayoutController controller = loader.getController();
controller.setMain(this);
[documentation解释(在“对话框关闭规则”部分中),除非有一个按钮,或者有两个或多个按钮,其中一个实质上是一个“取消”按钮。因此,当您使用Alert
创建一个AlertType.NONE
时,它没有按钮,因此使用标准的“关闭窗口”按钮将其关闭。
因此,如果您不想要AlertType.INFORMATION
,则需要在警报中添加一个按钮,可以使用此按钮
alert.getDialogPane().getButtonTypes().add(ButtonType.OK);
基于Dialog documentation,看来您必须在Dialog / Alert中至少有一个按钮才能使用角上的“ x”按钮将其关闭。根据文档,使用“ x”按钮关闭被视为“异常关闭”。它是这样的:
在两种情况下,只能“异常”关闭JavaFX对话框(如上定义):
当对话框只有一个按钮时,或
当对话框具有多个按钮时,只要其中一个按钮满足以下要求之一:
该按钮具有一个ButtonType,其ButtonBar.ButtonData的类型为ButtonBar.ButtonData.CANCEL_CLOSE。
按钮具有一个ButtonType,当调用ButtonBar.ButtonData.isCancelButton()时,其ButtonBar.ButtonData返回true。在所有其他情况下,对话框将拒绝响应所有关闭请求...
您可以使用AlertType.INFORMATION,然后隐藏“确定”按钮。这样一来,您无需使用其他按钮即可从一角通过“ x”按钮关闭窗口。
dialogPane.lookupButton(ButtonType.OK).setVisible(false);