JAVA布局模式:GridBagConstraints終極技巧參數詳解
布局模式 :GridBagConstraints布局,先發一個實例:
gridx = 2; // X2
gridy = 0; // Y0
gridwidth = 1; // 橫占一個單元格
gridheight = 1; // 列占一個單元格
weightx = 0.0; // 當窗口放大時,長度不變
weighty = 0.0; // 當窗口放大時,高度不變
anchor = GridBagConstraints.NORTH; // 當組件沒有空間大時,使組件處在北部
fill = GridBagConstraints.BOTH; // 當格子有剩余空間時,填充空間
insert = new Insets(0, 0, 0, 0); // 組件彼此的間距
ipadx = 0; // 組件內部填充空間,即給組件的最小寬度添加多大的空間
ipady = 0; // 組件內部填充空間,即給組件的最小高度添加多大的空間
new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insert, ipadx, ipady);
GridBagLayout之變態玩法:
很多人抱怨GridBagLayout沒有XYLayout布局靈活,但是做為一個專業程序,所有組件必須隨着窗口大小改變而改變。其次,當僅僅簡單使用XYLayout時,需要包含一個大庫,對於一些場合這些多余的類超出了可接受范圍(例如Applet應用)
拿一個比較簡單的界面來作介紹
1。首先建立一個JFrame,設定它的Layout為XYLayout
2。在其上堆上控件,對齊好位置,否則轉換時會有較大調整
3。完成創建控件后,設置Layout為GridBagLayout,這時所有控件基本保持原位,待下一步做精細調整
4。選擇一個控件,點擊右邊屬性欄的"constraints" 對應的調整按鈕,彈出最重要的屬性調整界面
5。將所有邊界和空白去掉,同時Grid大小也暫時去掉,因為這些邊距會影響我們的調整
6。設置需要擴展的行和列
7。粗輪廓完成后,可以設置組件邊距進行精細調整了
8。精細調整完成,運行調試

