在Java圖形化編程中,我們根據(jù)不同的業(yè)務(wù)場景選擇不同的布局容器來滿足我們的需求。那么Java為我們提供了多少布局容器呢衡蚂,下圖為布局管理器與常見布局容器之間的關(guān)系
LayoutManger
- 這個接口規(guī)范了布局容器的布局方式
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>添加到布局,
* 與之關(guān)聯(lián)的是一個特定的字符串<code>name</code>.
*
* @param name the string to be associated with the component
* 與組件關(guān)聯(lián)的字符串
* @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.
*
* 根據(jù)它包含的組件,計算指定容器的首選大小尺寸
*
* @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.
*
* 根據(jù)它包含的組件,計算指定容器的最小大小尺寸
* @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的一個擴展類窿克,提出了一種思想:基于布局約束對象去布局容器骏庸。根據(jù)約束對象我們可以知道組件添加到布局的方式和位置
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.
* 根據(jù)所包含的組件計算指定容器的最大尺寸大小
* @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軸對齊方式. 這指定了組件如何相對于其他組件進行對齊。
* 該值應(yīng)為0到1之間的數(shù)字
* 其中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軸對齊方式. 這指定了組件像相對于其他組件進行對齊让歼。
* 該值應(yīng)為0到1之間的數(shù)字
* 其中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.
* 使布局無效,需要注意的是如果布局管理器已緩存信息,則應(yīng)將其丟棄掉.
*/
public void invalidateLayout(Container target);
}
常見布局容器
布局名稱 | 說明 |
---|---|
GridLayout | 以M*N列的格子形式來放置組件敞恋,每個格子只能容納一個組件 |
FlowLayout | 組件以添加的順序根據(jù)相對應(yīng)的對齊方式從左到右進行排列,一行放置不下時換行放置谋右。 |
OverlayLayout | 支持組件之間根據(jù)設(shè)置的相應(yīng)的規(guī)則進行重疊放置 |
GroupLayout | 組件以層次分組硬猫,根據(jù)分組決定它們在容器中的位置 |
BoxLayout | 允許多個組件垂直或者水平布置,組件不會被包裹 |
JRootPane.RootLayout | 負責(zé)layeredPane, glassPane和menuBar的布局改执。 |
CardLayout | 每個組件被視為一張卡片啸蜜,一次只能看到一張卡片,卡的順序由容器自己的組件對象的內(nèi)部順序決定 |
GridBagLayout | 組件可以垂直辈挂,水平或沿其基線對齊衬横,不需要組件大小相同 |
SpringLayout | 根據(jù)一組約束排列其關(guān)聯(lián)容器的組件 |
BorderLayout(補,繼承LayoutManager2) | 用一個容器在五個區(qū)域:北,南终蒂,東蜂林,西和中心內(nèi)安排和放置組件。 每個區(qū)域最多放置一個組件 |