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

时间:2010-10-01 20:37:48

标签: java swing gridbaglayout

我正在尝试使用GridBagLayout将标签放在两行中,但我希望某些标签跨越两行,而其他标签则放在彼此的顶部。由于GridBagLayout的{​​{1}}和weightx属性中的比例大小调整功能,我需要使用weighty。这是我正在寻找的布局:

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

问题是GridBagConstraints位于labelE下方。这是我的代码的布局部分:

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 个答案:

答案 0 :(得分:1)

您需要设置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();

希望有所帮助。

答案 1 :(得分:1)

您可以使用gridxgridy选择您在网格上的位置:

    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 :(得分:0)

我的建议是你可以在不使用GridBaglayout的情况下完成上述操作。它将为您节省大量时间并减少线路数量。对于您的特定问题,您可以实现使用简单网格布局的尝试。

创建一个大小为1x4的GridLayout。对于第三列,您可以使用另一个大小为2x1的GridLayout。把它们全部放在适当的位置,你应该达到你想要的效果。

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();
    }
}

答案 3 :(得分:0)

解决此问题的最简单方法(使用GridBag)是在JPanel中包含labelC和labelE。

解决它的更困难的方法是使用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 |          |
+---------------+--------------+----------+----------+