GroupLayout 布局
文档说明:
以下引自:Java™ Platform
Standard Ed. 7
public class GroupLayout
extends Object
implements LayoutManager2
GroupLayout
is a LayoutManager
that hierarchically groups components in order to position them in a Container
. GroupLayout
is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the Group
class. GroupLayout
supports two types of groups. A sequential group positions its child elements sequentially, one after another. A parallel group aligns its child elements in one of four ways.
Each group may contain any number of elements, where an element is a Group
,Component
, or gap. A gap can be thought of as an invisible component with a minimum, preferred and maximum size. In addition GroupLayout
supports a preferred gap, whose value comes from LayoutStyle
.
Elements are similar to a spring. Each element has a range as specified by a minimum, preferred and maximum. Gaps have either a developer-specified range, or a range determined by LayoutStyle
. The range for Component
s is determined from theComponent
's getMinimumSize
, getPreferredSize
and getMaximumSize
methods. In addition, when adding Component
s you may specify a particular range to use instead of that from the component. The range for a Group
is determined by the type of group. AParallelGroup
's range is the maximum of the ranges of its elements. ASequentialGroup
's range is the sum of the ranges of its elements.
GroupLayout
treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. The horizontal group is responsible for determining the minimum, preferred and maximum size along the horizontal axis as well as setting the x and width of the components contained in it. The vertical group is responsible for determining the minimum, preferred and maximum size along the vertical axis as well as setting the y and height of the components contained in it. Each Component
must exist in both a horizontal and vertical group, otherwise anIllegalStateException
is thrown during layout, or when the minimum, preferred or maximum size is requested.
The following diagram shows a sequential group along the horizontal axis. The sequential group contains three components. A parallel group was used along the vertical axis.
To reinforce that each axis is treated independently the diagram shows the range of each group and element along each axis. The range of each component has been projected onto the axes, and the groups are rendered in blue (horizontal) and red (vertical). For readability there is a gap between each of the elements in the sequential group.
The sequential group along the horizontal axis is rendered as a solid blue line. Notice the sequential group is the sum of the children elements it contains.
Along the vertical axis the parallel group is the maximum of the height of each of the components. As all three components have the same height, the parallel group has the same height.
The following diagram shows the same three components, but with the parallel group along the horizontal axis and the sequential group along the vertical axis.
As c1
is the largest of the three components, the parallel group is sized to c1
. As c2
andc3
are smaller than c1
they are aligned based on the alignment specified for the component (if specified) or the default alignment of the parallel group. In the diagram c2
and c3
were created with an alignment of LEADING
. If the component orientation were right-to-left then c2
and c3
would be positioned on the opposite side.
The following diagram shows a sequential group along both the horizontal and vertical axis.
GroupLayout
provides the ability to insert gaps between Component
s. The size of the gap is determined by an instance of LayoutStyle
. This may be turned on using thesetAutoCreateGaps
method. Similarly, you may use thesetAutoCreateContainerGaps
method to insert gaps between components that touch the edge of the parent container and the container.
The following builds a panel consisting of two labels in one column, followed by two textfields in the next column:
JComponent panel = ...;
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout); // Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true); // Turn on automatically creating gaps between components that touch
// the edge of the container and the container.
layout.setAutoCreateContainerGaps(true); // Create a sequential group for the horizontal axis. GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); // The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
// Putting the labels in a parallel group along the horizontal axis
// positions them at the same x location.
//
// Variable indentation is used to reinforce the level of grouping.
hGroup.addGroup(layout.createParallelGroup().
addComponent(label1).addComponent(label2));
hGroup.addGroup(layout.createParallelGroup().
addComponent(tf1).addComponent(tf2));
layout.setHorizontalGroup(hGroup); // Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); // The sequential group contains two parallel groups that align
// the contents along the baseline. The first parallel group contains
// the first label and text field, and the second parallel group contains
// the second label and text field. By using a sequential group
// the labels and text fields are positioned vertically after one another.
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label1).addComponent(tf1));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label2).addComponent(tf2));
layout.setVerticalGroup(vGroup);
When run the following is produced.
This layout consists of the following.
- The horizontal axis consists of a sequential group containing two parallel groups. The first parallel group contains the labels, and the second parallel group contains the text fields.
- The vertical axis consists of a sequential group containing two parallel groups. The parallel groups are configured to align their components along the baseline. The first parallel group contains the first label and first text field, and the second group consists of the second label and second text field.
There are a couple of things to notice in this code:
- You need not explicitly add the components to the container; this is indirectly done by using one of the
add
methods ofGroup
. - The various
add
methods return the caller. This allows for easy chaining of invocations. For example,group.addComponent(label1).addComponent(label2);
is equivalent togroup.addComponent(label1); group.addComponent(label2);
. - There are no public constructors for
Group
s; instead use the create methods ofGroupLayout
.
- Since:
- 1.6
总结:
例子:
参考自网上,有另一个比较好的例子:
import java.awt.EventQueue; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField; public class GroupLayoutTest extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new GroupLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
} public GroupLayoutTest() {
setTitle("GroupLayoutTest");
setSize(200, 200); JLabel la1 = new JLabel("登录系统");
JLabel la2 = new JLabel("帐号:");
JLabel la3 = new JLabel("密码:"); JTextField tf1 = new JTextField(10);
tf1.setMaximumSize(tf1.getPreferredSize());
JTextField tf2 = new JTextField(10);
tf2.setMaximumSize(tf2.getPreferredSize()); JCheckBox rb1 = new JCheckBox("记住密码");
JCheckBox rb2 = new JCheckBox("自动登录"); JButton bt = new JButton("登录"); GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true); layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(la2)
.addComponent(la3))
.addGroup(layout.createParallelGroup()
.addComponent(la1)
.addComponent(tf1)
.addComponent(tf2)
.addComponent(rb1)
.addComponent(rb2)
.addComponent(bt))); layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(la1))
.addGroup(layout.createParallelGroup()
.addComponent(la2)
.addComponent(tf1))
.addGroup(layout.createParallelGroup()
.addComponent(la3)
.addComponent(tf2))
.addGroup(layout.createParallelGroup()
.addComponent(rb1))
.addGroup(layout.createParallelGroup()
.addComponent(rb2))
.addGroup(layout.createParallelGroup()
.addComponent(bt))); }
}
GroupLayout 布局的更多相关文章
- (转)Java 的swing.GroupLayout布局管理器的使用方法和实例
摘自http://www.cnblogs.com/lionden/archive/2012/12/11/grouplayout.html (转)Java 的swing.GroupLayout布局管理器 ...
- Java 的swing.GroupLayout布局管理器的使用方法和实例(转)
The following builds a panel consisting of two labels in one column, followed by two textfields in t ...
- GUI(GroupLayout 分组布局)
组:一些组件的集合 连续组:一个接着一个摆放 并行组:一个组在另一个组的顶部 ...
- Java Swing 第03记 布局管理器
几种Swing常用的布局管理器 BorderLaout 它将容器分为5个部分,即东.南.西.北.中,每一个区域可以容纳一个组件,使用的时候也是通过BorderLayout中5个方位常量来确定组件所在的 ...
- 前端框架 EasyUI (2)页面布局 Layout
在 Web 程序中,页面布局对应用程序的用户体验至关重要. 在一般的信息管理类的 Web 应用程序中,页面结构通常有一个主工作区,然后在工作区上下左右靠近边界的区域设置一些边栏,用于显示信息或放置一些 ...
- TODO:Laravel 使用blade标签布局页面
TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...
- CSS HTML元素布局及Display属性
本篇文章主要介绍HTML的内联元素.块级元素的分类与布局,以及dispaly属性对布局的影响. 目录 1. HTML 元素分类:介绍内联元素.块级元素的分类. 2. HTML 元素布局:介绍内联元素. ...
- 谈谈一些有趣的CSS题目(六)-- 全兼容的多列均匀布局问题
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- Xamarin+Prism开发详解五:页面布局基础知识
说实在的研究Xamarin到现在,自己就没设计出一款好的UI,基本都在研究后台逻辑之类的!作为Xamarin爱好者,一些简单的页面布局知识还是必备的. 布局常见标签: StackLayout Abso ...
随机推荐
- 自己写的Dapper通用数据访问层
using Microsoft.Practices.EnterpriseLibrary.Data; using Microsoft.Practices.EnterpriseLibrary.Data.O ...
- Java web 文件下载
/** * 下载文件 * @param msg */ public boolean printOutFile(String fileFullName,String fileName) { if (fi ...
- 第三篇:python高级之生成器&迭代器
python高级之生成器&迭代器 python高级之生成器&迭代器 本机内容 概念梳理 容器 可迭代对象 迭代器 for循环内部实现 生成器 1.概念梳理 容器(container ...
- LINQ高级编程 笔记
相关资料:http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 1.什么是LINQ 语言集成查询是一系列标准查询操作符的集合, ...
- c#正则表达式采集数据
protected void Page_Load(object sender, EventArgs e){ StringBuilder MyStringBuilder = new StringBuil ...
- ssh配置事务
http://blog.csdn.net/jianxin1009/article/details/9202907(不错)
- HibernateTool的安装和使用(Eclipse中)
http://blog.sina.com.cn/s/blog_919273e20101g1t7.html
- OC - 30.如何封装自定义布局
概述 对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果 由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可 在进行分装的时候,通常需要用 ...
- 网页main中左边固定宽度,右边自适应。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL Server 连接字符串和身份验证
SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合.每一个属性/值对都由分号隔开. PropertyName1=Value1;P ...