以下是GridBagLayout的詳解:
雖說GridBagLayout和GridLayout只有一點差別,它
的作用卻是出奇的大。這是因為GridBagLayout一改其他的外觀管理器的死板
模樣,具有很多的靈活性。它不再像其他的外觀管理器那樣,使得各個組件
的大小都一樣。 GridBagLayout通過類GridBagConstraints的幫助,按照
設計的意圖,改變組件的大小,把它們擺在設計者希望擺放的位置上。
在GridBagLayout中,每個組件都有一個GridBagConstraints
對象來給出它的大小和擺放位置。我們在使用GridBagLayout的時候,最重
要的就是學會使用這個類GridBagConstraints的使用方法,學會如何設置組
件的大小、位置等限制條件。
我們先看一個用GridBagLayout外觀管理器生成的窗口
圖14.8程序14.5的執行結果
這個窗口里面的幾個按鈕有的大、有的小,其大小、位
置均不同,沒有一定的規律可循,這即是發揮了GridBagLayout外觀管理器
的靈活性。生成此窗口的程序為:
程序14.5
importjava.awt.*;
//輸入所有的java.awt 類
publicclasswindow7extendsjava.applet.Applet
{
publicvoidinit() {
resize(300,100);//設置窗口的大小
GridBagConstraintsgbc=new GridBagConstraints(
);//使用類GridBagConstriants
setLayout(newGridBagLayout());//設定外觀
管理器為 GridBagLayout外觀管理器
gbc.fill =GridBagConstraints.BOTH;//★
所有的按鈕都會把分配的剩余空間填滿
gbc.gridwidth=1;//★設置第一個按鈕的大
小
gbc.gridheight=1;// ★
ButtonButton1=newButton("東 ");
((GridBagLayout)getLayout( )).setConstraints(Button1,gbc);
add(Button1);
gbc.gridwidth= GridBagConstraints.REMAINDER;
//★第二個按鈕填滿整行空間
ButtonButton2=newButton("西 ");
((GridBagLayout)getLayout( )).setConstraints(Button2,gbc);
add(Button2);
gbc.gridheight=4;//設置第三個按鈕的大
小
gbc.gridwidth= 1;
ButtonButton3=newButton("南 ");
((GridBagLayout)getLayout( )).setConstraints(Button3,gbc);
add(Button3);
gbc.gridheight=2;//設置第四個按鈕的大
小
gbc.gridwidth= 2;//GridBagConstraints.REMAINDER;
ButtonButton4=newButton("北");
((GridBagLayout)getLayout( )).setConstraints(Button4,gbc);
add(Button4);
gbc.gridwidth= GridBagConstraints.REMAINDER;
Button Button5=newButton("中");
((GridBagLayout)getLayout( )).setConstraints(Button5,gbc);
add(Button5);
gbc.insets=new Insets(5,6,7,8);//★設置第五個按鈕的位置
ButtonButton6=newButton("好酒在張弓");
((GridBagLayout)getLayout( )).setConstraints(Button6,gbc);
add(Button6);
}
}
★注釋:程序14.5里面有星號的語句都將做詳細的解釋
下面就詳細地解釋一下程序14.5,通過對這個小程序的分析可以從中了解GridBagLayout外觀管理器的工作原理和工作方法。
GridBagLayout外觀管理器實際上是根據類GridBagConstraints所給
出的條件限制以及組件本身的一向特性條件(例如每個組件程序允許的最小
尺寸),來決定各個組件的外觀的。
讓我們把程序14.5之中出現的新鮮的語句一條一條地看個明白吧:
1.gbc.fill=GridBagConstraints.BOTH;
每個組件有一定的原始大小,例如在類FlowLayout外
觀管理器的管理之下顯示的就都是組件的本身原始大小。如果我們分配給一
個組件的空間比它原本所需要的空間大時,就需要一定的方式方法來決定如
何處理這一部分多余的空間。這時就用到了fill值。Java根據人們給這個
fill設定的值來決定如何處理比組件原始空間大的那部分空間。
fill可以取四種不同的值,它們分別代表了四種不同
的剩余空間處理方式:
GridBagConstraints.NONE
不必理睬剩余空間的存在,讓它空着好了。
GridBagConstraints.BOTH
不讓一點剩余空間存在,改變組件的大小,讓它填
滿分配給它的整個空間。
GridBagConstraints.HORIZONTAL
調整組件的大小,把水平方向的空間填滿。
GridBagConstraints.VERTICAL
調整組件的大小,把垂直方向的空間填滿,讓水平
方向的空間空着吧。
2.gbc.gridwidth=1;和 gbc.gridheight=1;
這兩句話像是一對孿生兄弟,應該同時給以同樣的重視
。它們一個負責組件的水平寬度(gridwidth),一個負責組件的垂直高度
(gridheight )。由此我們可以知道,組件的大小是可以變化的。
組件的形狀是不能改變的,永遠是矩形的。
好了,這兩條語句的意義很簡單,就講到這里吧。
喂!等一等,我看到下面有一條語句是:
gbc.gridwidth= GridBagConstraints.REMAINDER;
這是什么意思?
怎么gridwidth的值不是一個數,而是“ GridBagConstraints.REMAINDER
”?
原來,這是Java精心為大家設計的一個特別有用的變
量,使用它就可以通知外觀管理器讓組件占據本行的所有剩余空間,而不必
去計算寬度值是多少,很自動化。
3.gbc.insets=newInsets(5,6,7,8);
這條語句里面提到了兩個拼寫幾乎完全相同的詞:insets
和 Insets,雖然只相差一個字母:一個是大寫I,一個是小寫i,但是它
們代表的意義可大不相同。
Insets是AWT里面一個類的名字,代表着類Insets,它的用途是用來定義組件容器周圍的空間大小,其中帶有四個參數:
Insets(第一個參數,第二個參數,第三個參數,第
四個參數 )
第一個參數代表距上面有幾個點的空白,第二個參數代
表距左邊有幾個點的空白,第三個參數代表距下邊有幾個點的空白區域,第
四個參數代表距右邊留幾個點的空白區域。
形象一點的表示如圖 14.9:
圖14.9參數的設定順序
insets是類GridBagConstraints的一個限定條件。
insets和Insets既然起的名字相同,兩者之間也一
定有相同之處,它們的相似之處就在於它們的用法和用途。insets用來設置
一個組件和其他的組件之間的距離的。所以在上面程序里的按鈕和其他的按
鈕不同,它和其他的按鈕之間都有一定的距離,而不是和其他的按鈕挨在一
起。
總之,使用外觀管理器給我們帶來了許多的方便之處,
使得我們可以輕輕松松地完成各種窗口的外觀處理工作。
使用外觀管理器除了使得程序設計變得方便以外,還
使得程序變得容易在各種不同的窗口環境下運行,從而協助實現了Java的多
平台之夢。
小結:
學會使用各種各樣的外觀管理器會帶來事半功倍的編
程效果。
這一章里新學習的類有:
BorderLayout,CardLayout,FlowLayout,GridLayot
,ridBagLayout,GridBagConstraints和Insets。
類GridBagLayout生成的外觀管理器是最具有靈活性
的外觀管理器。
類GridBagLayout需要通過類GridBagconstraints
來實現對程序窗口外觀的管理。
gridx = 2; // X2
gridy = 0; // Y0
gridwidth = 1; // 橫占一個單元格
gridheight = 1; // 列占一個單元格
weightx = 0.0; // 當窗口放大時,長度不變
weighty = 0.0; // 當窗口放大時,高度不變
anchor = GridBagConstraints.NORTH; // 當組件沒有空間大時,使組件處在北部
fill = GridBagConstraints.BOTH; // 當格子有剩余空間時,填充空間
insert = new Insets(0, 0, 0, 0); // 組件彼此的間距
ipadx = 0; // 組件內部填充空間,即給組件的最小寬度添加多大的空間
ipady = 0; // 組件內部填充空間,即給組件的最小高度添加多大的空間
new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insert, ipadx, ipady);
GridBagLayout之變態玩法:
很多人抱怨GridBagLayout沒有XYLayout布局靈活,但是做為一個專業程序,所有組件必須隨着窗口大小改變而改變。其次,當僅僅簡單使用XYLayout時,需要包含一個大庫,對於一些場合這些多余的類超出了可接受范圍(例如Applet應用)
拿一個比較簡單的界面來作介紹
1。首先建立一個JFrame,設定它的Layout為XYLayout
2。在其上堆上控件,對齊好位置,否則轉換時會有較大調整
3。完成創建控件后,設置Layout為GridBagLayout,這時所有控件基本保持原位,待下一步做精細調整
4。選擇一個控件,點擊右邊屬性欄的"constraints" 對應的調整按鈕,彈出最重要的屬性調整界面
5。將所有邊界和空白去掉,同時Grid大小也暫時去掉,因為這些邊距會影響我們的調整
6。設置需要擴展的行和列
7。粗輪廓完成后,可以設置組件邊距進行精細調整了
8。精細調整完成,運行調試

