日期:2014-05-20  浏览次数:20709 次

关于GridBagLayout的问题,请教!
如下代码示例GridBagLayout,却总不能正常显示,求指教!!!!
Java code
import java.awt.*;
import java.awt.event.*;

public class GridBagLayoutDemo extends Frame{
    Label l1,l2,l3,l4;
    TextField tf1,tf2,tf3;
    Button btn1,btn2;
    CheckboxGroup cbg;
    Checkbox cb1,cb2,cb3,cb4;
    GridBagLayout gb;
    GridBagConstraints gbc;
    public GridBagLayoutDemo(String title) {
        super(title);
        l1=new Label("Name:");
        l2=new Label("Password:");
        l3=new Label("Repeate:");
        l4=new Label("tujing:");
        tf1=new TextField(20);
        tf2=new TextField(20);
        tf3=new TextField(20);
        gb=new GridBagLayout();
        setLayout(gb);
        gbc=new GridBagConstraints();
        Panel p=new Panel();
        cbg=new CheckboxGroup();
        cb1=new Checkbox("Search",cbg,false);
        cb2=new Checkbox("Advertise",cbg,false);
        cb3=new Checkbox("Friends",cbg,false);
        cb4=new Checkbox("Others",cbg,false);
        p.add(cb1);
        p.add(cb2);
        p.add(cb3);
        p.add(cb4);
        btn1=new Button("Submit");
        btn2=new Button("Reset");
        Panel p1=new Panel();
        p1.add(btn1);
        p1.add(btn2);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        gbc.fill=GridBagConstraints.HORIZONTAL;
        addComponent(l1,0,0,1,1);
        addComponent(tf1,0,2,1,4);
        addComponent(l2,1,0,1,1);
        addComponent(tf2,1,2,1,4);
        addComponent(l3,2,0,1,1);
        addComponent(tf3,2,2,1,4);
        addComponent(l4,3,0,1,1);
        addComponent(p,3,2,1,1);
        addComponent(p1,4,2,1,5);
    }
    
    public void addComponent(Component c,int row,int col,int nrow,int ncol) {
        gbc.gridx=col;
        gbc.gridy=row;
        gbc.gridheight=ncol;
        gbc.gridwidth=nrow;
        gb.setConstraints(c, gbc);
        add(c);
    }
    
    public static void main(String[] args) {
        GridBagLayoutDemo mygb=new GridBagLayoutDemo("GridBagLayout");
        mygb.setSize(500,400);
        mygb.setVisible(true);

    }
}



------解决方案--------------------
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(l1, 0, 0, 1, 1);
addComponent(tf1, 0, 2, 1, 1);
addComponent(l2, 1, 0, 1, 1);
addComponent(tf2, 1, 2, 1, 1);
addComponent(l3, 2, 0, 1, 1);
addComponent(tf3, 2, 2, 1, 1);
addComponent(l4, 3, 0, 1, 1);
addComponent(p, 3, 2, 1, 1);
addComponent(p1, 4, 2, 1, 5);
------解决方案--------------------
gridheight、gridwidth颠倒了,高对应所占的行数、宽对应所占列数。

Java code
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GridBagLayoutDemo extends Frame{
    Label l1,l2,l3,l4;
    TextField tf1,tf2,tf3;
    Button btn1,btn2;
    CheckboxGroup cbg;
    Checkbox cb1,cb2,cb3,cb4;
    GridBagLayout gb;
    GridBagConstraints gbc;
    public GridBagLayoutDemo(String title) {
        super(title);
        l1=new Label("Name:");
        l2=new Label("Password:");
        l3=new Label("Repeate:");
        l4=new Label("tujing:");
        tf1=new TextField(20);
        tf2=new TextField(20);
        tf3=new TextField(20);
        gb=new GridBagLayout();
        setLayout(gb);
        gbc=new GridBagConstraints();
        Panel p=new Panel();
        cbg=new CheckboxGroup();
        cb1=new Checkbox("Search",cbg,false);
        cb2=new Checkbox("Advertise",cbg,false);
        cb3=new Checkbox("Friends",cbg,false);
        cb4=new Checkbox("Others",cbg,false);
        p.add(cb1);
        p.add(cb2);
        p.add(cb3);
        p.add(cb4);
        btn1=new Button("Submit");
        btn2=new Button("Reset");
        Panel p1=new Panel();
        p1.add(btn1);
        p1.add(btn2);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        gbc.fill=GridBagConstraints.HORIZONTAL;
        addComponent(l1,0,0,1,1);
        addComponent(tf1,0,2,1,4);
        addComponent(l2,1,0,1,1);
        addComponent(tf2,1,2,1,4);
        addComponent(l3,2,0,1,1);
        addComponent(tf3,2,2,1,4);
        addComponent(l4,3,0,1,1);
        addComponent(p,3,2,1,1);
        addComponent(p1,4,2,1,5);
    }
    
    public void addComponent(Component c,int row,int col,int nrow,int ncol) {
        gbc.gridx=col;
        gbc.gridy=row;
        
        // gridheight、gridwidth颠倒了
        // gbc.gridheight=ncol;
        // gbc.gridwidth=nrow;
        
        gbc.gridheight=nrow;
        gbc.gridwidth=ncol;
        
        gb.setConstraints(c, gbc);
        add(c);
    }
    
    public static void main(String[] args) {
        GridBagLayoutDemo mygb=new GridBagLayoutDemo("GridBagLayout");
        mygb.setSize(500,400);
        mygb.setVisible(true);

    }
}