    01: import java.awt.*;
    02:
    03: /**
    04:    A layout manager that lays out components along a central axis
    05: */
    06: 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,
    53:             dright.height);
    54:
    55:          cleft.setBounds(xcenter - dleft.width, y + (height -
    56:             dleft.height) / 2, dleft.width, dleft.height);
    57:
    58:          cright.setBounds(xcenter + GAP, y + (height
    59:             - dright.height) / 2, dright.width, dright.height);
    60:          y += height;
    61:       }
    62:    }
    63:
    64:    public void addLayoutComponent(String name,
    65:       Component comp)
    66:    {}
    67:
    68:    public void removeLayoutComponent(Component comp)
    69:    {}
    70:
    71:    private int left;
    72:    private int right;
    73:    private int height;
    74:    private static final int GAP = 6;
    75: }