Java Swing OSX Window Menu Icon Alignment

落花浮王杯 提交于 2019-12-29 02:16:50

问题


Java Swing seems to place the 'Menu Text' after the icon (if present) on MenuItems. See example below.

It does NOT look very nice.

Is there a way around this?

On OSX the icon fits in the left margin and the text aligns with all other MenuItems.


回答1:


Do you mean something like this :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextPaneExample
{
    private Icon info = UIManager.getIcon("OptionPane.informationIcon");
    private Icon error = UIManager.getIcon("OptionPane.errorIcon");
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("JTextPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextPane tpane = new JTextPane();
        tpane.setContentType("text/html");
        JScrollPane scroller = new JScrollPane();
        scroller.setViewportView(tpane);

        try
        {
            java.net.URL url = new java.net.URL("http://maps.google.es/");
            //tpane.setPage(url);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        frame.setJMenuBar(createMenuBar());

        frame.getContentPane().add(scroller);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private JMenuBar createMenuBar()
    {
        JMenuBar menuBar = new JMenuBar();
        JMenu windowMenu = new JMenu("Window");
        JMenuItem minimizeItem = new JMenuItem("Minimize");
        minimizeItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
        minimizeItem.setIcon(info);
        minimizeItem.setIconTextGap(1);
        minimizeItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        JMenuItem zoomItem = new JMenuItem("Zoom");
        zoomItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
        zoomItem.setIconTextGap(1);
        zoomItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);         
        cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
        cbmi.setIconTextGap(17);
        cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        windowMenu.add(minimizeItem);
        windowMenu.add(zoomItem);
        windowMenu.add(cbmi);
        menuBar.add(windowMenu);
        return menuBar;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JTextPaneExample().createAndDisplayGUI();
            }           
        });
    }
}

Here is the Output :




回答2:


You could try either of these approaches:

  • Unicode characters are appealing, but they offer poor alignment in a variable pitch font:

    JMenuBar menuBar = new JMenuBar();
    JMenu windowMenu = new JMenu("Window");
    windowMenu.add(new JMenuItem("♦ Item"));
    windowMenu.add(new JMenuItem("✓ Item"));
    windowMenu.add(new JMenuItem("• Item"));
    menuBar.add(windowMenu);
    frame.setJMenuBar(menuBar);
    
  • Better, implement the Icon interface, illustrated here and here, using a fixed-size implementation to control geometry. CellTest shows one approach to rendering an arbitrary unicode glyph.



来源:https://stackoverflow.com/questions/10502844/java-swing-osx-window-menu-icon-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!