GridBagLayout 以及 GridBagConstraints 用

GridBagLayout是一個靈活的佈局管理器,部件如果想加入其中需藉助GridBagConstraints,其中有若干個參數,解釋如下:

gridx/gridy:組件的橫縱座標

gridwidth:組件所佔列數,也是組件的寬度

gridheight:組件所佔行數,也是組件的高度

fill:當組件在其格內而不能撐滿其格時,通過 fill的值來設定填充方式,有四個值

ipadx: 組件間的橫向間距

ipady:組件間的縱向間距

insets:當組件不能填滿其格時,通過 insets來指定四周(即上下左右)所留空隙

anchor:同樣是當組件不能填滿其格時,通過 anchor來設置組件的位置,anchor有兩種值,絕對和相對的值分別有 若干個,文檔中有,可自行查看

weightx:行的權重,通過這個屬性來決定如何分配行的剩餘空間

weighty:列的權重,通過這個屬性來決定如何分配列的剩餘空間

 

 

1.要明確一點概念:每個 GridBagLayout 對象維持一個動態的矩形單元網格,每個組件佔用一個或多個這樣的單元,稱爲顯示區域。
網格的總體方向取決於容器的 ComponentOrientation 屬性。對於水平的從左到右的方向,網格座標 (0,0) 位於容器的左上角,其中 X 向右遞增,Y 向下遞增。

2.要使用GidBagLayout要先定義一個GridBagConstraints對象。
Java API說明如下:“每個由 GridBagLayout 管理的組件都與 GridBagConstraints 的實例相關聯。Constraints 對象指定組件在網格中的顯示區域以及組件在其顯示區域中的放置方式。”
例如,如下幾行代碼就可以添加其它組件:
         GridBagLayout gridbag = new GridBagLayout();
         GridBagConstraints c = new GridBagConstraints();
         JFrame   f=new JFrame();
         f.setLayout(gridbag);
         Button button = new Button(name);
         gridbag.setConstraints(button, c);
         f.add(jButton);

3.爲了有效使用網格包佈局,必須自定義與組件相關聯的一個或多個 GridBagConstraints 對象。
即須設置GridBagConstraints 對象的屬性。我認爲只要能掌握以下四種參數就能很好的使用GidBagLayout:
(1)GridBagConstraints.gridwidthGridBagConstraints.gridheight
    指定組件的顯示區域行(針對 gridwidth)或列(針對 gridheight)中的單元數。默認值爲 1。如下向窗口中添加一個佔兩個單元格(兩行一列)的按鈕的例子:
         JFrame   f=new JFrame();
         GridBagLayout gridbag = new GridBagLayout();
         GridBagConstraints c = new GridBagConstraints();
         f.setLayout(gridbag);
         c.gridheight=2;
         c.gridwidth=1;
         JButton jButton = new JButton("按鈕1");
         gridbag.setConstraints(button, c);
         f.add(jButton);
(2)GridBagConstraints.fill 
    當組件的顯示區域大於組件的所需大小時,用於確定是否(以及如何)調整組件。
    可能的值爲 GridBagConstraints.NONE(默認值)、
              GridBagConstraints.HORIZONTAL(加寬組件直到它足以在水平方向上填滿其顯示區域,但不更改其高度)、               

              GridBagConstraints.VERTICAL(加高組件直到它足以在垂直方向上填滿其顯示區域,但不更改其寬度)和                  

            GridBagConstraints.BOTH(使組件完全填滿其顯示區域)。 
    使用情景舉例:在一個很大的窗口(如300*300)中添加一個按鈕(原始大小40*30)。

(3)GridBagConstraints.anchor 
    當組件小於其顯示區域時,用於確定將組件置於何處(在顯示區域中)。可能的值有兩種:相對和絕對。相對值的解釋是相對於容器的ComponentOrientation 屬性,而絕對值則不然。個人覺得只使用絕對值就可以。有效值有:
    絕對值
    GridBagConstraints.NORTH 
    GridBagConstraints.SOUTH 
    GridBagConstraints.WEST 
    GridBagConstraints.EAST 
    GridBagConstraints.NORTHWEST 
    GridBagConstraints.NORTHEAST 
    GridBagConstraints.SOUTHWEST 
    GridBagConstraints.SOUTHEAST 
    GridBagConstraints.CENTER (the default) 

