zl程序教程

您现在的位置是:首页 >  其它

当前栏目

gridbaglayout布局_gridsearch

布局
2023-06-13 09:13:36 时间

大家好,又见面了,我是你们的朋友全栈君。

自己想做个小程序,却在布局上犯了难,使用FlowLayout和BorderLayout这些功能不够强大,使用GridBagLayout却不会,只好求助于文档了。

文档对这个布局管理器介绍很详细,但是最痛苦的是英文。不过幸好它有实例,经过在网上查阅和推敲实例,终于对GridBagLayout的使用有了一个成型的了解,拿出来与大家分享。

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,第五行封口。 要理解GridBagLayout,最好从例子的理解开始,呵呵。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。