在向由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();
gridBagCon.gridx=3;
gridBagCon.gridy=3;
gridBagCon.gridwidth=2;
gridBagCon.gridheight=2;
gridBag.setConstraints(button[6], gridBagCon);
panel2.add(button[6]);
//panel3演示ipadx和ipady
panel3.setLayout(gridBag);
//向panel3中添加button[7]
button[7].setText("组件Ⅰ");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=0;
gridBagCon.gridy=0;
gridBag.setConstraints(button[7], gridBagCon);
panel3.add(button[7]);
//向panel3中添加button[8]
button[8].setText("组件Ⅱ");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=0;
gridBagCon.gridy=1;
gridBag.setConstraints(button[8], gridBagCon);
panel3.add(button[8]);
//向panel3中添加button[9]
button[9].setText("组件Ⅲ");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=1;
gridBagCon.gridy=0;
gridBag.setConstraints(button[9], gridBagCon);
panel3.add(button[9]);
//向panel3中添加button[10]
button[10].setText("组件Ⅳ");
gridBagCon=new GridBagConstraints();
gridBagCon.gridx=1;
gridBagCon.gridy=1;
gridBagCon.ipadx=10;
gridBagCon.ipady=20;
gridBag.setConstraints(button[10], gridBagCon);
panel3.add(button[10]);
//添加panel1,panel2,panel3
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(3,1));
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test_GridBagLayout();
}
}
分享到:
相关推荐
GridBagLayout布局管理器:主要是关于java中GridBagLayout的布局,该文档详细描写的GridBagLayout的具体用法。
深入浅出介绍Java Swing编程中的GridBagLayout布局。
本文通过代码示例的方式介绍了Swing的GridBagLayout布局样式的应用,不拘泥于JDK文档教条方式的陈述,图形效果图与文字相结合,讲解直观而浅显易懂,另外,示例代码中还包含了BorderLayout、FlowLayout等布局的应用...
java网格包GridBagLayout布局管理器专题.pdf
java网格包GridBagLayout布局管理器专题借鉴.pdf
java网格包GridBagLayout布局管理器专题的分析.pdf
这是为初初学者提供练习的代码,具有很强的指导效果哈~~~
Java Swing GridBagLayout,很详细的Demo,对GridBagLayout如何使用做全面的讲解
网袋布局管理器--GridBagLayout使用介绍
java swing布局管理器实例之gridbaglayout,实现左右选择框
用java写的一个简单的计算器,包含源码、素材、说明
咋也不用愁,老师布置的界面设计的作业了
原创,为完整java工程包。根据html的table标签作为配置文件,进行swing GridBagLayout方式的布局管理。简化swing复杂界面布局不方便,容器定位的问题。 具体思想是将tabel td等标签相关元素和属性映射成...
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的,需要的朋友可以了解下
Java 计算器 采用gridbaglayout布局实现的界面布局
主要介绍了Java Swing GridBagLayout网格袋布局的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://chenlinbo.iteye.com/blog/610417
收集一些Java 图形用户界面编程实验源码集锦:1.窗口和事件处理。2.容器与布局管理(流式布局FlowLayout)。3.容器与布局管理(边缘布局BorderLayout)。4.容器与布局管理(网格布局GridLayout)。5.容器与布局管理...
Java编程老鸟潜心写作,奉献高效率的Java学习心得 完全站在没有编程经验读者的角度,手把手教会读者学习Java 配16小时多媒体教学视频,高效、直观 一一击破Java入门可能会遇到的难点和疑惑 抽丝剥茧,层层推进,让...