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);

GroupLayout 是一个 LayoutManager,它将组件按层次分组,以决定它们在 Container 中的位置。GroupLayout 主要供生成器使用,但也可以手工编码。分组由 Group 类的实例来完成。GroupLayout 支持两种组。串行组 (sequential group) 按顺序一个接一个地放置其子元素。并行组 (parallel group) 能够以四种方式对齐其子元素。

每个组可以包含任意数量的元素,其中元素有 GroupComponent 或间隙 (gap)。间隙可被视为一个具有最小大小、首选大小和最大大小的不可见组件。此外,GroupLayout 还支持其值取自 LayoutStyle 的首选间隙。

GroupLayout是一个很重要的是额布局管理器,在jdk 1.6才加入,配合其它的管理器可以实现很好的界面。

GroupLayout必须要设置它的GroupLayout.setHorizontalGroup和GroupLayout.setVerticalGroup。

GroupLayout.setHorizontalGroup是指按照水平来确定,下面例子“账号”和“密码”是一个级别的,其它的组件也是一个级别的。详情请看代码

GroupLayout.setVerticalGroup。是按照垂直来确定的。他们的级别是按照Group去设置组件的优先级别,级别越高就显示越上面。

GroupLayout.setHorizontalGroup(SequentialGroup(ParallelGroup(component)));

大概就是按照这个顺序去添加,当然不是就这么简单设置,多个component添加到ParallelGroup,然后多个ParallelGroup添加到SequentialGroup里面,

然后就设置到GroupLayout

下面的实例,设置GroupLayout.setHorizontalGroup,就是把2和4添加到一个ParallelGroup.addComponent(component),其它1,3,5,6,7,8添加到另一个ParallelGroup,然后把这两个ParallelGroup按照顺序添加到SequentialGroup.addGrou(ParallelGroup);

 
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MyFrame extends javax.swing.JFrame {
public static void main(String[] args) {
MyFrame f = new MyFrame();
} JLabel label1;
JLabel label2;
JLabel label3;
JTextField tf;
JPasswordField psf;
JRadioButton rb1;
JRadioButton rb2; JButton bt1;
JButton bt2; public MyFrame() {
this.setVisible(true);
this.setSize(250, 220);
this.setVisible(true);
this.setLocation(400, 200); label1 = new JLabel("快捷登陆");
label2 = new JLabel("账号:");
label3 = new JLabel("密码:");
tf = new JTextField();
psf = new JPasswordField();
rb1 = new JRadioButton("记住密码");
rb2 = new JRadioButton("自动登陆");
bt1 = new JButton("登陆");
// 为指定的 Container 创建 GroupLayout
GroupLayout layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
//创建GroupLayout的水平连续组,,越先加入的ParallelGroup,优先级级别越高。
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGap(5);//添加间隔
hGroup.addGroup(layout.createParallelGroup().addComponent(label2)
.addComponent(label3));
hGroup.addGap(5);
hGroup.addGroup(layout.createParallelGroup().addComponent(label1)
.addComponent(psf).addComponent(rb1).addComponent(rb2)
.addComponent(tf).addComponent(bt1));
hGroup.addGap(5);
layout.setHorizontalGroup(hGroup);
//创建GroupLayout的垂直连续组,,越先加入的ParallelGroup,优先级级别越高。
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGap(10);
vGroup.addGroup(layout.createParallelGroup().addComponent(label1));
vGroup.addGap(10);
vGroup.addGroup(layout.createParallelGroup().addComponent(label2)
.addComponent(tf));
vGroup.addGap(5);
vGroup.addGroup(layout.createParallelGroup().addComponent(label3)
.addComponent(psf));
vGroup.addGroup(layout.createParallelGroup().addComponent(rb1)); vGroup.addGroup(layout.createParallelGroup().addComponent(rb2));
vGroup.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(bt1));
vGroup.addGap(10);
//设置垂直组
layout.setVerticalGroup(vGroup);
}
}

http://www.cnblogs.com/taoweiji/archive/2012/12/10/2812221.html

Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
    [..]
    is not attached to a vertical group

Add a vertical group and add the components to it.

From the JavaDocs:

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 an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.

http://stackoverflow.com/questions/10472044/regarding-group-layout

GroupLayout gl_composite = new GroupLayout(composite);
91. gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(
92. GroupLayout.LEADING).add(canvas, GroupLayout.DEFAULT_SIZE, 468,
93. Short.MAX_VALUE).add(
94. GroupLayout.TRAILING,
95. gl_composite.createSequentialGroup().addContainerGap(125,
96. Short.MAX_VALUE).add(button_1).add(68).add(btnbutton)
97. .add(131)));
98. gl_composite.setVerticalGroup(gl_composite.createParallelGroup(
99. GroupLayout.LEADING).add(
100. gl_composite.createSequentialGroup().add(canvas,
101. GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE).add(48)
102. .add(
103. gl_composite.createParallelGroup(
104. GroupLayout.BASELINE).add(btnbutton)
105. .add(button_1)).add(81)));

