练习GUI时,自己写了一个小程序,设计一个网格包布局的窗口,代码如下:- import java.awt.*;
- import java.awt.event.*;
- class GuiDemo4 extends Frame
- {
- Label l1,l2,l3,l4;
- TextField tf1,tf2,tf3;
- Button btn1,btn2;
- CheckboxGroup cbg;
- Checkbox cb1,cb2,cb3,cb4;
- GridBagLayout gbl;
- GridBagConstraints gbc;
- public GuiDemo4(String title)
- {
- super(title);
- l1 = new Label("用户名:");
- l2 = new Label("密码:");
- l3 = new Label("确认密码:");
- l4 = new Label("获取途径:");
- tf1 = new TextField(20);
- tf2 = new TextField(20);
- tf3 = new TextField(20);
- gbl = new GridBagLayout();
- setLayout(gbl);
- gbc = new GridBagConstraints();
- Panel p = new Panel();
- cbg = new CheckboxGroup();
- cb1 = new Checkbox("搜索",cbg,false);
- cb2 = new Checkbox("广告",cbg,false);
- cb3 = new Checkbox("朋友",cbg,false);
- cb4 = new Checkbox("其他",cbg,false);
- p.add(cb1);
- p.add(cb2);
- p.add(cb3);
- p.add(cb4);
- btn1 = new Button("提交");
- btn2 = new Button("重填");
- Panel p2 = new Panel();
- p2.add(btn1);
- p2.add(btn2);
-
- addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- 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,4,0,1,1);
- addComponent(p,4,2,1,1);
- addComponent(p2,5,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;
- gbl.setConstraints(c,gbc);
- add(c);
- }
- public static void main(String []args)
- {
- GuiDemo4 f = new GuiDemo4("网格包布局管理");
- f.setSize(300,200);
- f.setVisible(true);
- }
- }
复制代码 运行结果如下:
我怎么改文本框就是不上去,请大家给找找原因!{:soso_e183:}
|