    01: import java.awt.*;
    02:
    03: /**
    04:    A layout manager that lays out components along a central axis
    05: */
    06: public class FormLayout implements LayoutManager
    07: {
    08:    public Dimension preferredLayoutSize(Container parent)
    09:    {
    10:       Component[] components = parent.getComponents();
    11:       left = 0;
    12:       right = 0;
    13:       height = 0;
    14:       for (int i = 0; i < components.length; i += 2)
    15:       {
    16:          Component cleft = components[i];
    17:          Component cright = components[i + 1];
    18:
    19:          Dimension dleft = cleft.getPreferredSize();
    20:          Dimension dright = cright.getPreferredSize();
    21:          left = Math.max(left, dleft.width);
    22:          right = Math.max(right, dright.width);
    23:          height = height + Math.max(dleft.height,
    24:                dright.height);
    25:       }
    26:       return new Dimension(left + GAP + right, height);
    27:    }
    28:
    29:    public Dimension minimumLayoutSize(Container parent)
    30:    {
    31:       return preferredLayoutSize(parent);
    32:    }
    33:
    34:    public void layoutContainer(Container parent)
    35:    {
    36:       preferredLayoutSize(parent); // Sets left, right
    37:
    38:       Component[] components = parent.getComponents();
    39:
    40:       Insets insets = parent.getInsets();
    41:       int xcenter = insets.left + left;
    42:       int y = insets.top;
    43:
    44:       for (int i = 0; i < components.length; i += 2)
    45:       {
    46:          Component cleft = components[i];
    47:          Component cright = components[i + 1];
    48:
    49:          Dimension dleft = cleft.getPreferredSize();
    50:          Dimension dright = cright.getPreferredSize();
    51:
    52:          int height = Math.max(dleft.height, dright.height);
    53:
    54:          cleft.setBounds(xcenter - dleft.width, y + (height
    55:                - dleft.height) / 2, dleft.width, dleft.height);
    56:
    57:          cright.setBounds(xcenter + GAP, y + (height
    58:                - dright.height) / 2, dright.width, dright.height);
    59:          y += height;
    60:       }
    61:    }
    62:
    63:    public void addLayoutComponent(String name, Component comp)
    64:    {}
    65:
    66:    public void removeLayoutComponent(Component comp)
    67:    {}
    68:
    69:    private int left;
    70:    private int right;
    71:    private int height;
    72:    private static final int GAP = 6;
    73: }