[英]How do I move my JMenuBar to the screen menu bar on Mac OS X?
When I move my JMenuBar
to the screen menu bar on Mac OS X, it leaves some blank space where the menu would be in my window;当我将
JMenuBar
移动到 Mac OS X 上的屏幕菜单栏时,它会在菜单所在的窗口中留下一些空白空间; I need to remove that space.我需要删除那个空间。 I am using
我在用
System.setProperty("apple.laf.useScreenMenuBar", "true")
to move my JMenuBar
to the screen menu bar.将我的
JMenuBar
移动到屏幕菜单栏。 My friend who uses a Mac reports that this leaves some ugly vertical space where the menu would reside if I did not set that property.我使用 Mac 的朋友报告说,如果我没有设置该属性,这会在菜单所在的位置留下一些难看的垂直空间。 What is the best way to resolve this issue?
解决此问题的最佳方法是什么?
EDIT: Here is an example from my source:编辑:这是我的资料来源的一个例子:
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name");
JFrame frame = new JFrame("Gabby");
final DesktopMain dm = new DesktopMain();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dm);
frame.setSize(160, 144);
frame.setLocationRelativeTo(null);
frame.setIgnoreRepaint(true);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// Populating the menu bar code goes here
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
Depending on when it's done, setting the property after your program launches may be too late to be effective.根据完成的时间,在程序启动后设置属性可能为时已晚而无法生效。 Instead, add the setting at launch time.
相反,在启动时添加设置。
java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
Alternatively, set the property in your application bundle's Info.plist
, as discussed in Java Deployment Options for Mac OS X , Java Dictionary Info.plist Keys , About Info.plist Keys and Java Runtime System Properties .或者,在应用程序包的
Info.plist
设置属性,如Mac OS X 的 Java 部署选项、 Java 字典 Info.plist 键、 关于 Info.plist 键和Java 运行时系统属性中所述。
<key>Properties</key>
<dict>
<key>apple.laf.useScreenMenuBar</key>
<string>true</string>
...
</dict>
Addendum: As shown below, the problem does not appear using the approach suggested by @Urs Reupke or myself.附录:如下所示,使用@Urs Reupke 或我自己建议的方法不会出现问题。 Your (missing)
DesktopMain
may be at fault.您的(丢失的)
DesktopMain
可能有问题。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/questions/8955638 */
public class NewMain {
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty(
"com.apple.mrj.application.apple.menu.about.name", "Name");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Gabby");
final JPanel dm = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
dm.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dm);
frame.pack();
frame.setLocationByPlatform(true);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
});
}
}
As an alternative to what trashgod suggests, do what you're doing - but do it very early, before you initialize your UI.作为垃圾神建议的替代方案,做你正在做的事情 - 但在你初始化你的 UI 之前尽早做。
Also, you might consider using this to display your application's name in the bar:此外,您可以考虑使用它在栏中显示您的应用程序名称:
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MyApplication");
I know this question is old, but what worked for me is I created a new class called Main, and then set the system property like我知道这个问题很老,但对我有用的是我创建了一个名为 Main 的新类,然后设置系统属性,如
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.awt.application.name", "My app");
Works in JDK15.在 JDK15 中工作。
对于 Java 8 使用这个
System.setProperty("apple.awt.application.name", "My app");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.