Java 如何设置 JMenuItem 的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19580343/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:30:26  来源:igfitidea点击:

How to set a size for JMenuItem?

javaswingjmenujmenuitempreferredsize

提问by Zalberth

enter image description here

在此处输入图片说明

As you can see, it's ugly to have these kind of JMenuItems. The width of the menu items is quite small.

正如你所看到的,拥有这些类型是很丑陋JMenuItem的。菜单项的宽度非常小。

Here is the code:

这是代码:

JMenu menuOne=new JMenu("MenuOne");
    JMenu menuTwo=new JMenu("MenuTwo");
    JMenu menuThree=new JMenu("MenuThree");
    JMenu menuFour=new JMenu("MenuFour");
    JMenuBar mbar=new JMenuBar();
    //add the menu to menubar
    mbar.add(menuOne);
    JMenuItem OneItOne=new JMenuItem("1");

    JMenuItem OneItTwo=new JMenuItem("2");
    menuOne.add(OneItOne);
    menuOne.addSeparator();
    menuOne.add(OneItTwo);
    mbar.add(menuTwo);
    mbar.add(menuThree);
    mbar.add(menuFour);
    setJMenuBar(mbar);

Simply adding some blanks in the String is okay (e.g. "1     "), but is there any better way to set a preferred length of JMenuItem? I've tried OneItTwo.setSize(), but failed.

简单地在 String 中添加一些空格是可以的(例如"1     "),但是有没有更好的方法来设置 的首选长度JMenuItem?我试过了OneItTwo.setSize(),但失败了。

采纳答案by Boann

setPreferredSize(new Dimension(...));works on menu items. setMinimumSizedoes not work though; it does not seem to be honored by the menu layout manager, so if you want to set a minimum that may be overridden by having larger content, or you want to set a minimum or preferred width without changing the height, you must override getPreferredSize()in a subclass. E.g.,

setPreferredSize(new Dimension(...));适用于菜单项。setMinimumSize虽然不起作用;菜单布局管理器似乎不尊重它,因此如果您想设置可能被更大内容覆盖的最小值,或者您想设置最小或首选宽度而不更改高度,则必须getPreferredSize()在子类。例如,

menuOne.add(new JMenuItem("1") {
    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.width = Math.max(d.width, 400); // set minimums
        d.height = Math.max(d.height, 300);
        return d;
    }
});

回答by ricgeorge

Here, you can use setPreferredSizeto specify the dimensions you need. You can specify the width to whatever you want (200 looks good to me). You can allow the height to remain autosized based on the amount of menu items by getting the default preferred size:

在这里,您可以使用setPreferredSize来指定您需要的尺寸。您可以将宽度指定为您想要的任何值(200 对我来说看起来不错)。您可以通过获取默认的首选大小来允许高度根据菜单项的数量保持自动调整大小:

JMenuItem item = new JMenuItem();
item.setPreferredSize(new Dimension(200, item.getPreferredSize().height));

回答by Quentin

If you want to set the size of your JMenuItem (width in my case). You can change the width with this solution :

如果您想设置 JMenuItem 的大小(在我的情况下为宽度)。您可以使用此解决方案更改宽度:

 JMenuItem returnMenu = new JMenuItem(action) {
            @Override
            public Dimension getPreferredSize() {
                Dimension preferred = super.getPreferredSize();
                Dimension minimum = getMinimumSize();
                Dimension maximum = getMaximumSize();
                preferred.width = Math.min(Math.max(preferred.width, 200),
                        maximum.width);
                preferred.height = Math.min(Math.max(preferred.height, minimum.height),
                        maximum.height);
                return preferred;
            }
        };

I just reused this solution : JMenuItem setMinimumSize doesn't work

我只是重用了这个解决方案:JMenuItem setMinimumSize 不起作用

It works better as you an see. Selected solution beforeThis solution afterI grant you, this is similar to the selected answer.

正如你所见,它效果更好。Selected solution before我授予您之后的此解决方案 ,这类似于所选答案。