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 ...
随机推荐
- isAssignableFrom与instanceof的区别
1.isAssignableFrom针对的是class对象: 2.instanceof是实例. isAssignableFrom是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的 ...
- android 高德地图API 之 java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLibrary returned null错误
错误场景: 运行android app时,在运行到调用高德地图API时,出现 “java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLi ...
- Tomcat-java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a readable
启动Tomcat的时候,报错:java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a r ...
- C# DataTable去除重复,极其简便、简单
其中sourceDT是获取到的一个DataTable类型的集合对象 去重复使用方式: 实例化一个DataView对象 假设为dv,直接dv.ToTable()即可,ToTable中可为(true,&q ...
- Orace数据库锁表的处理与总结<摘抄与总结三>
当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...
- UITextView/UITextField检测并过滤Emoji表情符号
UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...
- Java中char占用几个字节
在讨论这个问题之前,我们需要先区分unicode和UTF. unicode :统一的字符编号,仅仅提供字符与编号间映射.符号数量在不断增加,已超百万.详细:[https://zh.wikipedia. ...
- STL库list::sort()实现深度解析
原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...
- phpcms源码跟踪(1)
本次跟踪解决几个问题: 1.缓存文件从哪里来,过程中被删除了怎么办 2.模板html是如何被引入的 进入首页时,通过最初的调用,进入控制器\phpcms\modules\content\index.p ...
- 基于jQuery 的图片瀑布流实现
解题思路: 第1步 分析问题:我这边的处理方式是以列为单位.每次滚动条滚到底部,把需要加的新的内容放在高度最小的列.如下图所示 加载后的显示 如果在继续往下滚动.新图片就会在1下边显示,如此类推. ...