摘自http://blog.csdn.net/qq_18989901/article/details/52403737

 GridBagLayout的用法

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练习的更多相关文章

  1. Java GridBagLayout 简单使用

    这里只介绍了很基础布局构建及使用,主要是关于 GridBagLayout. 首先整套流程大概是, 声明一个 GridBagLayout 对象 private GridBagLayout gridBag ...

  2. Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)

    控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...

  3. 关于GridBagLayout的讲解哦

    13-08-29 17:01:10|  分类: Java |  标签:gridbaglayout  gridbagconstraints  添加方法  |字号 订阅     GridBagLayout ...

  4. GridBagLayout()的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  5. GridBagLayout:网格包布局管理器

    GridBagLayout:网格包布局管理器   GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说: GridBagConstraints g ...

  6. GridBagLayout占多行效果注意

    如果想要出现按钮2占两行的效果,必须按键3.按钮4同时存在且同时可见. 如果缺少按钮4,则按钮2不会占两行: 如果缺少按钮3.4,则按钮2也不会占两行. package com.wst.bj; imp ...

  7. javax.Swing 使用GridBagLayout的程序栗子

    摘自https://zhidao.baidu.com/question/110748776.html javax.Swing 使用GridBagLayout的程序栗子 总共两个文件,第一个是启动文件, ...

  8. 使用GridBagLayout控制行列的高度和宽度

    摘自http://bbs.csdn.net/topics/340189065使用GridBagLayout控制行列的高度和宽度 gridwidth 指定组件显示区域的某一行中的单元格数. 默认值1,水 ...

  9. Java 的布局管理器GridBagLayout的使用方法(转)

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

随机推荐

  1. softlayer virtual machine vhd磁盘镜像导入shell脚本

    脚本

  2. 获取对象类型(swift)

    获取对象类型(swift) by 伍雪颖 let date = NSDate() let name = date.dynamicType println(name) let string = &quo ...

  3. BOOST中如何实现线程安全代码

    1 需要include <boost/thread.hpp> 2 背景知识请参考<boost程序库完全开发指南>第12章 3 编绎:g++ -o mthread mthread ...

  4. qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程

    1.概述 QCustomPlot 是一个超强超小巧的qt绘图类,非常漂亮,非常易用,只需要加入一个qcustomplot.h和qcustomplot.cpp文件即可使用,远比qwt方便和漂亮,可以自己 ...

  5. HBase配置&启动脚本分析

    本文档基于hbase-0.96.1.1-cdh5.0.2,对HBase配置&启动脚本进行分析 date:2016/8/4 author:wangxl HBase配置&启动脚本分析 剔除 ...

  6. POJ 1269 - Intersecting Lines 直线与直线相交

    题意:    判断直线间位置关系: 相交,平行,重合 include <iostream> #include <cstdio> using namespace std; str ...

  7. C# 导出Excel Aspose.Cells

    public void ExportExcel() { Workbook workbook = new Workbook(); //工作簿 workbook.Worksheets.Clear(); w ...

  8. js前台获取list的demo

    后台 bannerlist=homePageService.searchBanner(bannerVO); // 注意这里的Gson的构建方式为GsonBuilder,区别于test1中的Gson g ...

  9. 我的第一个comet长连接例子

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. js经典代码技巧学习之一:使用三元运算符处理javascript兼容

    window.Event = { add: function() { //使用条件表达式检测标准方法是否存在 return document.addEventListener ? function(a ...