📜  Java LayoutManagers-SpringLayout

📅  最后修改于: 2020-10-01 04:00:25             🧑  作者: Mango

Java SpringLayout

SpringLayout根据一组约束来排列其关联容器的子代,约束不过是两个组件边缘之间的水平和垂直距离。每个约束都由一个SpringLayout.Constraint对象表示。

SpringLayout容器的每个子容器以及容器本身都具有一组与之关联的约束。

每个边缘位置取决于另一个边缘的位置。如果添加了约束以创建新的边线,则先前的绑定将被丢弃。 SpringLayout不会自动设置它管理的组件的位置。

嵌套类

Modifier and Type Class Description
static class SpringLayout.Constraints It is a Constraints object helps to govern component’s size and position change in a container that is controlled by SpringLayout

领域

Modifier and Type Field Description
static String BASELINE It specifies the baseline of a component.
static String EAST It specifies the right edge of a component’s bounding rectangle.
static String HEIGHT It specifies the height of a component’s bounding rectangle.
static String HORIZONTAL_CENTER It specifies the horizontal center of a component’s bounding rectangle.
static String NORTH It specifies the top edge of a component’s bounding rectangle.
static String SOUTH It specifies the bottom edge of a component’s bounding rectangle.
static String VERTICAL_CENTER It specifies the vertical center of a component’s bounding rectangle.
static String WEST It specifies the left edge of a component’s bounding rectangle.
static String WIDTH It specifies the width of a component’s bounding rectangle.

有用的方法

Modifier and Type Method Description
void addLayoutComponent(Component component, Object constraints) If constraints is an instance of SpringLayout. Constraints, associates the constraints with the specified component.
void addLayoutComponent(String name, Component c) Has no effect, since this layout manager does not use a per-component string.
Spring getConstraint(String edgeName, Component c) It returns the spring controlling the distance between the specified edge of the component and the top or left edge of its parent.
SpringLayout.Constraints getConstraints(Component c) It returns the constraints for the specified component.
float getLayoutAlignmentX(Container p) It returns 0.5f (centered).
float getLayoutAlignmentY(Container p) It returns 0.5f (centered).
void invalidateLayout(Container p) It Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
void layoutContainer(Container parent) It lays out the specified container.
Dimension maximumLayoutSize(Container parent) It is used to calculates the maximum size dimensions for the specified container, given the components it contains.
Dimension minimumLayoutSize(Container parent) It is used to calculates the minimum size dimensions for the specified container, given the components it contains.
Dimension preferredLayoutSize(Container parent) It is used to calculates the preferred size dimensions for the specified container, given the components it contains.

import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class MySpringDemo {
 private static void createAndShowGUI() {
        JFrame frame = new JFrame("MySpringDemp");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

        JLabel label = new JLabel("Label: ");
        JTextField textField = new JTextField("My Text Field", 15);
        contentPane.add(label);
        contentPane.add(textField);
 
        layout.putConstraint(SpringLayout.WEST, label,6,SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, label,6,SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, textField,6,SpringLayout.EAST, label);
        layout.putConstraint(SpringLayout.NORTH, textField,6,SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, contentPane,6,SpringLayout.EAST, textField);
        layout.putConstraint(SpringLayout.SOUTH, contentPane,6,SpringLayout.SOUTH, textField);

        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

输出: