代码之家  ›  专栏  ›  技术社区  ›  Erick Robertson

如何使用GridBagLayout将不同高度的标签放置在两行中?

  •  2
  • Erick Robertson  · 技术社区  · 15 年前

    我试着用 GridBagLayout 将标签放置在两行中,但我希望一些标签跨越两行,而另一些标签放置在彼此的顶部。我需要使用 网格包布局 因为 weightx weighty GridBagConstraints

    +----------+----------+----------+----------+
    |          |          |  labelC  |          |
    |  labelA  |  labelB  |----------|  labelD  |
    |          |          |  labelE  |          |
    +----------+----------+----------+----------+
    

    问题是 labelE LabelA . 以下是我代码的布局部分:

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    this.setLayout(gridBag);
    c.fill = GridBagConstraints.BOTH;
    
    c.gridwidth = 1;
    c.gridheight = 2;
    c.weighty = PANEL_HEIGHT;
    gridbag.setConstraints(labelA, c);
    this.add(labelA);
    
    gridbag.setConstraints(labelB, c);
    this.add(labelB);
    
    c.gridheight = 1;
    c.weighty = TOPROW_HEIGHT;
    gridbag.setConstraints(labelC, c);
    this.add(labelC);
    
    c.gridheight = 2;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = PANEL_HEIGHT;
    gridbag.setConstraints(labelD, c);
    this.add(labelD);
    
    c.gridheight = 1;
    c.gridwidth = 1;
    c.weighty = BOTROW_HEIGHT;
    gridbag.setConstraints(labelE, c);
    this.add(labelE);
    
    this.validate();
    

    你知道我遗漏了什么吗?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Colin Hebert    15 年前

    你可以用 gridx gridy

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
    
        this.setLayout(gridbag);
    
        c.fill = GridBagConstraints.BOTH;
    
        c.gridwidth = 1;
        c.gridheight = 2;
        c.gridx = 0;
        c.gridy = 0;
        c.weighty = PANEL_HEIGHT;
        gridbag.setConstraints(labelA, c);
        this.add(labelA);
    
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(labelB, c);
        this.add(labelB);
    
        c.gridx = 2;
        c.gridy = 0;
        c.gridheight = 1;
        c.weighty = TOPROW_HEIGHT;
        gridbag.setConstraints(labelC, c);
        this.add(labelC);
    
    
        c.gridx = 3;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weighty = PANEL_HEIGHT;
        gridbag.setConstraints(labelD, c);
        this.add(labelD);
    
        c.gridx = 2;
        c.gridy = 1;
        c.fill = GridBagConstraints.VERTICAL;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weighty = BOTROW_HEIGHT;
        gridbag.setConstraints(labelE, c);
        this.add(labelE);
    
        this.validate();
    
        2
  •  1
  •   Erick Robertson    15 年前

    您需要设置gridX和gridY:

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    this.setLayout(gridBag);
    c.fill = GridBagConstraints.BOTH;
    
    c.gridwidth = 1;
    c.gridheight = 2;
    c.gridx = 0;
    c.gridy = 0;
    c.weighty = PANEL_HEIGHT;
    gridbag.setConstraints(labelA, c);
    this.add(labelA);
    
    c.gridx = 1;
    c.gridy = 0;
    gridbag.setConstraints(labelB, c);
    this.add(labelB);
    
    c.gridx = 2;
    c.gridy = 0;
    c.gridheight = 1;
    c.weighty = TOPROW_HEIGHT;
    gridbag.setConstraints(labelC, c);
    this.add(labelC);
    
    c.gridx = 3;
    c.gridy = 0;
    c.gridheight = 2;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = PANEL_HEIGHT;
    gridbag.setConstraints(labelD, c);
    this.add(labelD);
    
    c.gridx = 2;
    c.gridy = 1;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.weighty = BOTROW_HEIGHT;
    gridbag.setConstraints(labelE, c);
    this.add(labelE);
    
    this.validate();
    

        3
  •  0
  •   bragboy    15 年前

    我的建议是你可以不使用GridBaglayout来完成上面的工作。它将为您节省大量时间并减少行数。对于您的特定问题,您可以使用简单的网格布局来实现所要实现的目标。

    http://img823.imageshack.us/img823/5643/screenshot20101002at225.png

    import java.awt.Component;
    import java.awt.GridLayout;
    
    import javax.swing.*;
    
    public class LayoutTest extends JFrame {
    
        public LayoutTest(){
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.setSize(300,75);
            this.add(getCustomPanel());
            this.setVisible(true);
        }
    
        private Component getCustomPanel() {
            JPanel pnl = new JPanel();
            GridLayout gridLayout = new GridLayout(1,4);
            pnl.setLayout(gridLayout);
    
            JPanel subPanel = new JPanel();
            GridLayout subLayout = new GridLayout(2,1);
            subPanel.setLayout(subLayout);
    
            subPanel.add(new JLabel("labelC"));
            subPanel.add(new JLabel("labelE"));
    
            pnl.add(new JLabel("labelA"));
            pnl.add(new JLabel("labelB"));
            pnl.add(subPanel);
            pnl.add(new JLabel("labelD"));
    
            return pnl;
        }
    
        public static void main(String[] args) {
            new LayoutTest();
        }
    }
    
        4
  •  0
  •   Codemwnci    15 年前

    更难的解决方法是使用gridx、gridy和gridheight约束。所以,你应该

    +---------------+--------------+----------+----------+
    |  gridy=0      | gridy=0      |  gridy=0 |  gridy=0 |
    |               |              |  gridx=2 |  gridx=3 |
    |  gridx=0      | gridx=1      |----------|          |
    |  gridheight=2 | gridheight=2 |  gridy=1 |          |        
    |               |              |  gridx=2 |          |
    +---------------+--------------+----------+----------+