Java中GridBagLayout布局

来源:互联网 发布:linux vi使用 编辑:程序博客网 时间:2024/03/28 18:57

GridBagLayout是一个灵活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干个参数,解释如下:

gridx/gridy:组件的横纵坐标

gridwidth:组件所占列数,也是组件的宽度

gridheight:组件所占行数,也是组件的高度

fill:当组件在其格内而不能撑满其格时,通过fill的值来设定填充方式,有四个值

ipadx: 组件间的横向间距

ipady:组件间的纵向间距

insets:当组件不能填满其格时,通过insets来指定四周(即上下左右)所留空隙

anchor:同样是当组件不能填满其格时,通过anchor来设置组件的位置,anchor有两种值,绝对和相对的值分别有         若干个,文档中有,可自行查看

weightx:行的权重,通过这个属性来决定如何分配行的剩余空间

weighty:列的权重,通过这个属性来决定如何分配列的剩余空间

还是文档实用,用例子来说话

import java.awt.*; import java.util.*; import java.applet.Applet;  public class GridBagEx1 extends Applet {      protected void makebutton(String name,                               GridBagLayout gridbag,                               GridBagConstraints c) {         Button button = new Button(name);         gridbag.setConstraints(button, c);         add(button);     }      public void init() {         GridBagLayout gridbag = new GridBagLayout();         GridBagConstraints c = new GridBagConstraints();          setFont(new Font("SansSerif", Font.PLAIN, 14));         setLayout(gridbag);          c.fill = GridBagConstraints.BOTH;         c.weightx = 1.0;         makebutton("Button1", gridbag, c);         makebutton("Button2", gridbag, c);         makebutton("Button3", gridbag, c);        c.gridwidth = GridBagConstraints.REMAINDER; //end row         makebutton("Button4", gridbag, c);          c.weightx = 0.0;   //reset to the default         makebutton("Button5", gridbag, c); //another row     c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row         makebutton("Button6", gridbag, c);     c.gridwidth = GridBagConstraints.REMAINDER; //end row         makebutton("Button7", gridbag, c);     c.gridwidth = 1;     //reset to the default    c.gridheight = 2;         c.weighty = 1.0;         makebutton("Button8", gridbag, c);          c.weighty = 0.0;   //reset to the default    c.gridwidth = GridBagConstraints.REMAINDER; //end row    c.gridheight = 1;   //reset to the default         makebutton("Button9", gridbag, c);         makebutton("Button10", gridbag, c);          setSize(300, 100);     }      public static void main(String args[]) {    Frame f = new Frame("GridBag Layout Example");    GridBagEx1 ex1 = new GridBagEx1();     ex1.init();     f.add("Center", ex1);    f.pack();    f.setSize(f.getPreferredSize());    f.setVisible(true);     } }


可以自行运行,查看其结果

文档对其各个按钮的参数设定解释如下:

Button1, Button2, Button3: weightx = 1.0 
Button4: weightx = 1.0, gridwidth = GridBagConstraints.REMAINDER 
Button5: gridwidth = GridBagConstraints.REMAINDER 
Button6: gridwidth = GridBagConstraints.RELATIVE 
Button7: gridwidth = GridBagConstraints.REMAINDER 
Button8: gridheight = 2, weighty = 1.0 
Button9, Button 10: gridwidth = GridBagConstraints.REMAINDER

对照着程序和运行结果,还有其参数设定,我的理解如下:

第一行:第一行之所以有四个按钮,关键点在于,weightx=1.0,这样就可以在前边的按钮后继续加入按钮,而button4成为行尾是因为其gridwidth = GridBagConstraints.REMAINDER,这句话就设定它是行的末尾。 
第二行:既然第一行都有末尾了,那么再加入按钮的话,必定是另起一行了(这个道理)。此时加入了button5,而button5又被设定为了本行的最后一个(gridwidth = GridBagConstraints.REMAINDER),加之它又是第二行的第一个按钮,所以第二行只有一个按钮,就是button5。 
第三行:button6不可避免的成为了第一个按钮,它被设定了gridwidth = GridBagConstraints.RELATIVE,表明button6要紧挨它前边的那个按钮和最后的那个按钮,也就是说它一定是倒数第二个按钮(为最后一个按钮的出现做好了准备)。button7出现了,由于有gridwidth = GridBagConstraints.REMAINDER,它就为第三行封了口。第三行结束。 
第四行:这一行有一个特殊的按钮button8,它的设定为gridheight = 2, weighty = 1.0,即它占用两行一列(其实这个一列和两行都是相对的)。这一行还没封口,所以后面来的button9加在了这一行,因为它gridwidth = GridBagConstraints.REMAINDER,所以第四行封口。 
第五行:这一行button8已经占据了第一个的位置(因为button8的gridheight=2),所以后来的button10加在第二,同样由于gridwidth = GridBagConstraints.REMAINDER,第五行封口。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 准予迁入证明过期了怎么办 粉底液容易脱妆怎么办 家人进了火疗传销怎么办 自发热护膝洗了怎么办 用气垫bb卡粉怎么办 贴药膏后皮肤过敏红肿怎么办 贴完膏药皮肤痒怎么办 猕猴桃吃的嘴疼怎么办 摩拜单车怎么办月卡 出国忘了带护照怎么办 雅漾喷雾失压了怎么办 洗衣服时衣服粘上卫生纸怎么办 一晚上卫生巾都是满的怎么办 宝宝头上痱子痒怎么办 短裤里的宽松紧带拧了怎么办 肉色内衣被染黑色了怎么办 安全裤总往上缩怎么办 夏天穿裙子膝盖怕凉怎么办 夏天穿裙子膝盖冷怎么办 天凉嗓子痒咳嗽怎么办 棉服里面跑毛怎么办 棉衣里面的棉一块一块的怎么办 穿姨妈巾悟出痱子怎么办 穿裙子上衣太长了怎么办 微信封号了零钱怎么办 快递加盟商欠我工资怎么办 加盟费交了以后怎么办 加盟总部违约加盟商该怎么办 自行车锁钥匙丢了怎么办 假体隆胸肿胀痛怎么办 恶露60天不干净怎么办 剖腹产俩月恶露不干净怎么办 剖腹产恶露一个多月还没干净怎么办 小月子全身流虚汗怎么办 生完孩子严重便秘怎么办啊 产后第5天恶露少怎么办 有恶露排不出来怎么办 刚生完小孩便秘怎么办 产后15天有鲜血怎么办 恶露未净同房了怎么办 药流2月恶露不尽怎么办