Java中的Gridbag布局 [英] Gridbag Layout in java

查看:184
本文介绍了Java中的Gridbag布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须有问题地编辑网格袋布局,并且结果很奇怪.
预期:
| A | | B |
| -| | C |
| D | | -|

I have to edit a gridbag layout problematically and i am having weird results.
expected:
| A | | B |
| -- | | C |
| D | | -- |

结果:
| A | | B |
| D | | C |

Results:
| A | | B |
| D | | C |

A和C的高度为2 这就是gridbag的工作方式吗?反正有强迫吗?

A and C have a height of 2 Is this just how gridbag works? is there anyway to force it?

我的程序有两列,行数为n.它支持2的宽度,但只有在第一个列中时才生效.如果在第二行中,它的行为就好像宽度是1.

My program has two columns and n number of rows. It supports a width of 2 but it only comes into effect when it is in the first col. If in the 2nd row it acts as though the width is 1.

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(7, 7, 7, 7);
gbc.weightx = 0.5;
gbc.anchor = GridBagConstraints.NORTH;

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(7, 7, 7, 7);
gbc.weightx = 0.5;
gbc.anchor = GridBagConstraints.NORTH;

由用户添加组件,并且用户确定widthheight. gridxgridy的值由添加和放置哪些其他组件确定.

the components are added by the user and the user determines the width and height. the gridxand gridy values are determined by what other components are added and placed.

gridbag布局可以很好地发挥作用
* _ _
| A | B |
| _ | C |
当C的高度为2时,它似乎并不喜欢它.

The gridbag layout works fine for say
*_ _
|A|B|
|_|C|
it just doesn't seem to like it when C has a height of 2

推荐答案

现在,问题已得到澄清:

Now that the question has been clarified:

protected static final Insets entryInsets = new Insets(0, 10, 4, 10);
protected static final Insets spaceInsets = new Insets(10, 10, 4, 10);

protected void createPartControl() {
    panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    int gridy = 0;
    gridy = createTextFields(gridy);
}

protected int createTextFields(int gridy) {
    JLabel a = new JLabel("A");
    a.setHorizontalAlignment(SwingConstants.LEFT);
    addComponent(panel, a, 0, gridy, 1, 2, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    JLabel b = new JLabel("B");
    b.setHorizontalAlignment(SwingConstants.LEFT);
    addComponent(panel, b, 1, gridy++, 1, 1, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    JLabel c = new JLabel("C");
    c.setHorizontalAlignment(SwingConstants.LEFT);
    addComponent(panel, c, 1, gridy++, 1, 1, entryInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    JLabel d = new JLabel("D");
    d.setHorizontalAlignment(SwingConstants.LEFT);
    addComponent(panel, d, 0, gridy++, 2, 1, entryInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    return gridy;
}

protected void addComponent(Container container, Component component,
        int gridx, int gridy, int gridwidth, int gridheight, 
        Insets insets, int anchor, int fill) {
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
            gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
    container.add(component, gbc);
}

这篇关于Java中的Gridbag布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