Java圖形化編程之布局管理器

在Java圖形化編程中,我們根據(jù)不同的業(yè)務(wù)場景選擇不同的布局容器來滿足我們的需求。那么Java為我們提供了多少布局容器呢衡蚂,下圖為布局管理器與常見布局容器之間的關(guān)系

常見布局及布局管理器關(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ū)域最多放置一個組件
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拇泣,一起剝皮案震驚了整個濱河市噪叙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌霉翔,老刑警劉巖睁蕾,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異债朵,居然都是意外死亡子眶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門序芦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來臭杰,“玉大人,你說我怎么就攤上這事谚中」杪” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵藏杖,是天一觀的道長将塑。 經(jīng)常有香客問我,道長蝌麸,這世上最難降的妖魔是什么点寥? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮来吩,結(jié)果婚禮上敢辩,老公的妹妹穿的比我還像新娘蔽莱。我一直安慰自己,他們只是感情好戚长,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布盗冷。 她就那樣靜靜地躺著,像睡著了一般同廉。 火紅的嫁衣襯著肌膚如雪仪糖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天迫肖,我揣著相機與錄音锅劝,去河邊找鬼。 笑死蟆湖,一個胖子當(dāng)著我的面吹牛故爵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播隅津,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼诬垂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伦仍?” 一聲冷哼從身側(cè)響起结窘,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎呢铆,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹲缠,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡棺克,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了线定。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娜谊。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖斤讥,靈堂內(nèi)的尸體忽然破棺而出纱皆,到底是詐尸還是另有隱情,我是刑警寧澤芭商,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布派草,位于F島的核電站,受9級特大地震影響铛楣,放射性物質(zhì)發(fā)生泄漏近迁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一簸州、第九天 我趴在偏房一處隱蔽的房頂上張望鉴竭。 院中可真熱鬧歧譬,春花似錦、人聲如沸搏存。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽璧眠。三九已至缩焦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蛆橡,已是汗流浹背舌界。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留泰演,地道東北人呻拌。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像睦焕,于是被迫代替她去往敵國和親藐握。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法垃喊,類相關(guān)的語法猾普,內(nèi)部類的語法,繼承相關(guān)的語法本谜,異常的語法初家,線程的語...
    子非魚_t_閱讀 31,632評論 18 399
  • 二十多年以前,我曾經(jīng)吃過一碗面乌助。味道怎么樣我著實不記得了溜在。 那個夏天的午后,我在棗樹涼兒里房檐下他托,吃到嘴里的確定是...
    君子包閱讀 216評論 0 2
  • 一無所有是一種心態(tài)掖肋,力量來自"最初的想法",讓你可以用最小的資源赏参,最小的力量志笼,干出最大的活。當(dāng)你一無所有時把篓,你才會...
    江南臺北人閱讀 643評論 0 0