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 ...
随机推荐
- U盘安装centos 6.4教程(总算是弄好了
参考:http://blog.chinaunix.net/uid-27666459-id-3342477.html http://www.linuxidc.com/Linux/2011-05/3569 ...
- 编写android的widget
以前对这个东西很感兴趣,因为确实方便,如今有时间了来做一个例子 首先要定义一个layout(widgetview.xml)和一个配置文件(widgetconfig.xml) <?xml vers ...
- Linux下长时间ping网络加时间戳并记录到文本(转)
[root@test ~]# ping 192.168.2.1 -c 10 PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 bytes f ...
- POS tagging的解釋
轉錄文章~~ 什么是词性标注(POS tagging) Tue, 04/13/2010 - 10:36 — Fuller 词性标注也叫词类标注,POS tagging是part-of-speech t ...
- Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
- 将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据
领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其 ...
- SQL SERVER 中PatIndex的用法个人理解
一般用法:PatIndex('%AAA%',‘BBBBBBBB’) 上句的意思是查找AAA在BBBBBBBB中的位置,从1开始计算,如果没有的话则返回0 其中%AAA%的用法和 SQL语句中like的 ...
- Spot light工具集
Spot light on UNIX 安装没什么问题 Spot light on Oracle 必须安装32位的客户端,不然搞死你 两者的界面都是吊炸天啊
- HDOJ 2037简单的贪心算法
代码: #include<iostream> using namespace std; int main() { int n,s,t1[100],t2[100],i,t,j; while( ...
- 我用Emacs,后来转向Vim——Vim学习之Vim键盘图(绝对值得珍藏)
Emacs本来就比较臃肿,麻烦.当我发现Vim键盘图时,我就渐渐转向Vim,追随Unix/Linux哲学去了.. 我用了Emacs三个月,因为它的学习曲线没Vim陡,这点吸引了,我使用Linux才7. ...