GridBagLayout类似GridLayout但是比它更加灵活,更加复杂,因为它的每个单元的 宽度和高度的值是可以不相等的。GridBagLayout是利用GridBagLayoutConstraints的约束条件来对组件进行约束添加。关于这个GridBagLayoutConstraints有几个重要的参数,我们只要理解了这个,剩下的就好办了。
1:gridx,gridy:
这个表示的组件的横向索引和纵向索引,int类型,索引指向的值就是对应的单元格。
2:gridwidth,gridheight
这个表示的是一个组件占据的单元格的数目,int类型。首先,占据的数目大不意味着组件大,因为它周边可能是空白。其次,要注意的是,这里gridwidth,gridheight的值可以超过行最大组件数,列最大组件数,但是超过之后也没意义,超过之后系统会把它变成行或者列的最大值。例如GridBagLayout是M×N格子,那么Gridwidth最大有效值就是M,gridheight的最大有效值就是N。
默认值是1。
3:weightx,weighty
指定如何分布额外的水平,竖直空间。这个意思也很简单,权重越大,在窗口面积发生变化时,组件周围占据的空白就越大,就好比膨胀一样,原本组件1原本占据的单元格比组件2大,那么在膨胀后,它占据的空白就比组件2更多。
默认值是0
4:fill
这个指定怎么填充组件占据的空白,有HORIZENTAL,VERTICAL,BOTH,NULL。
默认值是NULL
5:anchor
这个主要是规定组件在它显示区域内应该以何种方式显示,说白了就是各种对齐的问题。
6:insets
这个是一个Inset类,指定组件与其上,左,底,右之间的距离,单位是像素,不是单元格。默认值是(0,0,0,0)
Insets(int top,int left,int bottom,int right)
7:ipadx,ipady
指定组件内部的宽,高,组件的大小等于组件的最小宽,高加上对应的ipadx,ipady。
默认值都是0.
/* * GirdBagLayoutDemo.java requires no other files. */ import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; public class GridBagLayouts extends JFrame{ private JButton btn1 = new JButton("Button1"); private JButton btn2 = new JButton("Button2"); private JButton btn3 = new JButton("Button3 what a fine day"); private JButton btn4 = new JButton("Button4 what a fine da"); private JButton btn5 = new JButton("Button5 what a fine d"); private JButton btn6 = new JButton("Button6 what a fines"); GridBagLayouts() { super(); initComponent(); } private void initComponent() { Container container = this.getContentPane(); container.setLayout(new GridBagLayout()); GridBagConstraints ctr1 = new GridBagConstraints(); ctr1.gridx = 0; ctr1.gridy = 0; ctr1.weighty = 3; //ctr1.weightx = 1; ctr1.gridwidth = 4; ctr1.gridheight = 1; //ctr1.anchor = GridBagConstraints.WEST; ctr1.fill = GridBagConstraints.HORIZONTAL; container.add(btn1,ctr1); GridBagConstraints ctr2 = new GridBagConstraints(); ctr2.gridx = 0; ctr2.gridy = 1; ctr2.weighty = 2; //ctr2.weightx = 2; ctr2.gridwidth = 2; ctr2.gridheight = 1; //ctr2.anchor = GridBagConstraints.WEST; ctr2.fill = GridBagConstraints.HORIZONTAL; container.add(btn2,ctr2); GridBagConstraints ctr3 = new GridBagConstraints(); ctr3.gridx= 2; ctr3.gridy = 1; ctr3.weighty = 2; //ctr3.weightx = 1; ctr3.gridwidth = 1; ctr3.gridheight = 1; //ctr3.anchor = GridBagConstraints.WEST; ctr3.fill = GridBagConstraints.HORIZONTAL; ctr3.insets = new Insets(0,20,0,0);//top,left,bottom,right ctr3.ipady = 40; container.add(btn3,ctr3); GridBagConstraints ctr4 = new GridBagConstraints(); ctr4.gridx = 0; ctr4.gridy = 2; ctr4.weighty = 1; //ctr4.weightx = 3; ctr4.gridwidth = 1; ctr4.gridheight = 1; //ctr4.anchor = GridBagConstraints.WEST; ctr4.fill = GridBagConstraints.HORIZONTAL; container.add(btn4,ctr4); GridBagConstraints ctr5 = new GridBagConstraints(); ctr5.gridx = 1; ctr5.gridy = 2; ctr5.weighty = 1; //ctr5.weightx = 2; ctr5.gridwidth = 1; ctr5.gridheight = 1; //ctr5.anchor = GridBagConstraints.WEST; ctr5.fill = GridBagConstraints.HORIZONTAL; container.add(btn5,ctr5); GridBagConstraints ctr6 = new GridBagConstraints(); ctr6.gridx = 2; ctr6.gridy = 2; ctr6.weighty = 1; //ctr6.weightx = 1; ctr6.gridwidth = 2; ctr6.gridheight = 1; ctr6.fill = GridBagConstraints.HORIZONTAL; container.add(btn6,ctr6); } public static void main(String[] args) { GridBagLayouts frame = new GridBagLayouts(); frame.pack(); frame.setVisible(true); } }
从图中可以看出
1:对于这样的网格Layout,我们可以忽略一些网格组件的设置,并不是每个网格都必须有组件
2:对于权重来说,有纵向比较,横向比较。在比较时,根据这一列(行)不同组件权重的对应大小,然后在窗口扩张中按照对应的权重来增加间距。如上图,button1的纵向权重最大,那么窗口变大时,它周围的空白就越多。
3:inset单位是像素,表示的是组件之间的间距
4:ipadx,ipady表示的 是组件内部的距离。