(4)GridBagConstraints.weightx、GridBagConstraints.weighty   (************最重要的屬性) 
用於確定分佈空間的方式,這對於指定調整行爲至關重要。例如:在一個很大的窗口(如300*300)中添加兩個按鈕(也可以是面板)(原始大小 40*30),默認的,你會發現兩個按鈕分別處於上下兩個等大小的區域中,且只佔用了一小部分,沒有被按鈕佔用的區域就被稱爲額外區域。該額外區域會隨着參數weightx、weighty而被分配。


   完整的示例代碼如下:

import javax.swing.*;
import Java.util.*;
import java.awt.*;

public class Example{

     public Example() {
     }

     public static void main(String args[]) {
        JFrame f = new JFrame("GridBag Layout Example");

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        f.setLayout(gridbag);
//添加按鈕1
        c.fill = GridBagConstraints.BOTH;
        c.gridheight=2;
        c.gridwidth=1;
        c.weightx=0.0;//默認值爲0.0
        c.weighty=0.0;//默認值爲0.0
        c.anchor=GridBagConstraints.SOUTHWEST;
        JButton jButton1 = new JButton("按鈕1");
        gridbag.setConstraints(jButton1, c);
        f.add(jButton1);
//添加按鈕2        
        c.fill = GridBagConstraints.NONE;
        c.gridwidth=GridBagConstraints.REMAINDER;
        c.gridheight=1;
        c.weightx=1.0;//默認值爲0.0
        c.weighty=0.8;
        JButton jButton2 = new JButton("按鈕2");
        gridbag.setConstraints(jButton2, c);
        f.add(jButton2);
//添加按鈕3
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth=1;
        c.gridheight=1;
        c.weighty=0.2;
        JButton jButton3 = new JButton("按鈕3");
        gridbag.setConstraints(jButton3, c);
        f.add(jButton3);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500,500);
        f.setVisible(true);
     }
}

 

gridx,gridy —— 設置組件的位置,
gridx設置爲GridBagConstraints.RELATIVE代表此組件位於之前所加入組件的右邊。
gridy設置爲GridBagConstraints.RELATIVE代表此組件位於以前所加入組件的下面。
建議定義出gridx,gridy的位置以便以後維護程序。gridx=0,gridy=0時放在0行0列。

gridwidth,gridheight —— 用來設置組件所佔的單位長度與高度,默認值皆爲1。
你可以使用GridBagConstraints.REMAINDER常量,代表此組件爲此行或此列的最後一個組件,而且會佔據所有剩餘的空間。

weightx,weighty —— 用來設置窗口變大時,各組件跟着變大的比例。
當數字越大,表示組件能得到更多的空間,默認值皆爲0。

anchor —— 當組件空間大於組件本身時,要將組件置於何處。
有CENTER(默認值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST選擇。

insets —— 設置組件之間彼此的間距。
它有四個參數,分別是上,左,下,右,默認爲(0,0,0,0)。

ipadx,ipady —— 設置組件間距,默認值爲0。

GridBagLayout裏的各種設置都必須通過GridBagConstraints,因此當我們將GridBagConstraints的參數都設置
好了之後,必須new一個GridBagConstraints的對象出來,以便GridBagLayout使用。


構造函數:
GirdBagLayout()建立一個新的GridBagLayout管理器。
GridBagConstraints()建立一個新的GridBagConstraints對象。
GridBagConstraints(int gridx,int gridy,
int gridwidth,int gridheight,
double weightx,double weighty,
int anchor,int fill, Insets insets,
int ipadx,int ipady)建立一個新的GridBagConstraints對象,並指定其參數的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章