Java SE 6 中包含一个新的 GroupLayout ,从GroupLayout的单词意思来看,它是以Group(组)为单位来管理布局,也就是把多个组件(如:JLable、JButton)按区域划分到不同的Group(组),再根据各个Group(组)相对于水平轴(Horizontal)和垂直轴(Vertical)的排列方式来管理。
对于GroupLayout来说,需要从两个方向对组件进行分组: 水平方向(Horizontal) 和 垂直方向(Vertical),某一个方向上又有两种排列方式,连续排列sequentialGroup 和 平行排列parallelGroup
下面我们针 对这样一个界面看看它的用法:
1、按水平(Horizontal)方向划分:
hpg1 : JLabel 所在组(只有一个组件可以不用分组)
hpg2a : 左边两个JCheckBox平行于水平轴(Horizontal)排列,构成一个ParallelGroup
hpg2b : 右边两个JChecKBox平行于水平轴(Horizontal)排列,构成一个ParallelGroup
hpg2H : hpg2a 和 hpg2b 两个组沿着水平轴(Horizontal)顺序排列,构成一个SequentialGroup
hpg2 : JTextBox和组hpg2H 平行于水平轴(Horizontal)排列,构成一个ParallelGroup
hpg3 : 两个JTextBox平行于水平轴(Horizontal)排列,构成一个ParallelGroup
另外JLabel 、hpg2、hpg3 沿着水平轴(Horizontal)顺序排列,构成一个SequentialGroup
2、按垂直(Vertical)方向划分:
vpg1 : JLabel、JTextBox 、JButton(Find)平行于垂直轴(Vertical)排列,构成一个ParallelGroup
vpg2 : 上边两个JChecKBox、JButton(Cancel)平行于垂直轴(Vertical)排列,构成一个ParallelGroup
vpg3 : 下边两个JChecKBox平行于垂直轴(Vertical)排列,构成一个ParallelGroup
另外vpg1、vpg2、vpg3 三个组沿着垂直轴(Vertical)顺序排列,构成一个SequentialGroup
3、编码:水平和垂直两个方向我们都必须设定,实现方法详见程序。
addComponent:向Group里添加组件,如JLabel、JTextBox
addGroup : 向Group里添加组
/// GLayout.java
package lee;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GLayout extends JFrame
{
public GLayout()
{
super("Find");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//String laf = UIManager.getCrossPlatformLookAndFeelClassName();
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
}
JLabel label1 = new JLabel("Find What:");
JTextField textField1 = new JTextField();
JCheckBox caseCheckBox = new JCheckBox("Match Case");
JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
JCheckBox wrapCheckBox = new JCheckBox("Warp Around");
JCheckBox backCheckBox = new JCheckBox("Search Backwards");
JButton findButton = new JButton("Find");
JButton cancelButton = new JButton("Cancel");
Container c = getContentPane();
GroupLayout layout = new GroupLayout(c);
c.setLayout(layout);
//自动设定组件、组之间的间隙
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//LEADING -- 左对齐 BASELINE -- 底部对齐 CENTER -- 中心对齐
GroupLayout.ParallelGroup hpg2a = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg2a.addComponent(caseCheckBox);
hpg2a.addComponent(wholeCheckBox);
GroupLayout.ParallelGroup hpg2b = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg2b.addComponent(wrapCheckBox);
hpg2b.addComponent(backCheckBox);
GroupLayout.SequentialGroup hpg2H = layout.createSequentialGroup();
hpg2H.addGroup(hpg2a).addGroup(hpg2b);
GroupLayout.ParallelGroup hpg2 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg2.addComponent(textField1);
hpg2.addGroup(hpg2H);
GroupLayout.ParallelGroup hpg3 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg3.addComponent(findButton);
hpg3.addComponent(cancelButton);
//水平
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(label1).addGroup(hpg2).addGroup(hpg3));
//设定两个Button在水平方向一样宽
layout.linkSize(SwingConstants.HORIZONTAL,new Component[] { findButton, cancelButton });
//layout.linkSize(SwingConstants.HORIZONTAL,new Component[] { caseCheckBox, wholeCheckBox, wrapCheckBox, backCheckBox});
GroupLayout.ParallelGroup vpg1 = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
vpg1.addComponent(label1);
vpg1.addComponent(textField1);
vpg1.addComponent(findButton);
GroupLayout.ParallelGroup vpg2 = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
vpg2.addComponent(caseCheckBox);
vpg2.addComponent(wrapCheckBox);
vpg2.addComponent(cancelButton);
GroupLayout.ParallelGroup vpg3 = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
vpg3.addComponent(wholeCheckBox);
vpg3.addComponent(backCheckBox);
//垂直
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(vpg1).addGroup(vpg2).addGroup(vpg3));
setLocation(200,200);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new GLayout();
}
}