java GUI中自动排列组件的工具类

public class GUIUtil {

    // optimization--shared Insets instances
    static Insets insets0_0_0_0 = new Insets(0, 0, 0, 0);

    static Insets insets2_4_0_0 = new Insets(2, 4, 0, 0);

    static Insets insets4_2_0_0 = new Insets(4, 2, 0, 0);

    static Insets insets0_0_2_2 = new Insets(0, 0, 2, 2);

    static Insets insets2_4_2_4 = new Insets(2, 4, 2, 4);

    static Insets insets0_4_2_4 = new Insets(0, 4, 2, 4);

    // Notice in Swing, the hierrachy of inheritance is:
    // Object <- java.awt.Component <- java.awt.Container <- JComponent <-
    // JPanel
    // <- AbstractButton
    // <- etc.
    //
    // This means that the container here can be a JPanel, component can be a
    // JButton.
    public static void constraint(Container container, Component component,
            GridBagLayout gb, int grid_x, int grid_y, int grid_width,
            int grid_height, double weightx, double weighty, int fill,
            int anchor, Insets myInsets) {

        GridBagConstraints gbConstraints = new GridBagConstraints();
        constraint(container, component, gb, gbConstraints, grid_x, grid_y,
                grid_width, grid_height, weightx, weighty, fill, anchor,
                myInsets);
    } // end of constraint().

    public static void constraint(Container container, Component component,
            GridBagLayout gb, GridBagConstraints gbConstraints, int grid_x,
            int grid_y, int grid_width, int grid_height, double weightx,
            double weighty, int fill, int anchor, Insets myInsets) {
        if (gbConstraints == null)
            return;

        gbConstraints.gridx = grid_x;
        gbConstraints.gridy = grid_y;
        gbConstraints.gridwidth = grid_width;
        gbConstraints.gridheight = grid_height;
        gbConstraints.weightx = weightx;
        gbConstraints.weighty = weighty;
        gbConstraints.fill = fill;
        gbConstraints.anchor = anchor;

        if (myInsets == null)
            gbConstraints.insets = insets0_0_0_0;
        else
            gbConstraints.insets = myInsets;

        // Note: gb.setConstraints(component, gbConstraints) seems cannot
        // work when component is a JComponent. However,
        // container.add(component, gbConstraints) works !
        // gb.setConstraints(component,gbConstraints);

        container.add(component, gbConstraints);
    } // end of constraint().

    public static void makePanel(JPanel panel, JComponent[] components,
            LayoutManager layout) {
        panel.setLayout(layout);
        for (int i = 0; i < components.length; i++)
            panel.add(components[i]);
    }

    /**
     * Adds components to a panel with horizontal orientation. Uses
     * ToolbarLayout for layout. Note: Nesting these does not appear to work
     * properly. Consider nesting makeRow()-filled panels inside of
     * makePanel()-filled ones.
     */
    public static void makePanel(JPanel panel, JComponent[] components) {
        makePanel(panel, components, new FlowLayout());
    }

    public static void makePanel(JPanel panel, JComponent[] components,
            int flowAlign) {
        makePanel(panel, components, new FlowLayout(flowAlign));
    }

    public static JPanel makePanel(JComponent[] components, LayoutManager layout) {
        JPanel panel = new JPanel();
        makePanel(panel, components, layout);
        return panel;
    }

    public static JPanel makePanel(JComponent[] components, int flowAlign) {
        JPanel panel = new JPanel();
        FlowLayout layout = new FlowLayout(flowAlign);
        makePanel(panel, components, layout);
        return panel;
    }

    public static JPanel makePanel(JComponent[] components) {
        JPanel panel = new JPanel();
        makePanel(panel, components);
        return panel;
    }

    /**
     * Wraps a component in a JPanel to align it.
     *
     * @param align
     *            GridBagConstraints alignment
     */
    public static JPanel wrapComponentAlign(JComponent component, int align) {
        GridBagLayout gb = new GridBagLayout();
        JPanel panel = new JPanel(gb);

        constraint(panel, component, gb, 0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.BOTH, align, insets0_0_2_2);
        return panel;
    }

    /**
     * Wraps a component to have a very tall or wide preferred size.
     *
     * @param axis
     *            BoxLayout.X_AXIS or BoxLayout.Y_AXIS
     */
    public static JPanel wrapPreferredSize(JComponent component, int axis) {
        return wrapPreferredSize(component, axis, GridBagConstraints.WEST);
    }