http://blog.csdn.net/captaingan/article/details/7086760

Java 的swing.GroupLayout布局管理器的使用方法和实例(转)的更多相关文章

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

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

  2. Java Swing 盒布局管理器

    Swing 盒布局管理器 因为项目的原因,重新看看swing的东西,在想要将两个横向的容器纵向表示的时候,发现用盒布局 话不多说,直接代码 package ui; import javax.swing ...

  3. JAVA学习Swing章节流布局管理器简单学习

    package com.swing; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton ...

  4. JAVA简单的网格布局管理器--JAVA基础

    网格布局管理器: GridLayoutDemo.java: import java.awt.GridLayout;import javax.swing.JButton;import javax.swi ...

  5. 【java】浅析java组件中的布局管理器

    这篇博文笔者介绍一下java组件中,常用的布局管理器.java组件中的布局方式有好几十种,所有的这些布局管理器都实现了java.awt.LayoutManager接口.接下来笔者介绍一下常用的5种布局 ...

  6. java图形用户界面边界布局管理器

    总结:不同方向的组件,所用的板是不同的: package com.moc; //用布局写一个界面 ///运用边界布局 //2个按钮在北,2个按钮在南 //中央一个大按钮 //将同一方向的组件封装后布局 ...

  7. java 图形化界面 布局管理器

    package Layout; import java.awt.*; import javax.swing.*; public class MyBorderLayout extends JFrame{ ...

  8. Java可视化编程,基于布局管理器的UI设计

    在<事件驱动模型>讲述了如何将用户与功能实现代码联系到一起.怎么样便于用户理解和符合用户的使用习惯? 本篇还是就此问题作分析,站在用户角度上分析UI各组件倒底该如何设计呈现. 优秀的UI会 ...

  9. Swing——布局管理器

    前言 在编写图形界面时,总是需要考虑的就是组件放在哪里,组件怎么大才合适.在Swing中就有现成的布局管理器帮我们做这些事情,我们不必写代码去一一安排.下面将介绍什么是布局管理器.Swing中常用布局 ...

随机推荐

  1. HDU 4284 Travel

    据说是TSP经典问题...可以用状态压缩做.但是看到数据量,就厚着脸皮上搜索了...先floyd预处理每对点间的最小消费,然后只考虑要去的城市就可以了,这样的话城市数最多16个...当时就暴搜了... ...

  2. Android中使用JNI获得APK签名的哈希值

    原地址:http://blog.csdn.net/i5suoi/article/details/19036975 最近在研究android应用中的安全问题,貌似只有将核心代码写到JNI底层才是最安全的 ...

  3. Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS

    Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS Patch to solve sqlite3_ ...

  4. [Java聊天室server]实战之五 读写循环(服务端)

    前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...

  5. 快速入门github的方法

    Sometimes you just need a little help. ps:官方的帮助,原汁原味的帮助!强烈推荐 https://help.github.com/ Pro Git Book C ...

  6. PHP监測memcache服务端的执行状况

    . 代码例如以下,代码为memcache官方代码,引用在此.做一下简单的说明: 1.设置username和password define('ADMIN_USERNAME','admin'); // A ...

  7. SE 2014年5月22日

    一.   用自己的理解描述 tunnel接口状态 up和down的情况都有哪些 Tunnel接口的状态UP:隧道的目标有可达路由. Tunnel接口的状态Down:隧道的目标路由不可达. 如图配置实验 ...

  8. PHP:小数位计算

    本文提供了两种方法,分数的方法成为字符串.然后,"."为了拦截.跟.子长后.另一个是关于小数*10的N钍.实例10的8再次钍8取余次.然后继续10余.取决于10结果的余数是不0. ...

  9. C#之异步编程

    1 异步编程的重要性 C#5.0最重要的改进是提供了更强大的异步编程,C#5.0仅增加两个关键字Async和Await,使用异步编程,方法调用是后台运行(通常在线程和任务的帮助下),并且不会阻塞调用线 ...

  10. zabbix 监控特定进程

    因为一些server上跑着一些重要程序,须要对它们进行监控,公司用的是zabbix监控,之前都是在zabbix中加入自己定义脚本对特定程序进行监控,近期看了zabbix的官方文档,发现原来强大的zab ...