GUI——网格布局管理器

GridBagLayout(网格布局管理器)
特点:灵活,复杂。
这个布局管理器和GridLayout相似,看名字也可以看的出来,就差一个Bag.但是他允许网格中的组件大小各不相同,而且允许一个组件跨越一个或者多个网格。
网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。

为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。可以通过设置一个或多个实例变量来自定义 GridBagConstraints 对象:

GridBagConstraints.gridx、GridBagConstraints.gridy
指定包含组件显示区域的前导角的单元,在此显示区域中,位于网格原点的单元地址是 gridx = 0、gridy = 0。对于水平的从左到右的布局,组件的前导角是其左上角。对于水平的从右到左的布局,组件的前导角是其右上角。使用 GridBagConstraints.RELATIVE(默认值)指定将组件置于添加此组件前刚刚添加到容器组件的后面(沿 gridx 的 X 轴或 gridy 的 Y 轴)。
GridBagConstraints.gridwidth、GridBagConstraints.gridheight
指定组件的显示区域中行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。使用 GridBagConstraints.REMAINDER 指定组件的显示区域为从 gridx 到该行(针对 gridwidth)中的最后一个单元,或者从 gridy 到该列(针对 gridheight)中的最后一个单元。 使用 GridBagConstraints.RELATIVE 指定组件的显示区域为从 gridx 到其所在行(针对 gridwidth)的倒数第二个单元,或者从 gridy 到其所在列(针对 gridheight)的倒数第二个单元。
GridBagConstraints.fill
当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。可能的值为 GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和 GridBagConstraints.BOTH(使组件完全填满其显示区域)。
GridBagConstraints.ipadx、GridBagConstraints.ipady
指定布局中组件的内部填充,对组件最小大小的添加量。组件的宽度至少为其最小宽度加上 ipadx 像素。类似地,组件的高度至少为其最小高度加上 ipady 像素。
GridBagConstraints.insets
指定组件的外部填充,组件与其显示区域边缘之间间距的最小量。
`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.show();
 }

}
`在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值