Java Swing GridBagLayout網格袋佈局的實現
1. 佈局: GridBagLayout
官方JavaDocsApi: java.awt.GridBagLayout
GridBagLayout,網格袋佈局管理器。它不要求元件的大小相同便可以將元件垂直、水平或沿它們的基線對齊。每個 GridBagLayout 物件維持一個動態的矩形單元格(動態計算出單個單元格的大小),每個元件佔用一個或多個的單元格,該單元格被稱為 顯示區域。每個元件顯示區域按 從左到右,從上到下,依次排列。
2. 約束: GridBagConstraints
GridBagConstraints,封裝了若干對元件的約束屬性,每個由 GridBagLayout 管理的 元件都關聯一個該約束例項,以指定 元件所在顯示區域 的具體放置位置,以及 元件在其顯示區域中 的對齊方式。
給元件新增約束: GridBagLayout.setConstraints(Component comp,GridBagConstraints c)
PS: 要理解 元件(所在)顯示區域(一個或多個單元格組成) 和 元件在其顯示區域中的對齊方式 這兩個不同的概念。
3. 屬性: GridBagConstraints 的屬性
下面屬性描述中的相關大寫字母常量均定義在GridBagConstraints類中。
3.1 顯示區域 約束屬性
元件的顯示區域相關約束屬性,直接作用在元件所在顯示區域上。
(1)gridx,gridy
元件顯示區域 開始顯示的位置(單元格座標),容器左上角第一個單元格位置為 (0,0),預設值為RELATIVE,表示放置在 “上一個元件”(所在行列最後新增的一個元件)的 “後面”。
(2)gridwidth,gridheight
元件顯示區域 水平/豎直方向 所佔單元格的個數,預設值為 1,如要佔多行/列,需整體結果有足夠的行/列。有如下兩個常量特殊值可選:
- REMAINDER: 佔完所在行/列餘下所有單元格(該值可實現 換行 作用);
- RELATIVE: 佔到所在行/列餘下的倒數第二個單元格(使用該值,所在行/列的最後一個單元格需要“合理”安排元件,並手動換行)。
設定該值不能導致 “前面” 或 “後面” 單元格有留空白,否則可能無效。
(3)weightx,weighty
如何 分佈額外空間(單元格區域外,容器邊緣內 的間隔),當指定行/列中的其中 任意一個 元件的權重(大於0),則該行/列將(和其他行/列按權重比例)分配額外的水平/豎直空間。當權重為 0(預設值)時,則 整個單元格區域 居中於容器中心。
PS: 如果呼叫了java.awt.Window.pack()方法,該值無效,因為 pack 後已無額外的空間。
3.2 元件 約束屬性
元件相關約束屬性,直接作用在元件上。
(1)fill
當 顯示區域 大小大於 元件 所需要的大小時,元件 在其 顯示區域內 的填充方式。可能的值如下:
- NONE: (預設)不調整元件大小;
- HORIZONTAL: 加寬元件,使它在水平方向上填滿其顯示區域,但是不改變高度;
- VERTICAL: 加高元件,使它在垂直方向上填滿其顯示區域,但是不改變寬度;
- BOTH: 使元件完全填滿其顯示區域。
(2)anchor
元件 在 顯示區域內 的位置(對齊方式),可能有如下三種值:
絕對值 | 相對於方向的值 | 相對於基線的值 |
---|---|---|
NORTH | PAGE_START | BASELINE |
SOUTH | PAGE_END | BASELINE_LEADING |
WEST | LINE_START | BASELINE_TRAILING |
EAST | LINE_END | ABOVE_BASELINE |
NORTHWEST | FIRST_LINE_START | ABOVE_BASELINE_LEADING |
NORTHEAST | FIRST_LINE_END | ABOVE_BASELINE_TRAILING |
SOUTHWEST | LAST_LINE_START | BELOW_BASELINE |
SOUTHEAST | LAST_LINE_END | BELOW_BASELINE_LEADING |
CENTER(預設值) | BELOW_BASELINE_TRAILING |
(3)ipadx,ipady
元件的內部填充(可看做是 元件的內邊距),即對元件最小大小的新增量。元件的寬度至少為其最小寬度/高度加上 ipadx/ipady 畫素。
(4)insets
元件的外部填充(可看做是 元件的外邊距,也可看做是 顯示區域 的內邊距),即 元件 與其 顯示區域邊緣 之間間距的最小量。
PS: 上面各屬性值之間,以及其他因素,有可能存在衝突或不相容,不一定設定了即有效,建議多做實驗,多寫測試Demo去嘗試。
4. 案例: GridBagLayout使用例項
先展示效果,如下圖所示:
各按鈕約束引數:
- Button01,Button02,Button03:預設
- Button04:gridwidth = REMAINDER、fill = BOTH
- Button05:gridwidth = REMAINDER、fill = BOTH
- Button06:gridwidth = RELATIVE、fill = BOTH
- Button07:gridwidth = REMAINDER
- Button08:gridheight = 2、fill = BOTH
- Button09:gridwidth = REMAINDER、fill = BOTH
- Button10:gridwidth = REMAINDER、fill = BOTH
程式碼實現:
package com.xiets.swing; import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame jf = new JFrame("測試視窗"); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); GridBagLayout gridBag = new GridBagLayout(); // 佈局管理器 GridBagConstraints c = null; // 約束 JPanel panel = new JPanel(gridBag); JButton btn01 = new JButton("Button01"); JButton btn02 = new JButton("Button02"); JButton btn03 = new JButton("Button03"); JButton btn04 = new JButton("Button04"); JButton btn05 = new JButton("Button05"); JButton btn06 = new JButton("Button06"); JButton btn07 = new JButton("Button07"); JButton btn08 = new JButton("Button08"); JButton btn09 = new JButton("Button09"); JButton btn10 = new JButton("Button10"); /* 新增 元件 和 約束 到 佈局管理器 */ // Button01 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn01,c); // 內部使用的僅是 c 的副本 // Button02 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn02,c); // Button03 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn03,c); // Button04 顯示區域佔滿當前行剩餘空間(換行),元件填充顯示區域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn04,c); // Button05 顯示區域獨佔一行(換行),元件填充顯示區域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn05,c); // Button06 顯示區域佔到當前尾倒車第二個單元格(下一個元件後需要手動換行),元件填充顯示區域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.RELATIVE; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn06,c); // Button07 放置在當前行最後一個單元格(換行) c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; gridBag.addLayoutComponent(btn07,c); // Button08 顯示區域佔兩列,元件填充顯示區域 c = new GridBagConstraints(); c.gridheight = 2; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn08,c); // Button09 顯示區域佔滿當前行剩餘空間(換行),元件填充顯示區域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn09,c); // Button10 顯示區域佔滿當前行剩餘空間(換行),元件填充顯示區域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn10,c); /* 新增 元件 到 內容面板 */ panel.add(btn01); panel.add(btn02); panel.add(btn03); panel.add(btn04); panel.add(btn05); panel.add(btn06); panel.add(btn07); panel.add(btn08); panel.add(btn09); panel.add(btn10); jf.setContentPane(panel); jf.pack(); jf.setLocationRelativeTo(null); jf.setVisible(true); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。