Mac OS X 上的JMenuBar 移動問題
嘗試在Mac OS X 環境中將JMenuBar 移到螢幕選單列時,視窗中菜單所在的位置通常會留下空白區域。這個問題可以透過在適當的時候建立屬性「apple.laf.useScreenMenuBar」來解決。
解決方案
要解決此問題,請設定屬性「apple .laf.useScreenMenuBar」在程式啟動時使用下列方法之一:
java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
<key>Properties</key> <dict> <key>apple.laf.useScreenMenuBar</key> <string>true</string> ... </dict>
其他注意事項
需要注意的是,在可能應用程式啟動後設定屬性不會有效。以下程式碼示範了不存在空格的正確實作:
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); } }); } }
以上是為什麼我的 JMenuBar 在 Mac OS X 螢幕選單列上留下空白?的詳細內容。更多資訊請關注PHP中文網其他相關文章!