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);
}
}
在上述代碼中添加按鈕2時c.weighty=0.8,而在添加按鈕3時c.weighty=0.2,這就會導致按鈕2所占區域的高大約是按鈕3所占區域的高的0.8/0.2=4倍。
很高興為您回答!