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 类是一个灵活的布局管理器,它不要求 ...
随机推荐
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)
2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...
- nodejs学习笔记之网络编程
了解一下OSI七层模型 OSI层 功能 TCP/IP协议 应用层 文件传输,电子邮件,文件服务,虚拟终端 TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet 表示层 数据格式化 ...
- shell数组(产生不同的随机数)
#!/bin/bash # declare -a ARRAY read -p "Please input num[1-39]:" EMENUM #对比新生成的随机数是否重复 fun ...
- add.fun.php
<?php header("Content-type: text/html; charset=utf-8"); function add($min_int,$max_int) ...
- 数据库中简单的增删改查(CRUD)
一切都是基于数据,而对数据的管理都离不开数据库.最近学到数据库的简单操作,所以写下这篇文章,总结一下学习到的知识.浅陋之处,多多见谅. 补充一下:一直弄不清SQL Server,Mysql ,以及Or ...
- Pure Css 菜单的使用
本人新手,之前偶尔接触Bootstrap,也做过一些响应式开发,但是都是略显皮毛,公司的业务需求也限制了深入学习. 现着手Pure Css学习,尝试了简单的左边菜单自动隐藏的demo.闲话少说,代码贴 ...
- 文件读写IO
摘要:本文主要总结了以下有关文件读写的IO,系统调用与库函数. 1.初级IO函数:close,creat,lseek,open,write 文件描述符是一个整型数 1.1close 1.2int cr ...
- ci 笔记
一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controllers/hello.php 1 <?php 2 //放止用户直接通过路 ...
- js获取url参数的方法
js获取url参数的方法有很多. 1.正则分析 function getQueryString(name) { var reg = new RegExp("(^|&)" + ...
- UIProgressView 圆角
里面外面都变成圆角 不用图片 直接改变layer 重点是里面外面都是圆角哦 for (UIImageView * imageview in self.progress.subviews) { imag ...