java JMenuBar、JMenu 和 JMenuItem 的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16450523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Height of JMenuBar, JMenu and JMenuItem
提问by Edward Ruchevits
Is it possible to set height of JMenuBar, JMenu and JMenuItem using UIManager once for all menus?
是否可以使用 UIManager 一次为所有菜单设置 JMenuBar、JMenu 和 JMenuItem 的高度?
I am currently using:
我目前正在使用:
setPreferredSize(new Dimension(100, 25));
But I feel it's not the best way.
但我觉得这不是最好的方法。
回答by Mady
The best way is to let system handle the width and height (a call to super should give you the correct width and height which will display the full text contents)
最好的方法是让系统处理宽度和高度(调用 super 应该给你正确的宽度和高度,这将显示全文内容)
Although if you want explicitly setting the width to 100 and height to 25, setPreferredSize() is one way.. Another way is to use Box Layout
虽然如果你想明确地将宽度设置为 100 和高度为 25,setPreferredSize() 是一种方法..另一种方法是使用 Box Layout
menuBar.add(Box.createRigidArea(new Dimension(100,25)));
回答by Bolo Kevin
The answer by Mady will squeeze all your JMenu
s(if that's what you added on the JMenuBar
) to the extreme left corner and display your empty Box on the menubar.
Mady 的答案会将您所有的JMenu
s(如果这是您在 上添加的JMenuBar
)压缩到最左角,并在菜单栏上显示您的空 Box。
This works well:
这很好用:
menubar.setPreferredSize(new Dimension(width,height));