我有一个适用于Mac的JavaFX应用程序,我正在使用NSMenuFX创建菜单栏。我使用MenuToolkit创建我的应用程序菜单。如果使用setApplicationMenu方法,则菜单正确显示。例如,它说退出MyApplication而不是退出com.example.MyApplication。但是,如果满足某些条件,我的代码将取消应用程序的关闭。我有一个处理关闭请求的舞台上的EventHandler。如果应用程序不应该关闭WindowEvent消耗。问题是应用程序仍然关闭。但是,如果我不使用setApplicationMenu,菜单显示不正确(它表示Quit com.example.MyApplication),但是使用WindowEvent会停止关闭应用程序。我正在使用Java 1.8u77。任何想法,我在做什么不正确?我无法在NSMenuFX附带的示例代码中重现此问题。以下是创建菜单栏的代码。nsmenufx MenuToolkit.setApplicationMenu产生不一致的行为
private void createMenu(VBox appBox) {
// Create the menubar
MenuBar menuBar = new MenuBar();
menuBar.useSystemMenuBarProperty().set(true);
MenuToolkit tk = MenuToolkit.toolkit();
String appName = "MyApplication";
Menu appMenu = new Menu(appName);
menuBar.getMenus().add(appMenu);
MenuItem aboutItem = tk.createAboutMenuItem("MyApplication");
aboutItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
AboutDialog aboutDialog = new AboutDialog(null);
aboutDialog.initOwner(stage);
aboutDialog.showAndWait();
}
});
appMenu.getItems().add(aboutItem);
appMenu.getItems().addAll(new SeparatorMenuItem(),
tk.createHideMenuItem(appName), tk.createHideOthersMenuItem(),
tk.createUnhideAllMenuItem(),
new SeparatorMenuItem(), tk.createQuitMenuItem(appName));
tk.setApplicationMenu(appMenu);
// Add the File menu
Menu file = new Menu("File");
menuBar.getMenus().add(file);
// Add the Print item
print = new MenuItem("Print");
print.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(
lineChart.getChart().getScene().getWindow())) {
boolean success = job.printPage(lineChart.getChart());
if (success) {
job.endJob();
}
}
});
file.getItems().add(print);
// Window Menu
Menu windowMenu = new Menu("Window");
windowMenu.getItems().addAll(
tk.createMinimizeMenuItem(), tk.createZoomMenuItem(),
tk.createCycleWindowsItem(), new SeparatorMenuItem(),
tk.createBringAllToFrontItem());
menuBar.getMenus().add(windowMenu);
tk.autoAddWindowMenuItems(windowMenu);
tk.setGlobalMenuBar(menuBar);
}