GridBagLayout给了我意想不到的行为

时间:2015-11-26 07:58:01

标签: java swing gridbaglayout

我有以下代码,我需要创建一个网格,15个项目分为两列,在第6行,只有一个项目将取代2个项目。

我希望看到两个具有相同水平宽度的列,但我得到的是不同的,第一列占整个大小的三分之一,而另一列占三分之二,为什么会发生这种情况?

Insets ins = new Insets(0,0,0,0);
GridBagConstraints SymbolTextc           = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints SymbolLabelc          = new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0); 
GridBagConstraints ComapnyTextc          = new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints ComapnyLabelc         = new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PriceTextc            = new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PriceLabelc           = new GridBagConstraints(1, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints MaxQtyTextc           = new GridBagConstraints(0, 3, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints MaxQtyLabelc          = new GridBagConstraints(1, 3, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PurchasePowerTextc    = new GridBagConstraints(0, 4, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PurchasePowerLabelc   = new GridBagConstraints(1, 4, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints NewCoverageTextc      = new GridBagConstraints(0, 5, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints NewCoverageLabelc     = new GridBagConstraints(1, 5, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints NoteLabelc            = new GridBagConstraints(0, 6, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints CancelButtonc         = new GridBagConstraints(0, 7, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,ins,0,0);
GridBagConstraints OkButtonc             = new GridBagConstraints(1, 7, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,ins,0,0);

2 个答案:

答案 0 :(得分:2)

所以,我把你的代码放到一个可运行的例子中并得到......

GridBagLayout

愿意进一步指出你不喜欢的东西,可能以runnable example和屏幕拍摄的形式(你得到的和你期望的)

答案 1 :(得分:0)

以编程方式处理您的 x y 值,如下所示:

int gridcol1 = 0;
int gridcol2 = 1;


Insets ins = new Insets(0,0,0,0);

int y=0;
GridBagConstraints SymbolTextc           = new GridBagConstraints(gridcol1, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints SymbolLabelc          = new GridBagConstraints(gridcol2, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0); 

y++;

GridBagConstraints ComapnyTextc          = new GridBagConstraints(gridcol1, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints ComapnyLabelc         = new GridBagConstraints(gridcol2, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);

y++;

GridBagConstraints PriceTextc            = new GridBagConstraints(gridcol1, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PriceLabelc           = new GridBagConstraints(gridcol2, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);

y++;

GridBagConstraints MaxQtyTextc           = new GridBagConstraints(gridcol1, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints MaxQtyLabelc          = new GridBagConstraints(gridcol2, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);

y++;

GridBagConstraints PurchasePowerTextc    = new GridBagConstraints(gridcol1, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);
GridBagConstraints PurchasePowerLabelc   = new GridBagConstraints(gridcol2, y, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,ins,0,0);

如果按照我的描述进行操作,那么您将看到问题所在。