    /**
     * Wraps a component to have a very tall or wide preferred size.
     *
     * @param axis
     *            BoxLayout.X_AXIS or BoxLayout.Y_AXIS
     * @param align
     *            GridBagConstraints alignment
     */
    public static JPanel wrapPreferredSize(JComponent component, int axis,
            int align) {
        JPanel panel = wrapComponentAlign(component, align);

        Dimension dim = component.getPreferredSize();

        if (axis == BoxLayout.X_AXIS) {
            dim.width = 2000;
        } else {
            dim.height = 2000;
        }
        panel.setPreferredSize(dim);

        return panel;
    }

    /** Lays out components in a row using BoxLayout. */
    public static JPanel makeBoxRow(JComponent[] components) {
        return makeBox(components, BoxLayout.X_AXIS, GridBagConstraints.WEST);
    }

    /** Lays out components in a column using BoxLayout. */
    public static JPanel makeBoxColumn(JComponent[] components) {
        return makeBox(components, BoxLayout.Y_AXIS, GridBagConstraints.WEST);
    }

    /** Lays out components in a column using BoxLayout. */
    public static void makeBoxColumn(JPanel panel, JComponent[] components) {
        makeBox(panel, components, BoxLayout.Y_AXIS, GridBagConstraints.WEST);
    }

    /** Lays out components in a row using BoxLayout. */
    public static void makeBoxRow(JPanel panel, JComponent[] components) {
        makeBox(panel, components, BoxLayout.X_AXIS, GridBagConstraints.WEST);
    }

    /**
     * Lays out components using BoxLayout.
     *
     * @param axis
     *            BoxLayout.X_AXIS or BoxLayout.Y_AXIS
     * @param align
     *            GridBagConstraints alignment
     */
    public static JPanel makeBox(JComponent[] components, int axis, int align) {
        JPanel panel = new JPanel();
        makeBox(panel, components, axis, align);
        return panel;
    }

    /**
     * Lays out components using BoxLayout.
     *
     * @param axis
     *            BoxLayout.X_AXIS or BoxLayout.Y_AXIS
     * @param align
     *            GridBagConstraints alignment
     */
    public static void makeBox(JPanel panel, JComponent[] components, int axis,
            int align) {
        panel.setLayout(new BoxLayout(panel, axis));
        for (int i = 0; i < components.length; i++) {
            JPanel panelC = null;
            if (components[i] instanceof JPanel) {
                panelC = (JPanel) components[i];
            } else {
                panelC = wrapComponentAlign(components[i], align);
            }
            panel.add(panelC);
        }
    }

    public static JPanel makeColumn(JComponent[] components) {
        return makeColumn(components, GridBagConstraints.WEST);
    }

    public static void makeColumn(JPanel container, JComponent[] comList) {
        makeColumn(container, comList, GridBagConstraints.WEST);
    }

    public static JPanel makeColumn(JComponent[] components, int anchor) {
        return makeColumn(components, anchor, GridBagConstraints.HORIZONTAL);
    }

    public static void makeColumn(JPanel container, JComponent[] comList,
            int anchor) {
        makeColumn(container, comList, anchor, GridBagConstraints.HORIZONTAL);
    }

    public static JPanel makeColumn(JComponent[] components, int anchor,
            int fill) {
        JPanel container = new JPanel();
        makeColumn(container, components, anchor, fill);
        return container;
    }

    public static void makeColumn(JPanel container, JComponent[] comList,
            int anchor, int fill) {
        if (comList == null)
            return;

        GridBagLayout gbLayout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        container.setLayout(gbLayout);

        for (int i = 0; i < comList.length; i++) {
            GUIUtil.constraint(container, comList[i], gbLayout, c, 0, i, 1, 1,
                    1, 1, fill, anchor, insets2_4_0_0);
        }
    }

    public static JPanel makeRow(JComponent[] components) {
        return makeRow(components, GridBagConstraints.CENTER);
    }

    public static JPanel makeRow(JComponent[] components, int anchor) {
        return makeRow(components, anchor, GridBagConstraints.NONE);
    }

    public static JPanel makeRow(JComponent[] components, int anchor, int fill) {
        JPanel container = new JPanel();
        makeRow(container, components, anchor, fill);
        return container;
    }

    public static JPanel makeRow(JComponent[] components, int anchor, int fill,
            Insets insets) {
        JPanel container = new JPanel();
        makeRow(container, components, anchor, fill, insets);
        return container;
    }

    public static void makeRow(JPanel container, JComponent[] components,
            int anchor, int fill) {
        makeRow(container, components, anchor, fill, insets4_2_0_0);
    }

    public static void makeRow(JPanel container, JComponent[] components,
            int anchor, int fill, Insets insets) {
        GridBagConstraints c = new GridBagConstraints();
        GridBagLayout gb = new GridBagLayout();
        container.setLayout(gb);

        for (int i = 0; i < components.length; i++) {
            GUIUtil.constraint(container, components[i], gb, c, i, 0, 1, 1,
                    1.0, 1.0, fill, anchor, insets);
        }
    }

