gpt4 book ai didi

java - 无法使用系统菜单栏 Javafx

转载 作者:行者123 更新时间:2023-12-02 02:28:29 25 4
gpt4 key购买 nike

我想将 JavaFX 菜单栏添加到舞台,但让它使用 Mac 的系统菜单栏。

我的问题是使用:

menuBar.useSystemMenuBarProperty().set(true);

不起作用。我相信问题是因为我的主要方法不是 JavaFX 应用程序的一部分。我的主要方法如下所示:

public class SURPG_Launcher {

public static com.apple.eawt.Application macApp;

public static void main(String[] args) {
if(Toolbox.isMac()) {
initMac(args);
} else {
Application.launch(SURPG_Main.class, args);
}
}

private static void initMac(String[] args) {
System.out.println("MacOS System detected!");
macApp = com.apple.eawt.Application.getApplication();
macApp.setPreferencesHandler(new PreferencesHandler(){
@Override
public void handlePreferences(PreferencesEvent arg0) {
Platform.runLater(() -> {
Stage prefs = new Stage();
prefs.setMinHeight(200);
prefs.setMinWidth(200);
prefs.show();
});
}
});

Application.launch(SURPG_Mac.class, args);

}
}

SURPG_Mac.class 和 SURPG_Main.class 是扩展 JavaFX 应用程序的类。

我有另一个设置 GUI 的类,一个带有 BorderPane 的舞台。我有另一个带有公共(public)静态方法的类,可以调用这些方法来设置菜单栏,如下所示:

public class MenuControl {

public static MenuBar menuBar;
public static Menu menuFile;
public static Menu menuGame;
public static Menu menuTools;
public static MenuItem save;

public static void initMenusMac() {
menuBar = new MenuBar();
Platform.runLater(() -> {
menuBar.useSystemMenuBarProperty().set(true);
});
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}

public static void initMenus() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
}

我的最后一点是,由于 Mac 集成的不同兼容性问题,我无法更改它,因此主要方法位于 SURPG_Mac 或 SURPG_Main 中。

谁能帮我解决这个问题吗?

提前非常感谢您!

最佳答案

看看这个项目:https://github.com/codecentric/NSMenuFX它可以让你拥有一个更像 Mac 的菜单栏。但在使用它之前,您可能还必须清理有些奇怪的项目设置。

关于java - 无法使用系统菜单栏 Javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483342/

25 4 0