public class WBBorderLayout {
Frame f;
GridBagLayout gbl=new GridBagLayout();
public WBBorderLayout(){
f=new Frame();
f.setLayout(gbl);
int arr[][]={{0,0,3,0},
{1,0,0,0},{1,1,0,0},{1,2,0,0},
{2,0,3,0}}; Button b[]= new Button[5]; for(int i=0;i<arr.length;i++){
b[i]=new Button(" BUTTON "+i+" ");
add(f, b[i], arr[i]);
}
f.pack();
f.setVisible(true);
}
public void add(Frame f,Button b,int arr[]){
GridBagConstraints gc=new GridBagConstraints();
gc.gridx=arr[0];
gc.gridy=arr[1];
gc.gridwidth=arr[2];
gc.gridheight=arr[3];
f.add(b, gc);
}
public static void main(String[] args) {
WBBorderLayout mbl=new WBBorderLayout();
}
}
int arr[][] = { { 0, 0, 1, 1 }, { 1, 0, 1, 1 }, { 1, 1, 1, 1 },
{ 1, 2, 1, 1 }, { 2, 0, 1, 1 } };
gc.gridx=arr[0];
gc.gridy=arr[1];这两句应该是确定组件的位置gc.gridwidth=arr[2];
gc.gridheight=arr[3];这两句应该是确定组件的大小
所以组件的大小建议想2楼那样都设成1,1