Swing menu item on right of Menu Bar

大兔子大兔子 提交于 2019-12-23 15:37:33

问题


I have a swing application and on the JFrame's menu I want to add a Help MenuItem, but have it Right justified.
Any ideas ?

A Swing JMenuBar has a BoxLayout and I have tried:

menuItem = new JMenuItem("Help");
menuItem.setAlignmentX(Box.RIGHT_ALIGNMENT);
menuBar.add(menuItem);

The menu just stays on the left. I have also tried:

menuBar.add(Box.createHorizontalGlue());  

as per the Swing Tutorial... but that just adds a space.

I am using Windows 7. JDK 1.6.26

EDIT: It works as per the Java Tutorial if I do:

    menuBar.add(Box.createHorizontalGlue());

    helpMenu = new JMenu("Help");
    menuBar.add(helpMenu);
    menuItem = new JMenuItem("Help");
    helpMenu.add(menuItem);  

But that is not what I am looking for. I just want to be able to add the help MenuItem to the JMenuBar. For now that will need to be my fallback.


回答1:


Try Component.setComponentOrientation() method.

menuItem.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);


来源:https://stackoverflow.com/questions/6851867/swing-menu-item-on-right-of-menu-bar

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