美文网首页JAVA深入理解Java图形化编程
Java图形化编程之布局管理器

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

作者: 旅旅人 | 来源:发表于2017-09-06 17:38 被阅读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

  • 这个接口是LayoutManager的一个扩展类,提出了一种思想:基于布局约束对象去布局容器。根据约束对象我们可以知道组件添加到布局的方式和位置
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) 用一个容器在五个区域:北,南,东,西和中心内安排和放置组件。 每个区域最多放置一个组件

相关文章

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

    在Java图形化编程中,我们根据不同的业务场景选择不同的布局容器来满足我们的需求。那么Java为我们提供了多少布局...

  • 迟到的java总结

    1.默认布局管理器 1.1 Java的Jframe和Jwindow的默认布局管理器为边界式布局管理器 1.2 而J...

  • Java基础08GUI

    Java 基础07IO GUI四大布局 FlowLayout(流式布局管理器)它是Panel默认的布局管理器。 B...

  • SWT&JFace 常用布局

    常用布局管理器 Java的布局:FlowLayout、BorderLayout、GridLayout、GridBa...

  • 用AWT编写用户界面二之布局管理器

    布局管理器 :为了实现跨平台的特效并且获得动态的布局结果,java 将容器内的所有组件安排给一个“布局管理器”负责...

  • Java学习笔记之Swing(二)

    Java学习笔记之Swing(二) 通过本文你将学习到: 了解各种布局管理器的样式 掌握每一种布局管理器的使用 布...

  • 二、Android布局管理器

    1、布局管理器的继承关系 Android布局管理器本身也是UI组件,所有的布局管理器都是ViewGroup的子类,...

  • Java图形化:布局方式

    布局方式 FlowLayout:流布局 BorderLayout:边框布局 GridLayout:网格布局 Flo...

  • RecyclerView 的三种LayoutManager

    RecyclerView 推出了三种布局管理器:1、LinearLayoutManager 线性布局管理器,呈现线...

  • PYQT5布局管理

    Qt布局管理按简单分可分为绝对位置布局和布局管理器布局 一、绝对位置布局: 组件不放在布局管理器中,通过函数set...

网友评论

    本文标题:Java图形化编程之布局管理器

    本文链接:https://www.haomeiwen.com/subject/sljujxtx.html