首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用JComboBox的Swing JComboBox问题

不使用JComboBox的Swing JComboBox问题
EN

Stack Overflow用户
提问于 2011-09-28 10:32:28
回答 2查看 3.5K关注 0票数 5

下面是一个SSCCE:

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class BoxLayoutTest extends JFrame {

    public BoxLayoutTest(){
        JPanel main = new JPanel();
        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
        main.setBackground(Color.red);
        this.add(main);
        JPanel northPanel = new JPanel();

        JPanel middle = new JPanel();
        middle.setLayout(new BoxLayout(middle, BoxLayout.X_AXIS));
        middle.add(new JButton("FOO"));
        middle.add(Box.createHorizontalGlue());

        JPanel aPanel = new JPanel();
        aPanel.setBackground(Color.black);

            JComboBox b = new JComboBox();
    //b.setPreferredSize(new Dimension(100,16)); //uncomment this to see the layout I would like to achieve
    //b.setMaximumSize(new Dimension(100,16));
        //middle.add(b); //uncomment this line 

        middle.setBackground(Color.green);
        northPanel.setBackground(Color.blue);

        main.add(northPanel);
        main.add(middle);
        main.add(Box.createVerticalGlue());

        this.setSize(800,600);
        this.setResizable(true);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new BoxLayoutTest();
    }

}

当我不知道在组件上使用setXXXSize方法是错误的时候,我正在尝试重构我不久前编写的一些类。使用可调整大小的框架,我要达到的结果如下:

  1. northPanel应该保持在顶部,并将其大小相应地更改为帧大小修改(似乎工作得很好),
  2. ,绿色面板,我放置JButton时,应该保持JButton的最大尺寸,并保持在上面的蓝色面板下面(如果我只将JButtons放在该面板中,这会很好)。

如果我在绿色面板中放置一个JComboBox (尝试取消注释SSCCE中的行),就会出现问题。我想JComboBox没有指定的最大大小,所以它与框架一起延伸。在前面错误的代码版本中,我在JComboBox上使用了JComboBox方法来限制它的维度(尝试取消对setxxxSize方法的注释以查看它)。

我的问题是:

  1. 是否有可能在不调用setXXXSize()方法的情况下使用BoxLayout实现相同的结果?如果是,如何实现
  2. 是否还有其他的LayoutManager可以用来达到这个效果?

请把我引向正确的方向

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-28 10:47:22

JComboBox在报告一个无界的最大高度时行为不当(与JTextField一样):不应该显示超过一行。补救措施相同:子类和返回一个合理的高度。

代码语言:javascript
复制
        JComboBox b = new JComboBox() {

            /** 
             * @inherited <p>
             */
            @Override
            public Dimension getMaximumSize() {
                Dimension max = super.getMaximumSize();
                max.height = getPreferredSize().height;
                return max;
            }

        };

为了好玩,这里有一个使用MigLayout的片段(这是我目前最喜欢的:-)

代码语言:javascript
复制
    // two panels as placeholders
    JPanel northPanel = new JPanel();
    northPanel.setBackground(Color.YELLOW);
    JPanel southPanel = new JPanel();
    southPanel.setBackground(Color.YELLOW);
    // layout with two content columns
    LC layoutContraints = new LC().wrapAfter(2)
            .debug(1000);
    AC columnContraints = new AC()
    // first column pref, followed by greedy gap
            .size("pref").gap("push")
            // second
            .size("pref");
    // three rows, top/bottom growing, middle pref
    AC rowContraints = new AC()
       .grow().gap().size("pref").gap().grow();
    MigLayout layout = new MigLayout(layoutContraints, columnContraints,
            rowContraints);
    JPanel main = new JPanel(layout);
    main.setBackground(Color.WHITE);
    // add top spanning columns and growing
    main.add(northPanel, "spanx, grow");
    main.add(new JButton("FOO"));

    // well-behaved combo: max height == pref height
    JComboBox combo = new JComboBox() {

        @Override
        public Dimension getMaximumSize() {
            Dimension max = super.getMaximumSize();
            max.height = getPreferredSize().height;
            return max;
        }

    };
    // set a prototype to keep it from constantly adjusting
    combo.setPrototypeDisplayValue("somethingaslongasIwant");

    main.add(combo);
    // add top spanning columns and growing
    main.add(southPanel, "spanx, grow");
票数 9
EN

Stack Overflow用户

发布于 2011-09-28 10:56:06

我一直认为在jdk中使用布局管理器并不容易。它们要么过于简单和僵化,要么网格布局太麻烦了。相反,我开始使用jgoodies表单布局,从..。看一看。它非常简单易用。这里有一个链接:

http://www.jgoodies.com/freeware/forms/

一定要翻阅白皮书。

现在,google还为我们提供了一个用于表单布局的WYSISWG编辑器,作为eclipse的插件。这只会让生活变得容易得多。

http://code.google.com/javadevtools/wbpro/palettes/swing_palette.html

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7581846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档