gpt4 book ai didi

java - Java 中对 MacOS X 的 native Swing 菜单栏支持

转载 作者:太空狗 更新时间:2023-10-29 22:43:22 26 4
gpt4 key购买 nike

突出的链接是 http://www.devdaily.com/blog/post/jfc-swing/handling-main-mac-menu-in-swing-application/但是 Mac OS X 下的菜单栏显示为包名称而不是应用程序名称。我在没有任何运气的情况下使用上面链接中的代码,所以我不确定最近的 Mac OS 版本是否有任何更改。

摘录如下:

public RootGUI() {
super("Hello");
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem item = new JMenuItem("Woah");
file.add(item);
menuBar.add(file);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
pack();
setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new RootGUI();
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e.getMessage());
}
catch(InstantiationException e) {
System.out.println("InstantiationException: " + e.getMessage());
}
catch(IllegalAccessException e) {
System.out.println("IllegalAccessException: " + e.getMessage());
}
catch(UnsupportedLookAndFeelException e) {
System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
}

}
});
}

菜单栏上的第一个菜单项应该显示为“测试”,不幸的是,情况并非如此。另一方面,文件菜单工作正常。有什么想法吗?

最佳答案

@凯泽

我想我明白发生了什么。如果您将 main() 方法放在一个不同的类中,那么一切正常。所以你需要这样的东西:

public class RootGUILauncher {
public static void main(String[] args) {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e.getMessage());
}
catch(InstantiationException e) {
System.out.println("InstantiationException: " + e.getMessage());
}
catch(IllegalAccessException e) {
System.out.println("IllegalAccessException: " + e.getMessage());
}
catch(UnsupportedLookAndFeelException e) {
System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RootGUI();
}
});
}

然后将您的 RootGUI 类放在不同的文件中。

关于java - Java 中对 MacOS X 的 native Swing 菜单栏支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/307024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com