GridBagLayout练习
摘自http://blog.csdn.net/qq_18989901/article/details/52403737
GridBagLayout是面板设计中最复杂的布局工具,当然用的好的话也是最方便的。
GridBagLayout其组件的摆放都是有GridBagConstraints控制的。可以将需要设计的界面划分成许多个纵横的小网格,每个网格里面最多放置一个组件,一个组件可以占用多个网格。
网格的定义是由gridx和gridy定义的,前者表示横坐标,后者表示纵坐标,坐标是从0开始,从整个面板的左上角开始算起,所以在上图中,按钮7所在的位置gridx=0,gridy=0;以此类推,后面所有的网格位置都可以这样定义;
但是图中网格的宽度和高度是由什么定义的呢?答案是gridwidth和gridheight,为1表示占用一个网格,为2表示占用两个网格,如上图中"+"的gridwidth=1,gridheight=2;
知道这两个,就可以大体把网格划分清楚,每一个的具体位置也就有了着落。有朋友会说,我也是这样写的,但是组件怎么就那么小呢?那是因为GridBaglayout里面还有一项参数weight,她控制着组件随着面板的变化自身的变化情况,默认情况下weightx=0,weighty=0,意思是组件大小固定,不管面板如何变,自身就那么大,但是如果想让组件随面板变化的话,可以设置weightx和weighty,设置为浮点数也行,其值代表着变化的大小,也就是你宽度设为2,高度设为1的话,拉大面板会使组件越变越宽。
这三个是比较重要的Constraints,另外,还有insets,其有四个参数,表示其上左下右和相邻组件的最小间隔;ipadx和ipady表示组件间距,默认是0(其和insets不同读者可以自己摸索一下);fill参数表示当网格区域比组件大的伤害,组件是以何种方式填充区域,是全方位还是水平或竖直;anchor是区域比组件大时,组件应该显示在区域的哪个方位,西北还是东南;
测试1:
本文主要通过设计一个计算器界面的方式,来学习GridBagLayout的使用。最终的效果如下图所示:

