读书人

模拟BorderLayOut时只显示了一个组件?

发布时间: 2012-05-05 17:21:10 作者: rapoo

模拟BorderLayOut时只显示了一个组件?

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]; 

读书人网 >J2SE开发

热点推荐