文档说明:

以下引自: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 ContainerGroupLayout is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the Group class. GroupLayoutsupports 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 Components is determined from theComponent's getMinimumSizegetPreferredSize and getMaximumSize methods. In addition, when adding Components 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 c2and 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 Components. 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 of Group.
  • 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 Groups; 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 布局的更多相关文章

  1. (转)Java 的swing.GroupLayout布局管理器的使用方法和实例

    摘自http://www.cnblogs.com/lionden/archive/2012/12/11/grouplayout.html (转)Java 的swing.GroupLayout布局管理器 ...

  2. Java 的swing.GroupLayout布局管理器的使用方法和实例(转)

    The following builds a panel consisting of two labels in one column, followed by two textfields in t ...

  3. GUI(GroupLayout 分组布局)

    组:一些组件的集合                       连续组:一个接着一个摆放                       并行组:一个组在另一个组的顶部                   ...

  4. Java Swing 第03记 布局管理器

    几种Swing常用的布局管理器 BorderLaout 它将容器分为5个部分,即东.南.西.北.中,每一个区域可以容纳一个组件,使用的时候也是通过BorderLayout中5个方位常量来确定组件所在的 ...

  5. 前端框架 EasyUI (2)页面布局 Layout

    在 Web 程序中,页面布局对应用程序的用户体验至关重要. 在一般的信息管理类的 Web 应用程序中,页面结构通常有一个主工作区,然后在工作区上下左右靠近边界的区域设置一些边栏,用于显示信息或放置一些 ...

  6. TODO:Laravel 使用blade标签布局页面

    TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...

  7. CSS HTML元素布局及Display属性

    本篇文章主要介绍HTML的内联元素.块级元素的分类与布局,以及dispaly属性对布局的影响. 目录 1. HTML 元素分类:介绍内联元素.块级元素的分类. 2. HTML 元素布局:介绍内联元素. ...

  8. 谈谈一些有趣的CSS题目(六)-- 全兼容的多列均匀布局问题

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  9. Xamarin+Prism开发详解五:页面布局基础知识

    说实在的研究Xamarin到现在,自己就没设计出一款好的UI,基本都在研究后台逻辑之类的!作为Xamarin爱好者,一些简单的页面布局知识还是必备的. 布局常见标签: StackLayout Abso ...

随机推荐

  1. IOS-tableViewCell重用机制

    IOS-tableViewCell重用机制 首先介绍tableViewCell原生cell的使用和重用机制 使用dequeueReusableCellWithIdentifier方法,这个方法会从缓存 ...

  2. 关于一点coding.net与git配合在AndroidStudio/Idea上的使用笔记个的

    编写程序的我们经常需要对我们写的代码做版本控制,或者分支管理,具备类似功能的软件很多,诸如SVN,Git,CVS等等!但配置版本控制服务器(SVN server etc.)是繁琐的并且需要一定的成本! ...

  3. nest 'for' loop.

    /* nest for loop demo. Note that,'upside' triangle controls 'inner condition'. */ import kju.print.P ...

  4. Linux运行C#程序

    首先需要安装mono 安装教程http://www.cnblogs.com/aixunsoft/p/3422099.html 然后 用终端执行C#程序就可以了,mono 程序文件名 可以直接执行win ...

  5. js正则实现用户输入银行卡号的控制及格式化

    //js正则实现用户输入银行卡号的控制及格式化 <script language="javascript" type="text/javascript"& ...

  6. iOS 去除导航栏下的黑线

    Swift: navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) na ...

  7. 用JS实现版面拖拽效果

    类似于这样的一个版面,点击标题栏,实现拖拽效果. 添加onmousedown事件 通过获取鼠标的坐标(clientX,clientY)来改变面板的位置 注意:面板使用绝对定位方式,是以左上角为参考点, ...

  8. nextDay、beforeDay以及根据nextDay(beforeDay)求解几天后的日期,几天前的日期和两个日期之间的天数

    实现代码: package com.corejava.chap02; public class Date { private int year; private int month; private ...

  9. JS获取IP

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪多地域测试方法:http://int.dpool.s ...

  10. PHP数据库

    目录 1.创建数据库连接 2.创建数据库 3.选择数据库 4.设置当前连接使用的字符编码 5.创建表 6.插入数据 7.取得数据查询结果 8.关闭连接 1.创建数据库连接 //mysql_connec ...