JAVA深入理解Java图形化编程

Java图形化编程之布局管理器

2017-09-06  本文已影响27人  旅旅人

在Java图形化编程中,我们根据不同的业务场景选择不同的布局容器来满足我们的需求。那么Java为我们提供了多少布局容器呢,下图为布局管理器与常见布局容器之间的关系

常见布局及布局管理器关系图

LayoutManger

public interface LayoutManager {
    /**
     * If the layout manager uses a per-component string,
     * adds the component <code>comp</code> to the layout,
     * associating it
     * with the string specified by <code>name</code>.
     * 如果布局管理器对每个组件使用字符串表示,即将组件<code>comp</code>添加到布局,
     * 与之关联的是一个特定的字符串<code>name</code>.
     *
     * @param name the string to be associated with the component
     *             与组件关联的字符串
     * @param comp the component to be added
     *             被添加的组件
     */
    void addLayoutComponent(String name, Component comp);

    /**
     * Removes the specified component from the layout.
     * 从布局中移除组件
     * @param comp the component to be removed 被移除的组件
     */
    void removeLayoutComponent(Component comp);

    /**
     * Calculates the preferred size dimensions for the specified
     * container, given the components it contains.
     *
     * 根据它包含的组件,计算指定容器的首选大小尺寸
     *
     * @param parent the container to be laid out 父容器
     *
     * @see #minimumLayoutSize
     */
    Dimension preferredLayoutSize(Container parent);

    /**
     * Calculates the minimum size dimensions for the specified
     * container, given the components it contains.
     *
     * 根据它包含的组件,计算指定容器的最小大小尺寸
     * @param parent the component to be laid out
     * @see #preferredLayoutSize
     */
    Dimension minimumLayoutSize(Container parent);

    /**
     * Lays out the specified container.
     * 布置指定容器
     * @param parent the container to be laid out
     */
    void layoutContainer(Container parent);
}

LayoutManager2

public interface LayoutManager2 extends LayoutManager {

    /**
     * Adds the specified component to the layout, using the specified
     * constraint object.
     * 使用指定的约束类将指定的组件添加到布局中
     * @param comp the component to be added 被添加的组件
     * @param constraints  where/how the component is added to the layout.
     *                     约束组件在什么地方如何添加到布局中
     */
    void addLayoutComponent(Component comp, Object constraints);

    /**
     * Calculates the maximum size dimensions for the specified container,
     * given the components it contains.
     * 根据所包含的组件计算指定容器的最大尺寸大小
     * @see java.awt.Component#getMaximumSize
     * @see LayoutManager
     */
    public Dimension maximumLayoutSize(Container target);

    /**
     * Returns the alignment along the x axis.  This specifies how
     * the component would like to be aligned relative to other
     * components.  The value should be a number between 0 and 1
     * where 0 represents alignment along the origin, 1 is aligned
     * the furthest away from the origin, 0.5 is centered, etc.
     *
     * 返回沿着X轴对齐方式. 这指定了组件如何相对于其他组件进行对齐。
     * 该值应为0到1之间的数字
     * 其中0表示原点对齐,1对齐最距离起始点最远,0.5为中心等
     */
    public float getLayoutAlignmentX(Container target);

    /**
     * Returns the alignment along the y axis.  This specifies how
     * the component would like to be aligned relative to other
     * components.  The value should be a number between 0 and 1
     * where 0 represents alignment along the origin, 1 is aligned
     * the furthest away from the origin, 0.5 is centered, etc.
     *
     * 返回沿着y轴对齐方式. 这指定了组件像相对于其他组件进行对齐。
     * 该值应为0到1之间的数字
     * 其中0表示原点对齐 1表示距离起始点最远 0.5表示中心等
     */
    public float getLayoutAlignmentY(Container target);

    /**
     * Invalidates the layout, indicating that if the layout manager
     * has cached information it should be discarded.
     * 使布局无效,需要注意的是如果布局管理器已缓存信息,则应将其丢弃掉.
     */
    public void invalidateLayout(Container target);
}

常见布局容器

布局名称 说明
GridLayout 以M*N列的格子形式来放置组件,每个格子只能容纳一个组件
FlowLayout 组件以添加的顺序根据相对应的对齐方式从左到右进行排列,一行放置不下时换行放置。
OverlayLayout 支持组件之间根据设置的相应的规则进行重叠放置
GroupLayout 组件以层次分组,根据分组决定它们在容器中的位置
BoxLayout 允许多个组件垂直或者水平布置,组件不会被包裹
JRootPane.RootLayout 负责layeredPane, glassPane和menuBar的布局。
CardLayout 每个组件被视为一张卡片,一次只能看到一张卡片,卡的顺序由容器自己的组件对象的内部顺序决定
GridBagLayout 组件可以垂直,水平或沿其基线对齐,不需要组件大小相同
SpringLayout 根据一组约束排列其关联容器的组件
BorderLayout(补,继承LayoutManager2) 用一个容器在五个区域:北,南,东,西和中心内安排和放置组件。 每个区域最多放置一个组件
上一篇下一篇

猜你喜欢

热点阅读