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也是一种数据库,但是它不是用表,而是用集合来装数据的,我对这种 ...
随机推荐
- 66. Plus One【leetcode】
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...
- python_装饰器_语法糖
什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...
- Win7 IIS建站
1.在控制面板->程序和功能->打开或关闭windows功能->安装IIS服务 2.控制面板->管理工具->IIS管理器 客户端访问Web服务器,服务器端不需关闭防火墙, ...
- Session获取不到的情况及解决办法(源码解析)
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处,请尊重他人努力成果,谢谢. 1. 当有连个sessionFactory时,容易产生获取不到session的情况 ...
- 分页工具类 BaseAction
package com.xxxxxxx.bos.web.action.common; import java.io.IOException; import java.lang.reflect.Para ...
- 在 WebSphere Application Server V7 集群环境中管理 HTTP session[阅读]
http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1012_dingsj_wascluster/1012_ding ...
- ExecutorService的submit方法使用
在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...
- Akka(19): Stream:组合数据流,组合共用-Graph modular composition
akka-stream的Graph是一种运算方案,它可能代表某种简单的线性数据流图如:Source/Flow/Sink,也可能是由更基础的流图组合而成相对复杂点的某种复合流图,而这个复合流图本身又可以 ...
- LINQ to Entities 中的查询
MSDN地址:https://msdn.microsoft.com/zh-cn/library/bb399367%28v=vs.100%29.aspx .NET Framework 4 查询是一种从数 ...
- RSA简介(四)——求逆算法
此处所谓求逆运算,是指在模乘群里求逆. 第一节里提到互质的两个定义: (1)p,q两整数互质指p,q的最大公约数为1. (2)p.q两整数互质指存在整数a,b,使得ap+bq=1. 只要明白了欧几里得 ...