以下是GridBagLayout的詳解:
雖說GridBagLayout和GridLayout只有一點差別,它
的作用卻是出奇的大。這是因為GridBagLayout一改其他的外觀管理器的死板
模樣,具有很多的靈活性。它不再像其他的外觀管理器那樣,使得各個組件
的大小都一樣。 GridBagLayout通過類GridBagConstraints的幫助,按照
設計的意圖,改變組件的大小,把它們擺在設計者希望擺放的位置上。
在GridBagLayout中,每個組件都有一個GridBagConstraints
對象來給出它的大小和擺放位置。我們在使用GridBagLayout的時候,最重
要的就是學會使用這個類GridBagConstraints的使用方法,學會如何設置組
件的大小、位置等限制條件。
我們先看一個用GridBagLayout外觀管理器生成的窗口

圖14.8程序14.5的執行結果
這個窗口里面的幾個按鈕有的大、有的小,其大小、位
置均不同,沒有一定的規律可循,這即是發揮了GridBagLayout外觀管理器
的靈活性。生成此窗口的程序為:
程序14.5
importjava.awt.*;
//輸入所有的java.awt 類
publicclasswindow7extendsjava.applet.Applet
{
publicvoidinit() {
resize(300,100);//設置窗口的大小
GridBagConstraintsgbc=new GridBagConstraints(
);//使用類GridBagConstriants
setLayout(newGridBagLayout());//設定外觀
管理器為 GridBagLayout外觀管理器
gbc.fill =GridBagConstraints.BOTH;//★
所有的按鈕都會把分配的剩余空間填滿
gbc.gridwidth=1;//★設置第一個按鈕的大
小
gbc.gridheight=1;// ★
ButtonButton1=newButton("東 ");
((GridBagLayout)getLayout( )).setConstraints(Button1,gbc);
add(Button1);
gbc.gridwidth= GridBagConstraints.REMAINDER;
//★第二個按鈕填滿整行空間
ButtonButton2=newButton("西 ");
((GridBagLayout)getLayout( )).setConstraints(Button2,gbc);
add(Button2);
gbc.gridheight=4;//設置第三個按鈕的大
小
gbc.gridwidth= 1;
ButtonButton3=newButton("南 ");
((GridBagLayout)getLayout( )).setConstraints(Button3,gbc);
add(Button3);
gbc.gridheight=2;//設置第四個按鈕的大
小
gbc.gridwidth= 2;//GridBagConstraints.REMAINDER;
ButtonButton4=newButton("北");
((GridBagLayout)getLayout( )).setConstraints(Button4,gbc);
add(Button4);
gbc.gridwidth= GridBagConstraints.REMAINDER;
Button Button5=newButton("中");
((GridBagLayout)getLayout( )).setConstraints(Button5,gbc);
add(Button5);
gbc.insets=new Insets(5,6,7,8);//★設置第五個按鈕的位置
ButtonButton6=newButton("好酒在張弓");
((GridBagLayout)getLayout( )).setConstraints(Button6,gbc);
add(Button6);
}
}
★注釋:程序14.5里面有星號的語句都將做詳細的解釋
下面就詳細地解釋一下程序14.5,通過對這個小程序的分析可以從中了解GridBagLayout外觀管理器的工作原理和工作方法。
GridBagLayout外觀管理器實際上是根據類GridBagConstraints所給
出的條件限制以及組件本身的一向特性條件(例如每個組件程序允許的最小
尺寸),來決定各個組件的外觀的。
讓我們把程序14.5之中出現的新鮮的語句一條一條地看個明白吧:
1.gbc.fill=GridBagConstraints.BOTH;
每個組件有一定的原始大小,例如在類FlowLayout外
觀管理器的管理之下顯示的就都是組件的本身原始大小。如果我們分配給一
個組件的空間比它原本所需要的空間大時,就需要一定的方式方法來決定如
何處理這一部分多余的空間。這時就用到了fill值。Java根據人們給這個
fill設定的值來決定如何處理比組件原始空間大的那部分空間。
fill可以取四種不同的值,它們分別代表了四種不同
的剩余空間處理方式:
GridBagConstraints.NONE
不必理睬剩余空間的存在,讓它空着好了。
GridBagConstraints.BOTH
不讓一點剩余空間存在,改變組件的大小,讓它填
滿分配給它的整個空間。
GridBagConstraints.HORIZONTAL
調整組件的大小,把水平方向的空間填滿。
GridBagConstraints.VERTICAL
調整組件的大小,把垂直方向的空間填滿,讓水平
方向的空間空着吧。
2.gbc.gridwidth=1;和 gbc.gridheight=1;
這兩句話像是一對孿生兄弟,應該同時給以同樣的重視
。它們一個負責組件的水平寬度(gridwidth),一個負責組件的垂直高度
(gridheight )。由此我們可以知道,組件的大小是可以變化的。
組件的形狀是不能改變的,永遠是矩形的。
好了,這兩條語句的意義很簡單,就講到這里吧。
喂!等一等,我看到下面有一條語句是:
gbc.gridwidth= GridBagConstraints.REMAINDER;
這是什么意思?
怎么gridwidth的值不是一個數,而是“ GridBagConstraints.REMAINDER
”?
原來,這是Java精心為大家設計的一個特別有用的變
量,使用它就可以通知外觀管理器讓組件占據本行的所有剩余空間,而不必
去計算寬度值是多少,很自動化。
3.gbc.insets=newInsets(5,6,7,8);
這條語句里面提到了兩個拼寫幾乎完全相同的詞:insets
和 Insets,雖然只相差一個字母:一個是大寫I,一個是小寫i,但是它
們代表的意義可大不相同。
Insets是AWT里面一個類的名字,代表着類Insets,它的用途是用來定義組件容器周圍的空間大小,其中帶有四個參數:
Insets(第一個參數,第二個參數,第三個參數,第
四個參數 )
第一個參數代表距上面有幾個點的空白,第二個參數代
表距左邊有幾個點的空白,第三個參數代表距下邊有幾個點的空白區域,第
四個參數代表距右邊留幾個點的空白區域。
形象一點的表示如圖 14.9:

