GroupLayout
类对组件进行分层分组,以便将它们放置在容器中。
类声明
以下是javax.swing.GroupLayout
类的声明 -
public class GroupLayout
extends Object
implements LayoutManager2
字段
以下是javax.swing.GroupLayout
类的字段 -
static int DEFAULT_SIZE
- 指示组件的大小或间隙应用于特定范围值。static int PREFERRED_SIZE
- 表示组件的首选大小或间隙应用于特定范围值。
类构造函数
编号 | 构造函数 | 描述 |
---|---|---|
1 | GroupLayout(Container host) |
类方法
编号 | 类方法 | 描述 |
---|---|---|
1 | void addLayoutComponent(Component component, Object constraints) |
已将组件添加到父容器的通知。 |
2 | void addLayoutComponent(String name, Component component) |
已将组件添加到父容器的通知。 |
3 | GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop) |
创建并返回一个ParallelGroup ,它沿着基线对齐它的元素。 |
4 | GroupLayout.ParallelGroup createParallelGroup() |
创建并返回一个Alignment.LEADING 对齐的ParallelGroup 。 |
5 | GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment) |
创建并返回具有指定对齐方式的ParallelGroup 。 |
6 | GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable) |
创建并返回具有指定对齐和调整大小行为的ParallelGroup 。 |
7 | GroupLayout.SequentialGroup createSequentialGroup() |
创建并返回SequentialGroup 。 |
8 | boolean getAutoCreateContainerGaps() |
如果自动创建容器与边界容器的组件之间的间隙,则返回true 。 |
9 | boolean getAutoCreateGaps() |
如果自动创建组件之间的间隙,则返回true 。 |
10 | boolean getHonorsVisibility() |
返回在调整和定位组件时是否考虑组件可见性。 |
11 | float getLayoutAlignmentX(Container parent) |
返回沿x 轴的对齐方式。 |
12 | float getLayoutAlignmentY(Container parent) |
返回沿y 轴的对齐方式。 |
13 | LayoutStyle getLayoutStyle() |
返回用于计算组件之间的首选间隙的LayoutStyle 。 |
14 | void invalidateLayout(Container parent) |
使布局无效,表明如果布局管理器缓存了信息,则应将其丢弃。 |
15 | void layoutContainer(Container parent) |
布置指定的容器。 |
16 | void linkSize(Component... components) |
强制指定的组件具有相同的大小,无论其首选大小,最小大小或最大大小如何。 |
17 | void linkSize(int axis, Component... components) |
强制指定的组件沿指定的轴具有相同的大小,而不管它们的首选,最小或最大大小。 |
18 | Dimension maximumLayoutSize(Container parent) |
返回指定容器的最大大小。 |
19 | Dimension minimumLayoutSize(Container parent) |
返回指定容器的最小大小。 |
20 | Dimension preferredLayoutSize(Container parent) |
返回指定容器的首选大小。 |
21 | void removeLayoutComponent(Component component) |
通知已从父容器中删除组件。 |
22 | void replace(Component existingComponent, Component newComponent) |
用新的组件替换现有组件。 |
23 | void setAutoCreateContainerGaps(boolean autoCreateContainerPadding) |
设置是否应自动创建容器与触摸容器边框的组件之间的间隙。 |
24 | void setAutoCreateGaps(boolean autoCreatePadding) |
设置是否应自动创建组件之间的间隙。 |
25 | void setHonorsVisibility(boolean honorsVisibility) |
设置在调整和定位组件时是否考虑组件可见性。 |
26 | void setHonorsVisibility(Component component, Boolean honorsVisibility) |
设置是否考虑组件可见性以确定大小和位置。 |
27 | void setHorizontalGroup(GroupLayout.Group group) |
设置用于沿横轴定位和调整组件的组。 |
28 | void setLayoutStyle(LayoutStyle layoutStyle) |
设置用于计算组件之间首选间隙的LayoutStyle 。 |
29 | void setVerticalGroup(GroupLayout.Group group) |
沿垂直轴定位和调整组件大小设置组。 |
30 | String toString() |
返回此GroupLayout 的字符串表示形式。 |
方法继承
该类继承以下类中的方法 -
java.lang.Object
GroupLayout示例
使用编辑器创建以下Java程序:GroupLayoutDemo.java
package com.yiibai.layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GroupLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public GroupLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
GroupLayoutDemo swingLayoutDemo = new GroupLayoutDemo();
swingLayoutDemo.showGroupLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING GroupLayoutDemo(yiibai.com)");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showGroupLayoutDemo(){
headerLabel.setText("Layout in action: GroupLayout");
JPanel panel = new JPanel();
// panel.setBackground(Color.darkGray);
panel.setSize(200,200);
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JButton btn1 = new JButton("Button - A");
JButton btn2 = new JButton("Button - B");
JButton btn3 = new JButton("Button - C");
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(btn2)
.addComponent(btn3))));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addComponent(btn2)
.addComponent(btn3));
panel.setLayout(layout);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
执行上面示例代码,得到以下结果: