我有一个扩展 JFrame 的 MainWindow 类。 在 MainWindow 中我有一个 JMenuBar。
我想在 OSX 中将菜单栏显示在顶部(Apple 符号旁边)。这仅在我未设置 Substance Skin 时有效。是否可以使用 Substance Skin 并使用 MacOS 菜单栏?
我的代码:
//Set Menu for MacOS
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", name);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
SubstanceSkin skin = new GraphiteGlassSkin();
SubstanceLookAndFeel.setSkin(skin); //WORKS WHEN I COMMENT THIS (WITHOUT SUBSTANCE SKIN)
JFrame.setDefaultLookAndFeelDecorated(false);
MainWindow mainWindow = new MainWindow(name);
mainWindow.setVisible(true);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
您可以像这样单独指定菜单栏的 UI:
try {
UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
// log...
}
JMenuBar menubar = frame.getJMenuBar(); // assuming you've set the menu bar already
String os = System.getProperty("os.name");
if (os.equals("Mac OS X")) {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
menubar.setUI((MenuBarUI) Class.forName("com.apple.laf.AquaMenuBarUI").newInstance());
} catch (Exception ex) {
// log...
}
}
关于java - 物质和 MacOS 菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688518/