    public static JPanel make2Columns(JComponent[] leftCol,
            JComponent[] rightCol) {
        if (leftCol == null || rightCol == null)
            return null;

        JPanel container = new JPanel();

        make2Columns(container, leftCol, rightCol);

        return container;
    }

    public static void make2Columns(JPanel container, JComponent[] leftCol,
            JComponent[] rightCol) {
        GridBagLayout gbLayout = new GridBagLayout();
        container.setLayout(gbLayout);

        for (int i = 0; i < leftCol.length; i++) {
            GUIUtil.constraint(container, leftCol[i], gbLayout, 0, i, 1, 1,
                    0.5, 0, GridBagConstraints.HORIZONTAL,
                    GridBagConstraints.CENTER, insets0_0_2_2);

            GUIUtil.constraint(container, rightCol[i], gbLayout, 1, i, 1, 1, 1,
                    0, GridBagConstraints.HORIZONTAL,
                    GridBagConstraints.CENTER, insets0_0_2_2);
        }
    }

    // add by jack huang on 2005-05-16 for Odd Number Columns
    public static void make2ColumnsForOdd(JPanel container,
            JComponent[] leftCol, JComponent[] rightCol) {
        GridBagLayout gbLayout = new GridBagLayout();
        container.setLayout(gbLayout);

        for (int i = 0; i < leftCol.length; i++) {
            if (i == 2) {
                GUIUtil.constraint(container, leftCol[i], gbLayout, 0, i, 1, 1,
                        0.5, 0, GridBagConstraints.HORIZONTAL,
                        GridBagConstraints.CENTER, insets0_0_2_2);

            } else {
                GUIUtil.constraint(container, leftCol[i], gbLayout, 0, i, 1, 1,
                        0.5, 0, GridBagConstraints.HORIZONTAL,
                        GridBagConstraints.CENTER, insets0_0_2_2);

                GUIUtil.constraint(container, rightCol[i], gbLayout, 1, i, 1,
                        1, 1, 0, GridBagConstraints.HORIZONTAL,
                        GridBagConstraints.CENTER, insets0_0_2_2);
            }
        }

    }

    public static JPanel make2Rows(JComponent j1, JComponent j2) {
        if (j1 == null || j2 == null)
            return null;

        JPanel container = new JPanel();
        GridBagLayout gbLayout = new GridBagLayout();
        container.setLayout(gbLayout);

        GUIUtil.constraint(container, j1, gbLayout, 0, 0, 1, 1, 100, 0,
                GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,
                insets2_4_2_4);

        GUIUtil.constraint(container, j2, gbLayout, 0, 1, 1, 1, 100, 0,
                GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,
                insets0_4_2_4);
        return container;
    }

    public static JPanel makeRow(JComponent j1, JComponent j2) {
        if (j1 == null || j2 == null)
            return null;

        JPanel container = new JPanel();
        GridBagLayout gbLayout = new GridBagLayout();
        container.setLayout(gbLayout);

        GUIUtil.constraint(container, j1, gbLayout, 0, 0, 1, 1, 0.2, 0,
                GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,
                insets0_0_2_2);

        GUIUtil.constraint(container, j2, gbLayout, 1, 0, 1, 1, 1, 0,
                GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,
                insets0_0_2_2);
        return container;
    }

    public static JPanel makePanel(Component component) {
        JPanel panel = new JPanel();
        panel.add(component);
        return panel;
    }

    public static JLabel[] makeLabels(String[] strings) {
        JLabel[] lblRet = new JLabel[strings.length];
        for (int i = 0; i < strings.length; i++) {
            lblRet[i] = new JLabel(strings[i]);
        }
        return lblRet;
    }

    /**
     * A helper function to remove components from a container such as JFrame or
     * JDialog. This code was copied from
     * http://forum.java.sun.com/read/16799962/q_Mryu1nKp4sAAaQk#LR This code
     * manually removes components from containers in order to allow garbage
     * collection to clean it up. If Swing didn't have memory leaks this code
     * would not be necessary, however it doesn't hurt.
     */
    public static final void removeAllComponents(Container cont) {
        Component[] components = cont.getComponents();

        Component comp;

        for (int i = 0; i < components.length; i++) {
            comp = components[i];

            if (comp != null) {
                if (comp instanceof Container) {
                    removeAllComponents((Container) comp);
                }

                comp.transferFocus();
                comp.removeNotify();
                cont.remove(comp);
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值