圖14.9參數的設定順序
insets是類GridBagConstraints的一個限定條件。
insets和Insets既然起的名字相同,兩者之間也一
定有相同之處,它們的相似之處就在於它們的用法和用途。insets用來設置
一個組件和其他的組件之間的距離的。所以在上面程序里的按鈕和其他的按
鈕不同,它和其他的按鈕之間都有一定的距離,而不是和其他的按鈕挨在一
起。
總之,使用外觀管理器給我們帶來了許多的方便之處,
使得我們可以輕輕松松地完成各種窗口的外觀處理工作。
使用外觀管理器除了使得程序設計變得方便以外,還
使得程序變得容易在各種不同的窗口環境下運行,從而協助實現了Java的多
平台之夢。
小結:
學會使用各種各樣的外觀管理器會帶來事半功倍的編
程效果。
這一章里新學習的類有:
BorderLayout,CardLayout,FlowLayout,GridLayot
,ridBagLayout,GridBagConstraints和Insets。
類GridBagLayout生成的外觀管理器是最具有靈活性
的外觀管理器。
類GridBagLayout需要通過類GridBagconstraints
來實現對程序窗口外觀的管理。
=====================================================================================
- package com.wanju.chat.test;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- public class ExampleFrame_03 extends JFrame {
- public static void main(String args[]) {
- ExampleFrame_03 frame = new ExampleFrame_03();
- frame.setVisible(true);
- }
- public ExampleFrame_03() {
- super();
- setTitle("使用網格組布局管理器");
- getContentPane().setLayout(new GridBagLayout());
- setBounds(100, 100, 500, 170);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- final JButton button = new JButton("A");
- final GridBagConstraints gridBagConstraints = new GridBagConstraints();
- gridBagConstraints.gridy = 0;// 起始點為第1行
- gridBagConstraints.gridx = 0;// 起始點為第1列
- gridBagConstraints.weightx = 10;// 第一列的分布方式為10%
- gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
- getContentPane().add(button, gridBagConstraints);
- final JButton button_1 = new JButton("B");
- final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
- gridBagConstraints_1.gridy = 0;
- gridBagConstraints_1.gridx = 1;
- gridBagConstraints_1.insets = new Insets(0, 5, 0, 0);// 設置組件左側的最小距離
- gridBagConstraints_1.weightx = 20;// 第一列的分布方式為20%
- gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;
- getContentPane().add(button_1, gridBagConstraints_1);
- final JButton button_2 = new JButton("C");
- final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
- gridBagConstraints_2.gridy = 0;// 起始點為第1行
- gridBagConstraints_2.gridx = 2;// 起始點為第3列
- gridBagConstraints_2.gridheight = 2;// 組件占用兩行
- gridBagConstraints_2.insets = new Insets(0, 5, 0, 0);
- gridBagConstraints_2.weightx = 30;// 第一列的分布方式為30%
- gridBagConstraints_2.fill = GridBagConstraints.BOTH;// 同時調整組件的寬度和高度
- getContentPane().add(button_2, gridBagConstraints_2);
- final JButton button_3 = new JButton("D");
- final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
- gridBagConstraints_3.gridy = 0;
- gridBagConstraints_3.gridx = 3;
- gridBagConstraints_3.gridheight = 4;
- gridBagConstraints_3.insets = new Insets(0, 5, 0, 5);// 設置組件左側和右側的最小距離
- gridBagConstraints_3.weightx = 40;// 第一列的分布方式為40%
- gridBagConstraints_3.fill = GridBagConstraints.BOTH;
- getContentPane().add(button_3, gridBagConstraints_3);
- final JButton button_4 = new JButton("E");
- final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
- gridBagConstraints_4.gridy = 1;
- gridBagConstraints_4.gridx = 0;
- gridBagConstraints_4.gridwidth = 2;// 組件占用兩列
- gridBagConstraints_4.insets = new Insets(5, 0, 0, 0);// 設置組件上方的最小距離
- gridBagConstraints_4.fill = GridBagConstraints.HORIZONTAL;// 只調整組件的寬度
- getContentPane().add(button_4, gridBagConstraints_4);
- final JButton button_5 = new JButton("F");
- final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
- gridBagConstraints_5.gridy = 2;// 起始點為第3行
- gridBagConstraints_5.gridx = 0;// 起始點為第1列
- gridBagConstraints_5.insets = new Insets(5, 0, 0, 0);
- gridBagConstraints_5.fill = GridBagConstraints.HORIZONTAL;
- getContentPane().add(button_5, gridBagConstraints_5);
- final JButton button_6 = new JButton("G");
- final GridBagConstraints gridBagConstraints_6 = new GridBagConstraints();
- gridBagConstraints_6.gridy = 2;
- gridBagConstraints_6.gridx = 1;
- gridBagConstraints_6.gridwidth = 2;// 組件占用兩列
- gridBagConstraints_6.gridheight = 2;// 組件占用兩行
- gridBagConstraints_6.insets = new Insets(5, 5, 0, 0);
- gridBagConstraints_6.fill = GridBagConstraints.BOTH;// 只調整組件的高度
- // gridBagConstraints_6.fill = GridBagConstraints.VERTICAL;// 只調整組件的高度
- // gridBagConstraints_6.ipadx = 30;// 增加組件的首選寬度
- // gridBagConstraints_6.anchor = GridBagConstraints.EAST;// 顯示在東方
- getContentPane().add(button_6, gridBagConstraints_6);
- final JButton button_7 = new JButton("H");
- final GridBagConstraints gridBagConstraints_7 = new GridBagConstraints();
- gridBagConstraints_7.gridy = 3;
- gridBagConstraints_7.gridx = 0;
- gridBagConstraints_7.insets = new Insets(5, 0, 0, 0);
- gridBagConstraints_7.fill = GridBagConstraints.HORIZONTAL;
- getContentPane().add(button_7, gridBagConstraints_7);
- //
- }
- }
