日期:2014-05-16 浏览次数:20645 次
在向由GridBagLayout类管理的容器中添加组件时,需要为每个组件创建一个与之关联的GridBagContrains类的对象,通过该类的属性设置组件的布局信息。
?
GridBagConstraints常用字段:
?
(1)gridx和gridy
gridx:指定包含组件的显示区域开始边的单元格,其中行的第一个单元格为 gridx=0。
gridy:指定位于组件显示区域的顶部的单元格,其中最上边的单元格为 gridy=0。
?
(2)gridwidth和gridheight
gridwidth:指定组件显示区域的某一行中的单元格数。
gridheight:指定在组件显示区域的一列中的单元格数。
?
(3)insets
insets:指定组件的外部填充,即组件与其显示区域边缘之间间距的最小量。
//该控件与上边缘、左边缘、下边缘、右边缘的间距是分别是1、2、3、4
gridBagConstraints.insets=new Insets(1,2,3,4);
?
(4)fill
当组件的显示区域大于它所请求的显示区域的大小时使用此字段。它可以确定是否调整组件大小,以及在需要的时候如何进行调整。即:当单元格显示区域的面积大于组件的面积时,或者一个组件占用多个单元格时,显示组件可能不必占用所有显示区域,这种情况下使用fill属性设置组件的填充方式。
NONE:不调整组件大小。 HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度。 VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。 BOTH:使组件完全填满其显示区域。 (5)ipadx和ipady
ipadx:指定组件的内部填充,即给组件的最小宽度添加多大的空间。
ipady:指定内部填充,即给组件的最小高度添加多大的空间。
?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* 在向由GridBagLayout类管理的容器中添加组件时,需要为每个组件创建一个与之
* 关联的GridBagContrains类的对象,通过该类的属性设置组件的布局信息
*/
public class Test_GridBagLayout extends JFrame{
JButton []button;
JPanel panel1,panel2,panel3;
GridBagLayout gridBag=new GridBagLayout();
GridBagConstraints gridBagCon;
public Test_GridBagLayout(){
initFrame();
Dimension faceSize=new Dimension(400,600);
this.setSize(faceSize);
//设置窗体的显示区域
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width-faceSize.width)/2,
(int)(screenSize.height-faceSize.height)/2);
//
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/*
* 初始化界面
*/
public void initFrame(){
//初始化JButton控件
button=new JButton[11];
for(int i=0;i<button.length;i++){
button[i]=new JButton();
}
//初始化JPanel控件
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
//panel1演示gridx和gridy
panel1.setLayout(gridBag);
//向panel1中添加button[0];
button[0].setText("组件A(0,0)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=0;
gridBagCon.gridy=0;
gridBag.setConstraints(button[0], gridBagCon);
panel1.add(button[0]);
//向panel1中添加button[1];
button[1].setText("组件B(2,0)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=2;
gridBagCon.gridy=0;
gridBag.setConstraints(button[1], gridBagCon);
panel1.add(button[1]);
//向panel1中添加button[2];
button[2].setText("组件C(1,1)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=1;
gridBagCon.gridy=1;
gridBag.setConstraints(button[2], gridBagCon);
panel1.add(button[2]);
//向panel1中添加button[3];
button[3].setText("组件D(2,2)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=2;
gridBagCon.gridy=2;
gridBag.setConstraints(button[3], gridBagCon);
panel1.add(button[3]);
//panel2演示gridwidth和gridheight属性
panel2.setLayout(gridBag);
//向panel2中添加button[4]
button[4].setText("组件1(2,1)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=0;
gridBagCon.gridy=0;
gridBagCon.gridwidth=2;
gridBagCon.gridheight=1;
gridBag.setConstraints(button[4], gridBagCon);
panel2.add(button[4]);
//向panel2中添加button[5]
button[5].setText("组件2(1,2)");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=0;
gridBagCon.gridy=2;
gridBagCon.gridwidth=1;
gridBagCon.gridheight=2;
gridBag.setConstraints(button[5], gridBagCon);
panel2.add(button[5]);
//向panel2中添加button[6]
button[6].setText("组件3(2,2)");
gridBagCon=new GridBagConstraints(