computer 版 (精华区)

发信人: arbo (arbo), 信区: program
标  题: java 笔记9
发信站: 听涛站 (2001年09月17日10:51:01 星期一), 站内信件

发信人: airforce1 (五湖废人), 信区: Java
标  题: java 笔记9
发信站: BBS 水木清华站 (Sun Apr  2 16:16:11 2000)

还有一点时间,老师讲 附录的gridbaglayout manager
private GridBagLayout guiLayout;
private GridBagConstraints guiConstranins;
...
guiLayout=new GridBagLayout();
setLayout(guiLayout);
guiConstrains = new GridBagConstrains();
guiConstrains.anchor= GridBagConstrains.CENTER.

可以用网格的方式来布局,比较方便控件摆放
GridBaglayout的一些实例变量
gridx, gridy,确定在哪个cell, 位置
gridwitdth, gridheight , 宽和高
fill,当控件加入的时候,怎样填充,是充满还是不变

GridBagConstraints 实例变量
加入一个按扭。
b=new GridBagLayout;
a=new GridBagConstrains();
b.setconstrains(button,a);

看例子
不过我现在还是不是特别理解
import java.awt.*;

public class GB {
        private Frame f;
        public static void main(String args[]) {
                GB that = new GB();
                that.go();
        }

        public void go() {
                f = new Frame("GridBag Layout");

                GridBagLayout g=new GridBagLayout();
                GridBagConstraints c=new GridBagConstraints();
                f.setLayout(g);

                c.anchor=GridBagConstraints.EAST;
                Button b1=new Button("b1");
                g.setConstraints(b1,c);
                f.add(b1);


                c.anchor=GridBagConstraints.NORTHEAST;
                Button b2=new Button("b2");
                g.setConstraints(b2,c);
                f.add(b2);


                c.anchor=GridBagConstraints.NORTHWEST;
                Button b3=new Button("b3");
                g.setConstraints(b3,c);
                f.add(b3);

                f.setSize (200, 200);
                f.setVisible(true);
        }
}
另一个例子
import java.awt.*;

public class gb4 {
        private Frame f;
        public static void main(String args[]) {
                gb4 that = new gb4();
                that.go();
        }

        public void go() {
                f = new Frame("GridBag Layout");

                GridBagLayout g=new GridBagLayout();
                f.setLayout(g);
                GridBagConstraints c=new GridBagConstraints();


               //     1. 单元位置和大小 Cell pos and size
                c.gridx=0;                c.gridy=0;
                c.gridwidth=4; c.gridheight=2;
               //     2. 上下左右填满  fill both
                c.fill=GridBagConstraints.BOTH;

               //     4. 间距 3
                      c.insets=new Insets(10,30,50,70);
               //     5. 伸缩比例  上下左右伸缩
                c.weightx=1;
                c.weighty=1;
                Button b1=new Button("b1");
                g.setConstraints(b1,c);
                f.add(b1);



               //     1. 单元位置和大小 Cell pos and size
                c.gridx=2;               //  c.gridy=0; use above setting
                c.gridwidth=1;            c.gridheight=1;
              //      2.上下左右填满  fill both  (use above setting)
              //  c.fill=GridBagConstraints.NONE;
              //      5. 伸缩比例  上下占1  左右 不伸缩
                c.weightx=0;
                c.weighty=0;

                Button b2=new Button("b2");
                g.setConstraints(b2,c);
                f.add(b2);

               //     1. 单元位置和大小 Cell pos and size
               c.gridy=1;   // c.gridx=2; use above setting

              //     5. 伸缩比例  左右占1  上下不伸缩
               c.weightx=1;
               c.weighty=0;
               Button b3=new Button("b3");
               g.setConstraints(b3,c);
               f.add(b3);


               //     1. 单元位置和大小 Cell pos and size
                c.gridy=2;
                Button b4=new Button("b4");
                g.setConstraints(b4,c);
                f.add(b4);

                f.pack();
                f.setVisible(true);
        }
}


--
--
※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家] 
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.561毫秒