GridBagLayout

GridBagLayout的使用:

GridBagLayout是java中最有弹性但也是最复杂的一种版面管理器。它只有一种构造函数,但必须配合GridBagConstraints才能达到设置的效果。 GridBagLayout的类层次结构图:

java.lang.Object
     --java.awt.GridBagLayout
构造函数:

GirdBagLayout()建立一个新的GridBagLayout管理器。 
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty, int anchor,int fill, Insets insets,int ipadx,int ipady)建立一个新的GridBagConstraints对象 ,并指定其参数的值。

参数说明:

gridx,gridy:设置组件的位置,gridx设置为GridBagConstraints.RELATIVE代表此组件位于之前所加入组件的右边。 若将gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下面。建议定义出gridx,gridy的位置,以便以后维护程序。表示放在几行几列,gridx=0,gridy=0时放在0行0列。

gridwidth,gridheight:用来设置组件所占的单位长度与高度,默认值皆为1。你可以使用GridBagConstraints.REMAINDER常 量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间。

weightx,weighty:用来设置窗口变大时,各组件跟着变大的比例,当数字越大,表示组件能得到更多的空间,默认值皆为0。

anchor:当组件空间大于组件本身时,要将组件置于何处,有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、 WEST、NORTHWEST可供选择。

insets:设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,默认为(0,0,0,0).

ipadx,ipady:设置组件内的间距,默认值为0。

我们以前提过,GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我们将GridBagConstraints的参数都设置 好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。

例子: GridBagLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridBagLayoutDemo {
	public GridBagLayoutDemo() {
		JButton b;
		GridBagConstraints c;
		int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
		double weightx, weighty;
		Insets inset;

		JFrame f = new JFrame();

		GridBagLayout gridbag = new GridBagLayout();
		Container contentPane = f.getContentPane();
		contentPane.setLayout(gridbag);

		b = new JButton("first");
		gridx = 0;
		gridy = 0;
		gridwidth = 1;
		gridheight = 1;
		weightx = 10;
		weighty = 1;
		anchor = GridBagConstraints.CENTER;
		fill = GridBagConstraints.HORIZONTAL;
		inset = new Insets(0, 0, 0, 0);
		ipadx = 0;
		ipady = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b, c);
		contentPane.add(b);

		b = new JButton("second");
		gridx = 1;
		gridy = 0;
		gridwidth = 2;
		gridheight = 1;
		weightx = 1;
		weighty = 1;
		anchor = GridBagConstraints.CENTER;
		fill = GridBagConstraints.HORIZONTAL;
		inset = new Insets(0, 0, 0, 0);
		ipadx = 50;
		ipady = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b, c);
		contentPane.add(b);

		b = new JButton("third");
		gridx = 0;
		gridy = 1;
		gridwidth = 1;
		gridheight = 1;
		weightx = 1;
		weighty = 1;
		anchor = GridBagConstraints.CENTER;
		fill = GridBagConstraints.HORIZONTAL;
		inset = new Insets(0, 0, 0, 0);
		ipadx = 0;
		ipady = 50;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b, c);
		contentPane.add(b);

		b = new JButton("fourth");
		gridx = 1;
		gridy = 1;
		gridwidth = 1;
		gridheight = 1;
		weightx = 1;
		weighty = 1;
		anchor = GridBagConstraints.CENTER;
		fill = GridBagConstraints.HORIZONTAL;
		inset = new Insets(0, 0, 0, 0);
		ipadx = 0;
		ipady = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b, c);
		contentPane.add(b);

		b = new JButton("This is the last button");
		gridx = 2;
		gridy = 1;
		gridwidth = 1;
		gridheight = 2;
		weightx = 1;
		weighty = 1;
		anchor = GridBagConstraints.CENTER;
		fill = GridBagConstraints.HORIZONTAL;
		inset = new Insets(0, 0, 0, 0);
		ipadx = 0;
		ipady = 50;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b, c);
		contentPane.add(b);

		f.setTitle("GridBagLayout");
		f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new GridBagLayoutDemo();
	}
} 

猜你喜欢

转载自gxblluojialin.iteye.com/blog/2019536
今日推荐