2

我正在尝试将GridBagLayout标签放置在两行中,但我希望一些标签跨越两行,而另一些标签则放置在彼此之上。由于和的属性中GridBagLayout的比例尺寸功能,我需要使用。这是我正在寻找的布局:weightxweightyGridBagConstraints

+----------+----------+----------+----------+
| | | 标签C | |
| 标签A | 标签B |---------| 标签D |
| | | 标签E | |
+----------+----------+----------+----------+

问题是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

4 回答 4

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

希望有帮助。

于 2010-10-01T20:51:09.747 回答
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();
于 2010-10-01T20:51:10.070 回答
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();
    }
}
于 2010-10-01T20:46:44.747 回答
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 |          |
+---------------+--------------+----------+----------+
于 2010-10-01T20:55:13.020 回答