www.updatesofts.com
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(cb1,2,1,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(cb2,2,2,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(cb3,3,1,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(cb4,3,2,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(tf,4,0,1,3);
}
public void addComponent(Component c, int row, int col, int nrow, int ncol)
{
gbc.gridx=col;
gbc.gridy=row;
gbc.gridwidth=ncol;
gbc.gridheight=ncol;
gb.setConstraints(c,gbc);
add(c);
}
public static void main(String args[])
{
Gbltest t=new Gbltest(“GridBag Layout”);
t.setSize(300,200);
t.show();
}
}
Khi một container bị thay đổi kích thước và khi khoảng trắng phụ tồn tại, các thành phần
có chiều rộng lớn hơn sẽ chiếm giữ nhiều khoảng trống hơn là các thành phần có giá trị về
chiều rộng nhỏ hơn.
Kết xuất của chương trình được chỉ ra ở hình 5.15