【问题标题】:JMenuItem setMinimumSize doesn't workJMenuItem setMinimumSize 不起作用
【发布时间】:2009-08-28 09:18:40
【问题描述】:

我尝试为我的菜单项设置最小宽度,但它似乎不起作用。这是我创建项目的函数:

 private JMenuItem newItem(String text, String iconPath) {
     JMenuItem myMenuItem;
     if (iconPath == null || iconPath.isEmpty()) {
         myMenuItem = new JMenuItem(text);
     }
     else {
         ImageIcon icon = new ImageIcon(iconPath);
         myMenuItem = new JMenuItem(text, icon);
     }
     // this would work, but then setMaximumSize doesn't have any effect
     // myMenuItem.setPreferredSize(new Dimension(250,20)); 
     myMenuItem.setMinimumSize(new Dimension(250,20)); 
     myMenuItem.setMaximumSize(new Dimension(350,20));

     return myMenuItem;
 }

我做错了什么?

PS。我在 Windows XP 上使用 jdk1.6,带有 System Look&Feel 的 Servicepack 3

【问题讨论】:

  • 顺便说一句,“iconPath == ""”比较不起作用。
  • 哦,没错:)。谢谢!所以使用脚本语言,我忘记了字符串比较在java中不是静态的。
  • 您使用的是哪个版本的 Java?您使用的是哪个操作系统?您使用的是哪种外观和感觉?在 Swing 中,这很重要 :-(
  • 我在 WinXP servicepack 3 上使用 jdk1.6,系统外观和感觉。
  • 您是将菜单项添加到 JMenu 中,还是对它们进行其他操作?

标签: java swing


【解决方案1】:

大多数布局管理器都会忽略最小和最大尺寸。这是一个有用的小技巧,可以强制在任何布局管理器中尊重这些尺寸:

private class MyMenuItem extends JMenuItem {
    @Override
    public Dimension getPreferredSize() {
        Dimension preferred = super.getPreferredSize();
        Dimension minimum = getMinimumSize();
        Dimension maximum = getMaximumSize();
        preferred.width = Math.min(Math.max(preferred.width, minimum.width), 
            maximum.width);
        preferred.height = Math.min(Math.max(preferred.height, minimum.height), 
            maximum.height);
        return preferred;
    }
}

这具有即使菜单项的内容发生变化也能正常工作的优点。您将需要添加构造函数以满足您的目的。

【讨论】:

    【解决方案2】:

    我替换了你的

    myMenuItem.setMinimumSize(new Dimension(250, 20));
    myMenuItem.setMaximumSize(new Dimension(350, 20));
    

    myMenuItem.setPreferredSize(new Dimension(250, 20));
    

    它使它成为您正在寻找的宽度。根据我的经验,摆动组件对首选尺寸的响应比对最小和最大尺寸的反应更好。

    编辑:

    在再次尝试之后,我认为这很有效.. 它归结为与您所拥有的大致相同,我认为无论使用什么字体(在您对字体进行硬编码的位置)它都可以使用。

    然而,它没有考虑菜单项的前导槽,但除非你的文本真的很长,否则它不应该有所作为..

     private JMenuItem newItem(String text, String iconPath) {
        JMenuItem myMenuItem;
        if (iconPath == null || iconPath.isEmpty()) {
            myMenuItem = new JMenuItem(text);
            myMenuItem.setPreferredSize(new Dimension(myMenuItem.getFontMetrics(myMenuItem.getFont()).stringWidth(text), 20));
        } else {
            ImageIcon icon = new ImageIcon(iconPath);
            myMenuItem = new JMenuItem(text, icon);
            myMenuItem.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        }
        return myMenuItem;
    }
    

    【讨论】:

      【解决方案3】:

      好的,所以我找到了一个解决方案,也许不是最好的解决方案,但它似乎有效。不过,我仍然有点担心 FontMetrics 声明。因此,如果有人有更好的解决方案,我会很高兴看到它。这是我的代码(Menu.MINWIDTH 设置为 200,Menu.MAXWIDTH 设置为 300):

       private JMenuItem newItem(String text, String iconPath) {
           JMenuItem myMenuItem;
           ImageIcon icon= null;
           int iconPixels = 0;
           if (iconPath == null || iconPath.isEmpty()) {
               myMenuItem = new JMenuItem(text);
           }
           else {
               icon = new ImageIcon(iconPath);
               myMenuItem = new JMenuItem(text, icon);
               iconPixels = icon.getIconWidth();
           }
      
           FontMetrics fontM = myMenuItem.getFontMetrics(new Font("Default", Font.PLAIN, 12));
           int stringPixels = fontM.stringWidth(text);
      
           int newWidth = stringPixels + iconPixels;
      
           newWidth = newWidth < Menu.MINWIDTH ? Menu.MINWIDTH : newWidth;
           newWidth = newWidth > Menu.MAXWIDTH ? Menu.MAXWIDTH : newWidth;
      
           myMenuItem.setPreferredSize(new Dimension(newWidth, 20)); 
           System.out.println(text + " - " + newWidth);
           return myMenuItem;
       }
      

      【讨论】:

      • 不使用字体度量,您可以只使用字体、文本和图标构造一个JLabel,并获得其首选大小。它应该与JMenuItem 的默认首选大小相同,或者足够接近。
      • 实际上——我只是注意到你在这里构建菜单项——你甚至不需要JLabel;您可以只使用菜单项自己的原始首选大小,如 Russ Hayward 的回答。
      【解决方案4】:

      如果我理解这个问题,我不确定您为什么需要使用 FontMetrics。只需在菜单项上调用 getPreferredSize()。如果首选尺寸小于您的最小值,则将首选尺寸重置为您的最小值。

      【讨论】:

      • 大小应根据项目文本的长度而有所不同。如果文本长于最小值,则菜单项应更改其宽度,但不得超过允许的最大值。
      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      相关资源
      最近更新 更多