- Java code
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 } };
------解决方案--------------------
- Java code
gc.gridx=arr[0]; gc.gridy=arr[1];