Swing - 更改LayoutManager:每行两个组件

时间:2017-02-18 14:23:02

标签: java swing layout layout-manager

我正在创建一个Java应用程序,目前我使用这个布局管理器:

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

/**
 * @author Pasban
 */

public class VerticalLayout implements LayoutManager {

    public final static int CENTER = 0;
    public final static int RIGHT = 1;
    public final static int LEFT = 2;
    public final static int BOTH = 3;
    public final static int TOP = 1;
    public final static int BOTTOM = 2;
    private int vgap;
    private int alignment;
    private int anchor;

    public VerticalLayout() {
        this(5, CENTER, TOP);
    }

    public VerticalLayout(int vgap) {
        this(vgap, CENTER, TOP);
    }

    public VerticalLayout(int vgap, int alignment) {
        this(vgap, alignment, TOP);
    }

    public VerticalLayout(int vgap, int alignment, int anchor) {
        this.vgap = vgap;
        this.alignment = alignment;
        this.anchor = anchor;
    }

    private Dimension layoutSize(Container parent, boolean minimum) {
        Dimension dim = new Dimension(0, 0);
        Dimension d;
        synchronized (parent.getTreeLock()) {
            int n = parent.getComponentCount();
            for (int i = 0; i < n; i++) {
                Component c = parent.getComponent(i);
                if (c.isVisible()) {
                    d = minimum ? c.getMinimumSize() : c.getPreferredSize();
                    dim.width = Math.max(dim.width, d.width);
                    dim.height += d.height;
                    if (i > 0) {
                        dim.height += vgap;
                    }
                }
            }
        }
        Insets insets = parent.getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom + vgap + vgap;
        return dim;
    }

    public void layoutContainer(Container parent) {
        Insets insets = parent.getInsets();
        synchronized (parent.getTreeLock()) {
            int n = parent.getComponentCount();
            Dimension pd = parent.getSize();
            int y = 0;
            for (int i = 0; i < n; i++) {
                Component c = parent.getComponent(i);
                Dimension d = c.getPreferredSize();
                if (c.isVisible()) {
                    y += d.height + vgap;
                }
            }
            y -= vgap;
            if (anchor == TOP) {
                y = insets.top;
            } else if (anchor == CENTER) {
                y = (pd.height - y) / 2;
            } else {
                y = pd.height - y - insets.bottom;
            }
            for (int i = 0; i < n; i++) {
                Component c = parent.getComponent(i);
                Dimension d = c.getPreferredSize();
                if (!c.isVisible()) {
                    continue;
                }
                int x = 1;
                int wid = pd.width - 3;
                c.setBounds(x, y, wid, d.height);
                y += d.height + vgap;
            }
        }
    }

    public Dimension minimumLayoutSize(Container parent) {
        return layoutSize(parent, false);
    }

    public Dimension preferredLayoutSize(Container parent) {
        return layoutSize(parent, false);
    }

    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }

    public String toString() {
        return getClass().getName() + "[vgap=" + vgap + " align=" + alignment + " anchor=" + anchor + "]";
    }
}

使用示例(组件垂直对齐):

https://i.stack.imgur.com/uGtbz.png

如您所见,每个组件都在一行上。但是,我希望一些组件在一条线上。

例如,JButton将在一条线上,就像现在一样。 JTextAreas也是如此。

但是,我想让JLabel和JTextFields在同一条线上。 JLabels和JComboBoxs在同一条线上也一样。

而且我真的不知道该怎么做。有人可以解释一下吗? :d

2 个答案:

答案 0 :(得分:1)

首先,很难准确理解为什么要创建自己的布局? 看看GridBagLayout

https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

使用GridBagLayout可以轻松解决您正在解释的用例。

答案 1 :(得分:0)

如果您想在每一行上使用两个组件,请使用标准GridLayout

您可以通过指定只需要两列组件来强制组件换行:

GridLayout layout = new GridLayout(0, 2);
JPanel panel = new JPanel( layout );