下面是我实现面板显示部分的所有代码:
package com.wst.bj; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField; public class GridBagTest2 { private JFrame jframe = new JFrame();
private JButton bt1 = new JButton("1");
private JButton bt2 = new JButton("2");
private JButton bt3 = new JButton("3");
private JButton bt4 = new JButton("4");
private JButton bt5 = new JButton("5");
private JButton bt6 = new JButton("6");
private JButton bt7 = new JButton("7");
private JButton bt8 = new JButton("8");
private JButton bt9 = new JButton("9");
private JButton bt0 = new JButton("0");
private JButton btdot = new JButton(".");
private JButton btsignleft = new JButton("(");
private JButton btsignright = new JButton(")");
private JButton btp = new JButton("+");
private JButton btm = new JButton("-");
private JButton btmp = new JButton("*");
private JButton btd = new JButton("/");
private JButton bte = new JButton("=");
private JButton bt = new JButton("00");
private JButton btclear = new JButton("cl");
private JTextField textField = new JTextField(); public GridBagTest2() {
init();
} private void init()
{
FrameUtil.initFram(jframe, 300, 400);
JPanel jpanel = createPanel();
jframe.add(jpanel);
} private JPanel createPanel(){
JPanel panel = new JPanel(); GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbs = new GridBagConstraints();
panel.setLayout(gbl); panel.add(bt7);panel.add(bt8);panel.add(bt9);panel.add(btp);
panel.add(bt4);panel.add(bt5);panel.add(bt6);
// panel.add(bt1);panel.add(bt2);panel.add(bt9);panel.add(btp);
panel.add(bt1);panel.add(bt2);panel.add(bt3);panel.add(btm);
panel.add(bt0);panel.add(btdot);
panel.add(btsignleft);panel.add(btsignright);panel.add(bte);panel.add(btmp);
panel.add(btclear);panel.add(bt); panel.add(btd);
panel.add(textField); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=0;
gbl.setConstraints(bt7, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=0;
gbl.setConstraints(bt8, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=0;
gbl.setConstraints(bt9, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=0;
gbl.setConstraints(btp, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=1;
gbl.setConstraints(bt4, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=1;
gbl.setConstraints(bt5, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=1;
gbl.setConstraints(bt6, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=2;
gbl.setConstraints(bt1, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=2;
gbl.setConstraints(bt2, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=2;
gbl.setConstraints(bt3, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=2;
gbl.setConstraints(btm, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=2;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=3;
gbl.setConstraints(bt0, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=3;
gbl.setConstraints(btdot, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=4;
gbl.setConstraints(btsignleft, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=4;
gbl.setConstraints(btsignright, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=4;
gbl.setConstraints(bte, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=4;
gbl.setConstraints(btmp, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=5;
gbl.setConstraints(btclear, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=5;
gbl.setConstraints(bt, gbs);
gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=5;
gbl.setConstraints(btd, gbs); gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=4;gbs.gridheight=3;
gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=6;
gbl.setConstraints(textField, gbs); return panel;
}
}
测试2:

package com.wst.bj; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField; public class GridBagTest4 { private JFrame jframe = new JFrame();
private JButton bt1 = new JButton("1");
private JButton bt2 = new JButton("2");
private JButton bt3 = new JButton("3");
private JButton bt4 = new JButton("4");
private JButton bt5 = new JButton("5");
private JButton bt6 = new JButton("6");
private JButton bt7 = new JButton("7");
private JButton bt8 = new JButton("8");
private JButton bt9 = new JButton("9");
private JButton bt0 = new JButton("0");
private JButton btdot = new JButton(".");
private JButton btsignleft = new JButton("(");
private JButton btsignright = new JButton(")");
private JButton btp = new JButton("+");
private JButton btm = new JButton("-");
private JButton btmp = new JButton("*");
private JButton btd = new JButton("/");
private JButton bte = new JButton("=");
private JButton bt = new JButton("00");
private JButton btclear = new JButton("cl");
private JTextField textField = new JTextField(); public GridBagTest4() {
init();
} private void init()
{
FrameUtil.initFram(jframe, 300, 400);
JPanel jpanel = createPanel();
jframe.add(jpanel);
} private JPanel createPanel(){
JPanel panel = new JPanel(); GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbs = new GridBagConstraints();
panel.setLayout(gbl); panel.add(bt7);panel.add(bt8);panel.add(bt9);panel.add(btp);
panel.add(bt4);panel.add(bt5);panel.add(bt6);
panel.add(bt1);panel.add(bt2);panel.add(bt3);panel.add(btm);
panel.add(bt0);panel.add(btdot);
panel.add(btsignleft);panel.add(btsignright);panel.add(bte);panel.add(btmp);
panel.add(btclear);panel.add(bt); panel.add(btd);
panel.add(textField); gbs.fill=GridBagConstraints.NONE;
// gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.ipadx = 100;
// gbs.ipady = 100;
gbs.anchor = GridBagConstraints.WEST;
gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=0;
gbl.setConstraints(bt7, gbs); gbs.fill=GridBagConstraints.NONE;
// gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.NORTHEAST;
gbs.gridx=1;gbs.gridy=0;
gbl.setConstraints(bt8, gbs); gbs.fill=GridBagConstraints.NONE;
// gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.SOUTH;
gbs.gridx=2;gbs.gridy=0;
gbl.setConstraints(bt9, gbs);
//
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=2;
//// gbs.insets=new Insets(5, 5, 5, 5);
//// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=0;
gbl.setConstraints(btp, gbs);
// =====================================================
gbs.fill=GridBagConstraints.VERTICAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.WEST;
gbs.gridx=0;gbs.gridy=1;
gbl.setConstraints(bt4, gbs);
//
gbs.fill=GridBagConstraints.VERTICAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.SOUTHEAST;
gbs.gridx=1;gbs.gridy=1;
gbl.setConstraints(bt5, gbs); gbs.fill=GridBagConstraints.VERTICAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.CENTER;
gbs.gridx=2;gbs.gridy=1;
gbl.setConstraints(bt6, gbs);
// =====================================================
gbs.fill=GridBagConstraints.HORIZONTAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.NORTH;
gbs.gridx=0;gbs.gridy=2;
gbl.setConstraints(bt1, gbs); gbs.fill=GridBagConstraints.HORIZONTAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.SOUTHEAST;
gbs.gridx=1;gbs.gridy=2;
gbl.setConstraints(bt2, gbs); gbs.fill=GridBagConstraints.HORIZONTAL;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.CENTER;
gbs.gridx=2;gbs.gridy=2;
gbl.setConstraints(bt3, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=2;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=2;
gbl.setConstraints(btm, gbs);
// =====================================================
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=2;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=3;
gbl.setConstraints(bt0, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=3;
gbl.setConstraints(btdot, gbs);
// =====================================================
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=4;
gbl.setConstraints(btsignleft, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
gbs.insets=new Insets(7, 7, 2, 2);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=4;
gbl.setConstraints(btsignright, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=2;
gbs.insets=new Insets(0, 0, 0, 0);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=2;gbs.gridy=4;
gbl.setConstraints(bte, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=3;gbs.gridy=4;
gbl.setConstraints(btmp, gbs);
// =====================================================
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
gbs.weightx=0;gbs.weighty=0;
gbs.gridx=0;gbs.gridy=5;
gbl.setConstraints(btclear, gbs); gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.gridx=1;gbs.gridy=5;
gbl.setConstraints(bt, gbs); gbs.fill=GridBagConstraints.NONE;
gbs.gridwidth=1;gbs.gridheight=1;
// gbs.insets=new Insets(5, 5, 5, 5);
// gbs.weightx=1;gbs.weighty=1;
gbs.anchor = GridBagConstraints.EAST;
gbs.gridx=3;gbs.gridy=5;
gbl.setConstraints(btd, gbs);
// =====================================================
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=4;gbs.gridheight=3;
// gbs.insets=new Insets(5, 5, 5, 5);
gbs.weightx=1;gbs.weighty=1;
gbs.gridx=0;gbs.gridy=6;
gbl.setConstraints(textField, gbs);
// // =====================================================
gbs.anchor = GridBagConstraints.NORTH;
return panel;
}
}
GridBagLayout练习的更多相关文章
- Java GridBagLayout 简单使用
这里只介绍了很基础布局构建及使用,主要是关于 GridBagLayout. 首先整套流程大概是, 声明一个 GridBagLayout 对象 private GridBagLayout gridBag ...
- Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...
- 关于GridBagLayout的讲解哦
13-08-29 17:01:10| 分类: Java | 标签:gridbaglayout gridbagconstraints 添加方法 |字号 订阅 GridBagLayout ...
- GridBagLayout()的使用方法
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...
- GridBagLayout:网格包布局管理器
GridBagLayout:网格包布局管理器 GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说: GridBagConstraints g ...
- GridBagLayout占多行效果注意
如果想要出现按钮2占两行的效果,必须按键3.按钮4同时存在且同时可见. 如果缺少按钮4,则按钮2不会占两行: 如果缺少按钮3.4,则按钮2也不会占两行. package com.wst.bj; imp ...
- javax.Swing 使用GridBagLayout的程序栗子
摘自https://zhidao.baidu.com/question/110748776.html javax.Swing 使用GridBagLayout的程序栗子 总共两个文件,第一个是启动文件, ...
- 使用GridBagLayout控制行列的高度和宽度
摘自http://bbs.csdn.net/topics/340189065使用GridBagLayout控制行列的高度和宽度 gridwidth 指定组件显示区域的某一行中的单元格数. 默认值1,水 ...
- Java 的布局管理器GridBagLayout的使用方法(转)
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...
随机推荐
- poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- VS 项目(c#)引用了 DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称
1. 在项目上点右键-->属性-->应用程序-->目标框架-->修改为.NET Framework 4. 而我原来的设置是.NET Framework 4 Client Pro ...
- UGUI 快捷键创建UGUI组件
使用NGUI的时候还有xxx快捷键创建, spirte,label,button等等. 在UGUI里面的时候好像是没有快捷键的. 不知道以后多久才能有这个功能. 在家里闲无聊的时候写了一个脚本, ...
- pip安装lxml报错
报错信息 解决方法:`` ...
- Number of Parallelograms(求平行四边形个数)
Number of Parallelograms time limit per test 4 seconds memory limit per test 256 megabytes input sta ...
- ZOJ Goldbach 2013年长沙赛区网络赛
迟到了一天的AC.... 思路: 先把单个素数 或着 两个素数能组成的情况预处理一下,然后对于给出的 n,拿第三个素数去和两个素数的情况匹配,最后要注意去重. 详情见代码. 因为手残少敲了一个 els ...
- OLEDB简介
OLE DB(OLEDB)是微软的战略性的通向不同的数据源的低级应用程序接口.OLE DB不仅包括微软资助的标准数据接口开放数据库连通性(ODBC)的结构化查询语言(SQL)能力,还具有面向其他非SQ ...
- jquery使用load开展局部刷新没有效果
jquery使用load开展局部刷新没有效果 jquery使用load进行局部刷新没有效果我的代码 <html><head><meta charset="u ...
- javascript 获取event对象
//转载处 http://www.cnblogs.com/funlake/archive/2009/04/07/1431238.html 非常详细 先从一个简单的例子说起,一个简单的button控件如 ...
- Jquery根据字段内容设置字段宽度
来博客园很久了,初次写文章,新手,请大牛见谅! 前段时间遇到的问题,通过gridview后台动态生成table,列和行数量未知,要求根据每个单元格内容的多少,设置宽度,每一列选择本列最大的宽度. ta ...