📜  Java LayoutManagers-GroupLayout

📅  最后修改于: 2020-10-01 03:59:56             🧑  作者: Mango

组布局

GroupLayout将其组件分组并将其分层放置在Container中。分组是通过Group类的实例完成的。

Group是一个抽象类,实现此Group类的两个具体类是SequentialGroup和ParallelGroup。

SequentialGroup依次将其子项依次放置,其中ParallelGroup将其子项彼此对齐。

GroupLayout类提供诸如createParallelGroup()和createSequentialGroup()之类的方法来创建组。

GroupLayout独立对待每个轴。即,存在代表水平轴的组和代表垂直轴的组。每个组件都必须同时存在于水平和垂直组中,否则在布局期间或请求最小,首选或最大大小时会抛出IllegalStateException。

嵌套类

Modifier and Type Class Description
static class GroupLayout.Alignment Enumeration of the possible ways ParallelGroup can align its children.
class GroupLayout.Group Group provides the basis for the two types of operations supported by GroupLayout: laying out components one after another (SequentialGroup) or aligned (ParallelGroup).
class GroupLayout.ParallelGroup It is a Group that aligns and sizes it’s children.
class GroupLayout.SequentialGroup It is a Group that positions and sizes its elements sequentially, one after another.

领域

Modifier and Type Field Description
static int DEFAULT_SIZE It indicates the size from the component or gap should be used for a particular range value.
static int PREFERRED_SIZE It indicates the preferred size from the component or gap should be used for a particular range value.

建设者

GroupLayout(Container host) It creates a GroupLayout for the specified Container.

有用的方法

Modifier and Type Field Description
void addLayoutComponent(Component component, Object constraints) It notify that a Component has been added to the parent container.
void addLayoutComponent(String name, Component component) It notify that a Component has been added to the parent container.
GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop) It creates and returns a ParallelGroup that aligns it’s elements along the baseline.
GroupLayout.ParallelGroup createParallelGroup() It creates and returns a ParallelGroup with an alignment of Alignment.LEADING
GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment) It creates and returns a ParallelGroup with the specified alignment.
GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable) It creates and returns a ParallelGroup with the specified alignment and resize behavior.
GroupLayout.SequentialGroup createSequentialGroup() It creates and returns a SequentialGroup.
boolean getAutoCreateContainerGaps() It returns true if gaps between the container and components that border the container are automatically created.
boolean getAutoCreateGaps() It returns true if gaps between components are automatically created.
boolean getHonorsVisibility() It returns whether component visiblity is considered when sizing and positioning components.
float getLayoutAlignmentX(Container parent) It returns the alignment along the x axis.
float getLayoutAlignmentY(Container parent) It returns the alignment along the y axis.
Dimension maximumLayoutSize(Container parent) It returns the maximum size for the specified container.

public class GroupExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayoutExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel = frame.getContentPane();
GroupLayout groupLayout = new GroupLayout(contentPanel);

contentPanel.setLayout(groupLayout);

JLabel clickMe = new JLabel("Click Here");
JButton button = new JButton("This Button");

groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addComponent(clickMe)
.addGap(10, 20, 100)
.addComponent(button));
groupLayout.setVerticalGroup(
     groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    .addComponent(clickMe)
.addComponent(button));
frame.pack();
frame.setVisible(true);
}
}

输出:

例子2

import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.GroupLayout.Alignment.*;
public class GroupExample2 {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayoutExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myPanel = frame.getContentPane();

GroupLayout groupLayout = new GroupLayout(myPanel);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
myPanel.setLayout(groupLayout);

JButton b1 = new JButton("Button One");
JButton b2 = new JButton("Button Two");
JButton b3 = new JButton("Button Three");

groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(LEADING).addComponent(b1).addComponent(b3))
.addGroup(groupLayout.createParallelGroup(TRAILING).addComponent(b2)));

groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(BASELINE).addComponent(b1).addComponent(b2))
.addGroup(groupLayout.createParallelGroup(BASELINE).addComponent(b3)));

frame.pack();
frame.setVisible(true);
}
}

输出: