Swing-GridBagLayout用法-入门
注:本文内容转自:Java Layout总结-GridBagLayout。内容根据笔者理解稍有整理。
GridBagLayout布局管理器:
这就是最复杂的一个布局管理器了,网格包布局.在此布局中,组件大小不必相同.
GridBagLayout gb=new GridBagLayout();
ContainerName.setLayout(gb);
以上代码是让容器获得一个GridBagLayout.要使用网格包布局,还必须有其一个辅助类,GridBagContraints.它包含GridBagLayout类用来定位及调整组件大小所需要的全部信息.使用步骤如下:
1).创建网格包布局的一个实例,并将其定义为当前容器的布局管理器.
2).创建GridBagContraints的一个实例
3).为组件设置约束.
4).通过方法统治布局管理器有关组件及其约束等信息
5).将组件添加到容器.
6).对各个将被显示的组件重复以上步骤.
GridBagContraints类的成员变量列表如下:
1、gridx—组件的横向坐标;
2、girdy—组件的纵向坐标;
gridx=0,gridy=0时放在0行0列,GridBagConstraints.RELATIVE为默认值,表明当前组件紧跟在上一个组件之后。如果在行上不指定该值(同时也不指定gridx和gridy参数),那么无论添加多少个组件都是在同一行上
3、gridwidth——组件的横向宽度,也就是指组件占用的列数,这与HTML的colspan类似;
4、gridheight—组件的纵向长度,也就是指组件占用的行数,这与HTML的rowspan类似;
设置组件横向纵向跨越多少个网格,他们的默认值都是1,如果该组件是横向或纵向的最后一个还可以将此值设为GridBagConstraints.REMAINDER,若为倒数第二个组件则可以设值为GridBagConstraints.RELATIVE。
gridwidth能否能够保留住该宽度取决于其正上/下方相应位置是否有足够的组件来占位,如果无,位置将会被压缩。比如,设置gridwidth=3,但只添加了一个JButton,这时如果其上下方相应位置没有其他组件或只有1个组件,那么它只占有1个网格大小。如果它上/下方相应位置有3个组件,那它就可以占3个网格大小了。如果它上/下方相应位置只有2个组件,那它就占2个网格大小。
gridheight能否保留住该高度取决于其左右两边是否有足够的组件来占位。也就是说它的最大高度不大于左右两边最大的高度。比如,设置gridheight=3,如果左右两边组件只有1行,则它仅仅只有1行的高度,有2行则占2行的高度。
5、weightx—指行的权重,告诉布局管理器如何分配额外的水平空间;
6、weighty—指列的权重,告诉布局管理器如何分配额外的垂直空间;
用来设置窗口变大时,各组件跟着变大的比例。当数字越大,表示组件能得到更多的空间,默认值皆为0。比如组件A的weightx=0.5,组件B的weightx=1,那么窗口X轴变大时剩余的空间就会以1:2的比例分配给组件A和B;
7、anchor—告诉布局管理器组件在表格空间中的位置,当组件小于其显示区域时使用此字段;
有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。
8、fill—如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充;
9、insets—指组件与表格空间四周边缘的空白区域的大小,内边距,算入组件自身大小中。它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。
10、ipadx— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值;
11、ipady— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值。
外边距,最终组件占用的大小是组件自身大小加上外边距。
下面是测试用例:
GridBagLayoutTest.java:
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/*
* GridBagLayoutTest.java,source code from java核心技术 卷1 基础知识,P379
*/ public class GridBagLayoutTest { public static void main(String[] args) {
// TODO Auto-generated method stub FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} class FontFrame extends JFrame {
int DEUAULT_WIDTH = 300;
int DEUAULT_HEIGHT = 200;
private JComboBox face;
private JComboBox size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample; public FontFrame() {
setTitle("GridBagLayoutTest");
setSize(DEUAULT_WIDTH, DEUAULT_HEIGHT);
GridBagLayout layout = new GridBagLayout();
setLayout(layout); ActionListener listener = new FontAction();
JLabel faceLabel = new JLabel("face:");
face = new JComboBox(new String[] { "serif", "sansSerif", "Monospaced",
"Dialog", "DialogInput", });
face.addActionListener(listener); JLabel sizeLabel = new JLabel("Size:");
size = new JComboBox(new String[] { "8", "10", "12", "15", "18", "24",
"36", "48" });
size.addActionListener(listener); bold = new JCheckBox("Bold");
bold.addActionListener(listener); italic = new JCheckBox("Italic");
italic.addActionListener(listener); sample = new JTextArea();
sample.setText("The quick brown fox jump over the lazy dog.");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder()); add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(italic,
new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
} private class FontAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String fontFace = face.getSelectedItem().toString();
int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
+ (italic.isSelected() ? Font.ITALIC : 0);
int fontSize = Integer.parseInt(size.getSelectedItem().toString());
Font font = new Font(fontFace, fontStyle, fontSize);
sample.setFont(font);
sample.repaint();
}
}
}
GBC.java:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets; /*
* GBC.java,source code from java核心技术 卷1 基础知识,P381
*/ public class GBC extends GridBagConstraints{
/*
* constructs a GBC with a given gridx and gridy position and all other grid
* bag constraint values set to the default
* @param gridx the gridx position
* @param gridy the gridy position
*/
public GBC(int gridx, int gridy){
this.gridx = gridx;
this.gridy = gridy;
} public GBC(int gridx, int gridy, int gridWidth, int gridHeight){
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridWidth;
this.gridheight = gridHeight;
} /*
* sets the anchor
* @param anchor the anchor style
* @return this object for further modification
*/ public GBC setAnchor(int anchor){
this.anchor = anchor;
return this;
} /*
* sets the fill direction
* @param fill the fill direction
* @return this object for further modification
*/ public GBC setFill(int fill){
this.fill = fill;
return this;
} /*
* sets the cell weights
* @param weightx the cell weight in x direction
* @param weighty the cell weight in y direction
* @return this object for further modification
*/ public GBC setWeight(int weightx, int weighty){
this.weightx = weightx;
this.weighty = weighty;
return this;
} /*
* sets the insets of this cell
* @param insets distance ths spacing to use in all directions
* @return this object for further modification
*/ public GBC setInsets(int distance){
this.insets = new Insets(distance, distance, distance, distance);
return this;
} /*
* sets the insets of this cell
* @param top distance ths spacing to use on top
* @param bottom distance ths spacing to use on bottom
* @param left distance ths spacing to use to the left
* @param right distance ths spacing to use to the right
* @return this object for further modification
*/ public GBC setInsets(int top, int left,int bottom,int right){
this.insets = new Insets(top, left, bottom, right);
return this;
} /*
* sets the Ipad of this cell
* @param Ipad distance ths spacing to use in all directions
* @return this object for further modification
*/ public GBC setIpad(int ipadx, int ipady){
this.ipadx = ipadx;
this.ipadx = ipadx;
return this;
}
}

运行效果图
Swing-GridBagLayout用法-入门的更多相关文章
- 精通awk系列(4):awk用法入门
回到: Linux系列文章 Shell系列文章 Awk系列文章 awk用法入门 awk 'awk_program' a.txt awk示例: # 输出a.txt中的每一行 awk '{print $0 ...
- [转帖]PG语法解剖--基本sql语句用法入门
PG语法解剖--基本sql语句用法入门 https://www.toutiao.com/i6710897833953722894/ COPY 命令挺好的 需要学习一下. 原创 波波说运维 2019-0 ...
- Go之Logrus用法入门
Go之Logrus用法入门 Logrus是Go (golang)的结构化日志程序,完全兼容标准库的API日志程序. Logrus is a structured logger for Go (gola ...
- AWK用法入门详解
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- Swing-JComboBox用法-入门
JComboBox是Swing中的下拉菜单控件.它永远只能选中一个项目,然而比单选按钮节省空间.如果使用setEditable设置为true则内部选项的文本可以编辑,因此这种组件被称为组合框.注意,对 ...
- Swing-setBorder()用法-入门
注:本文内容转自:Swing编程边框(Border)的用法总结.内容根据笔者理解稍有整理. 函数说明: public void setBorder(Border border) 设置此组件的边框.Bo ...
- Java-Preferences用法-入门
Properties提供的应用程序解决方案主要存在两个问题: (1)配置文件不能放在主目录中,因为某些OS(如Win9X)没有主目录的概念: (2)没有标准的文件命名规则,存在文件名冲突的可能性. J ...
- Java-Properties用法-入门
对于应用程序的配置,通常的做法是将其保存在独立的配置文件中,程序启动时加载,修改时保存.Java中Properties类就提供了这样一种机制,配置项以Key-Value的数据结构存储在文本文件中,扩展 ...
- MongoDB 用法入门(windows)①
概述 大家对数据库肯定不陌生,肯定也有很多人用过MySQL,但是在用MySQL的时候各种建表,写表之间的关联让人非常头疼. MongoDB也是一种数据库,但是它不是用表,而是用集合来装数据的,我对这种 ...
随机推荐
- dfs+剪枝:poj2362
贴题目 Square Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24604 Accepted: 8449 Descr ...
- C++模板之Vector与STL初探
STL源码初步接触 STL = Standard Template Library,直译过来是:标准模板库,是惠普实验室开发的一系列软件的统称.从根本上说,STL是一些"容器"的集 ...
- Ubuntu 如何安装Google Chrome浏览器
1.上Chrome 官网下载http://www.google.cn/chrome/browser/desktop/ 2.保存下载文件到Home路径下(个人习惯) 3.按Ctrl+Alt +T 调出终 ...
- java参数传递
关于方法的参数传递,java中方法的参数传递均为值传递,根据传递的类型以及方法中对参数的处理可以分为2类: 1.传递参数为基本数据类型,因为是值传递,所以方法运行结束后对传递参数的值无影响. 2.传递 ...
- Razor语法问题(foreach里面嵌套if)
报错: @foreach (var item in ViewBag.TopList) { if (!string.IsNullOrWhiteSpace(item.LogoPic_Mobile)) &l ...
- 【Centos】yum安装MySQL
安装步骤 1. 点击此处下载MySQL的YUM源 -- [ MySQL RPM] 选择适合你平台的rpm,我的是centos7 2. 安装MySQL的yum源,即RPM sudo yum locali ...
- C语言 进程控制---创建进程fork()函数
#include "sys/types.h" #include "stdio.h" #include "stdlib.h" #include ...
- 【Js应用实例】javascript管理cookie
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Flink 1.3.2 Standalone模式安装
一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 二.文件准备 2.1 文件名称 flink-1.3.2-bin-ha ...
- 同形异义字:看我如何拿到TaoBao.com的解析权
*本文原创作者:expsky@MS509Team,本文属FreeBuf原创奖励计划 同形异义字钓鱼攻击号称"几乎无法检测",是最狡猾的钓鱼攻击!这种攻击产生的原因是国际化域名IDN ...