您的位置:首页 > 产品设计 > UI/UE

Java_GUI中GridBagLayout布局使用详解

2015-07-12 18:28 549 查看
GridBagLayout以表格形式布置容器内的组件, 将每个组件放置在每个单元格内,而一个单元格可以跨越多个单元格合并成一个单元格,即多个单元格可以组合成一个单元格,从而实现组件的自由布局。

每一个单元格都有各自的属性,而这些属性由GridBagConstrainsts类的成员变量来定义,且GridBagConstriaints中的所有成员变量都是public的。

构造函数:

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对象,并指定其参数的值。

下面提供一个用来设置GridBagConstraints对象各参数的帮助类

import java.awt.GridBagConstraints;
import java.awt.Insets;
/**
1.
2. @author han
3.         功能:用来设置GridBagConstraints对象的一些参数
4.
*/
public class GBC extends GridBagConstraints {
/**
* 初始化左上角位置 gridx,gridy —— 设置组件的位置,
* gridx=0,gridy=0时放在0行0列。
*
* @param gridx
* @param gridy
*/
public GBC(int gridx, int gridy) {
this.gridx = gridx;
this.gridy = gridy;
}

/**
* 初始化左上角位置和所占行数和列数
*
* gridwidth,gridheight —— 用来设置组件所占的单位长度与高度,默认值皆为1。
* 可以使用GridBagConstraints.REMAINDER常量,
* 代表此组件为此行或此列的最后一个 组件,而且会占据所有剩余的空间。
*
* @param gridx
* @param gridy
* @param gridwidth
* @param gridheight
*/
public GBC(int gridx, int gridy, int gridwidth, int gridheight) {
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}

/**
* 对齐方式 anchor:设置组件在单元格中的对齐方式。
* 由以下常量来定义
*
* GridBagConstraints.CENTER
*
* GridBagConstraints.EAST
*
* GridBagConstraints.WEST
*
* GridBagConstraints.SOUTH
*
* GridBagConstraints.NORTH
*
* GridBagConstraints.SOUTHEAST
*
* GrisBagConstraints.SOUTHWEST
*
* GridBagConstraints.NORTHEAST
*
* GridBagConstraints.NORTHWEST
*
* @param anchor
* @return
*/
public GBC setAnchor(int anchor) {
this.anchor = anchor;
return this;
}

/**
* 是否拉伸及拉伸方向
*
* fill:当某个组件未能填满单元格时,可由此属性设置横向、
* 纵向或双向填满。由以下常量来定义
*
* GridBagConstraints.NONE
*
* GridBagConstraints.HORIZONTAL
*
* GridBagConstraints.VERTICAL
*
* GridBagConstraints.BOTH
*
* @param fill
* @return
*/
public GBC setFill(int fill) {
this.fill = fill;
return this;
}

/**
* x和y方向上的增量
*
* weightx,weighty——用来设置窗口变大时,各组件跟着变大的比例。
* 当数字越大,表示组件能得到更多的空间,默认值皆为0(0-1)。
*
* @param weightx
* @param weighty
* @return
*/
public GBC setWeight(double weightx, double weighty) {
this.weightx = weightx;
this.weighty = weighty;
return this;
}

/**
* 外部填充
*
* @param distance
* @return
*/
public GBC setInsets(int distance) {
this.insets = new Insets(distance, distance, distance, distance);
return this;
}

/**
* 外填充
*
* insets —— 设置组件之间彼此的间距。
* 它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。
*
* @param top
* @param left
* @param bottom
* @param right
* @return
*/
public GBC setInsets(int top, int left, int bottom, int right) {
this.insets = new Insets(top, left, bottom, right);
return this;
}

/**
* 内填充
*
* ipadx,ipady —— 设置组件间距,默认值为0。
*
* @param ipadx
* @param ipady
* @return
*/
public GBC setIpad(int ipadx, int ipady) {
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}


GridBagLayout布局的一个案例—->使三个带颜色的面板一直在Frame窗口的中央显示(无论窗口放大还是缩小)

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
/**
*
* @author han
*
* 使用GridBagLayout布局的一个案例
*
* 功能:使三个带颜色的面板一直在Frame窗口的中央显示(无论窗口放大还是缩小)
*
*/
public class GridBagLayoutTest extends JFrame {

private static final long serialVersionUID = 1391949900949468015L;
private JPanel contentPane;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutTest frame = new GridBagLayoutTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public GridBagLayoutTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 511, 500);
setTitle("GridBagLayout布局案例");
contentPane = new JPanel();
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
contentPane.setLayout(gbl_contentPane);

JPanel jPanel1 = new JPanel();
jPanel1.setSize(300, 100);
jPanel1.setBackground(Color.blue);
JPanel jPanel2 = new JPanel();
jPanel2.setSize(300, 300);
jPanel2.setBackground(Color.red);
JPanel jPanel3 = new JPanel();
jPanel3.setSize(300, 100);
jPanel3.setBackground(Color.YELLOW);

//如果加入的是空的面板时需要把内部填充的大小与面板size的大小设为一样。
contentPane.add(jPanel1, new GBC(0, 1).setInsets(20, 0, 20, 0)
.setWeight(1, 0).setIpad(300, 100));
contentPane.add(jPanel2, new GBC(0, 2).setInsets(20, 0, 20, 0)
.setWeight(1, 0).setIpad(300, 100));
contentPane.add(jPanel3, new GBC(0, 3).setInsets(20, 0, 20, 0)
.setWeight(1, 0).setIpad(300, 100));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: