Java JMenu的文本未居中

Java JMenu的文本未居中,java,swing,look-and-feel,Java,Swing,Look And Feel,我扩展了basicmenui,并覆盖了getPreferredSize(JComponent c),因此JMenu的文本不再居中 我尝试使用几种setAlignment方法来修复它,但没有任何效果 我希望所有菜单的大小都相同,并且以文本为中心 谢谢。从BasicMenuItemUI继承而来的相关设计应该是“适合外观和感觉”。每个L&F都使用不同大小的装饰。如果不这样做,则应返回null并指定“组件的布局管理器” 当然,一个新的计划会有所帮助 增编: JMenu的文本不再居中 我认为它从来没有居中

我扩展了
basicmenui
,并覆盖了
getPreferredSize(JComponent c)
,因此
JMenu
的文本不再居中

我尝试使用几种
setAlignment
方法来修复它,但没有任何效果

我希望所有菜单的大小都相同,并且以文本为中心

谢谢。

BasicMenuItemUI
继承而来的相关设计应该是“适合外观和感觉”。每个L&F都使用不同大小的装饰。如果不这样做,则应返回
null
并指定“组件的布局管理器”

当然,一个新的计划会有所帮助

增编:

JMenu的文本不再居中

我认为它从来没有居中过,但如果需要,您可以移动
textRect

class CustomMenuUI extends BasicMenuUI {

    public static ComponentUI createUI(JComponent c) {
        return new CustomMenuUI();
    }

    @Override
    protected void paintText(Graphics g, JMenuItem menuItem,
            Rectangle textRect, String text) {
        g.setColor(Color.red);
        int w2 = menuItem.getBounds().width / 2;
        textRect.translate(w2 - textRect.width / 2, 0);
        super.paintText(g, menuItem, textRect, text);
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        return new Dimension(80, 32);
    }
}


重新格式化的代码;如果不正确,请还原。这应该是对您的问题的更新。
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.put("MenuUI", "testmenuui.CustomMenuUI");
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                    createAndShowGui();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static void createAndShowGui() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuTest1 = new JMenu("Menu1");

        JMenu menuTest2 = new JMenu("Menu2");

        menuBar.add(menuTest1);
        menuBar.add(menuTest2);

        JFrame frame = new JFrame();
        frame.setJMenuBar(menuBar);
        frame.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setVisible(true);
    }
}

class CustomMenuUI extends BasicMenuUI {

    public static ComponentUI createUI(JComponent c) {
        return new CustomMenuUI();
    }

    @Override
    protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
        g.setColor(Color.black);
        super.paintBackground(g, menuItem, bgColor);
    }

    @Override
    protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
        g.setColor(Color.white);
        super.paintText(g, menuItem, textRect, text);
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        return new Dimension(100, 100);
    }
}