[轉]GridBagLayout和GridBagConstraints
自己想做個小程序,卻在布局上犯了難,使用FlowLayout和BorderLayout這些功能不夠強大,使用GridBagLayout卻不會,只好求助于文檔了。
文檔對這個布局管理器介紹很詳細,但是最痛苦的是英文。不過幸好它有實例,經過在網上查閱和推敲實例,終于對GridBagLayout的使用有了一個成型的了解,拿出來與大家分享。
GridBagLayout是一個靈活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干個參數,解釋如下:
gridx/gridy:組件的橫縱坐標
gridwidth:組件所占列數,也是組件的寬度
gridheight:組件所占行數,也是組件的高度
fill:當組件在其格內而不能撐滿其格時,通過fill的值來設定填充方式,有四個值
ipadx: 組件間的橫向間距
ipady:組件間的縱向間距
insets:當組件不能填滿其格時,通過insets來指定四周(即上下左右)所留空隙
anchor:同樣是當組件不能填滿其格時,通過anchor來設置組件的位置,anchor有兩種值,絕對和相對的值分別有 若干個,文檔中有,可自行查看
weightx:行的權重,通過這個屬性來決定如何分配行的剩余空間
weighty:列的權重,通過這個屬性來決定如何分配列的剩余空間
還是文檔實用,用例子來說話
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class GridBagEx1 extends Applet {
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c);
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
setSize(300, 100);
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1();
ex1.init();
f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.setVisible(true);
}
}
可以自行運行,查看其結果
文檔對其各個按鈕的參數設定解釋如下:
Button1, Button2, Button3: weightx = 1.0
Button4: weightx = 1.0, gridwidth = GridBagConstraints.REMAINDER
Button5: gridwidth = GridBagConstraints.REMAINDER
Button6: gridwidth = GridBagConstraints.RELATIVE
Button7: gridwidth = GridBagConstraints.REMAINDER
Button8: gridheight = 2, weighty = 1.0
Button9, Button 10: gridwidth = GridBagConstraints.REMAINDER
對照著程序和運行結果,還有其參數設定,我的理解如下:
第一行:第一行之所以有四個按鈕,關鍵點在于,weightx=1.0,這樣就可以在前邊的按鈕后繼續加入按鈕,而button4成為行尾是因為其gridwidth = GridBagConstraints.REMAINDER,這句話就設定它是行的末尾。
第二行:既然第一行都有末尾了,那么再加入按鈕的話,必定是另起一行了(這個道理)。此時加入了button5,而button5又被設定為了本行的最后一個(gridwidth = GridBagConstraints.REMAINDER),加之它又是第二行的第一個按鈕,所以第二行只有一個按鈕,就是button5。
第三行:button6不可避免的成為了第一個按鈕,它被設定了gridwidth = GridBagConstraints.RELATIVE,表明button6要緊挨它前邊的那個按鈕和最后的那個按鈕,也就是說它一定是倒數第二個按鈕(為最后一個按鈕的出現做好了準備)。button7出現了,由于有gridwidth = GridBagConstraints.REMAINDER,它就為第三行封了口。第三行結束。
第四行:這一行有一個特殊的按鈕button8,它的設定為gridheight = 2, weighty = 1.0,即它占用兩行一列(其實這個一列和兩行都是相對的)。這一行還沒封口,所以后面來的button9加在了這一行,因為它gridwidth = GridBagConstraints.REMAINDER,所以第四行封口。
第五行:這一行button8已經占據了第一個的位置(因為button8的gridheight=2),所以后來的button10加在第二,同樣由于gridwidth = GridBagConstraints.REMAINDER,第五行封口。
要理解GridBagLayout,最好從例子的理解開始,呵呵。
網上還有另外幾篇介紹它的文章,大家也可參考
http://blog.163.com/everlee@126/blog/static/263574220089621157826/
http://hi.baidu.com/zml1003/blog/item/43728f6ee02a7bd980cb4afc.html
文章出處:DIY部落(http://www.diybl.com/course/3_program/java/javajs/20090405/164188.html)
---------------------------------------------------------------------------------------
——使你疲勞的不是遠方的高山,而是你鞋里一粒沙子!
posted on 2009-05-21 15:34 鋒行 閱讀(694) 評論(0) 編輯 收藏 所屬分類: Swing