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

标签 java eclipse javafx macos-sierra efxclipse

我想将 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/

相关文章:

切换场景的 JavaFX 问题

java - 如何在 eclipse Juno 中添加 xhtml 向导

java - 如何在 IntelliJ IDEA 中为 Maven Java 项目禁用 Kotlin 编译器?

java - 如何在 Hadoop MapReduce java API 中使用 Java 断言?

javascript - 需要帮助将我的处理代码转换为 p5.js(ArrayList +其他!)

java - 创建媒体文件以保存图像

eclipse - 了解 JVM 堆大小(使用 Eclipse 时)

browser - 使用 JavaFX WebView 下载

java - 如何定义可执行 jar 文件的正确类路径?

java - 如果每个 'transaction' 都是一个对象,那么如何存储所有交易的总成本?