注:本文內容轉自:Java Layout總結-GridBagLayout。內容根據筆者理解稍有整理。
GridBagLayout布局管理器:
這就是最復雜的一個布局管理器了,網格包布局.在此布局中,組件大小不必相同.
GridBagLayout gb=new GridBagLayout();
ContainerName.setLayout(gb);
以上代碼是讓容器獲得一個GridBagLayout.要使用網格包布局,還必須有其一個輔助類,GridBagContraints.它包含GridBagLayout類用來定位及調整組件大小所需要的全部信息.使用步驟如下:
1).創建網格包布局的一個實例,並將其定義為當前容器的布局管理器.
2).創建GridBagContraints的一個實例
3).為組件設置約束.
4).通過方法統治布局管理器有關組件及其約束等信息
5).將組件添加到容器.
6).對各個將被顯示的組件重復以上步驟.
GridBagContraints類的成員變量列表如下:
1、gridx—組件的橫向坐標;
2、girdy—組件的縱向坐標;
gridx=0,gridy=0時放在0行0列,GridBagConstraints.RELATIVE為默認值,表明當前組件緊跟在上一個組件之后。如果在行上不指定該值(同時也不指定gridx和gridy參數),那么無論添加多少個組件都是在同一行上
3、gridwidth——組件的橫向寬度,也就是指組件占用的列數,這與HTML的colspan類似;
4、gridheight—組件的縱向長度,也就是指組件占用的行數,這與HTML的rowspan類似;
設置組件橫向縱向跨越多少個網格,他們的默認值都是1,如果該組件是橫向或縱向的最后一個還可以將此值設為GridBagConstraints.REMAINDER,若為倒數第二個組件則可以設值為GridBagConstraints.RELATIVE。
gridwidth能否能夠保留住該寬度取決於其正上/下方相應位置是否有足夠的組件來占位,如果無,位置將會被壓縮。比如,設置gridwidth=3,但只添加了一個JButton,這時如果其上下方相應位置沒有其他組件或只有1個組件,那么它只占有1個網格大小。如果它上/下方相應位置有3個組件,那它就可以占3個網格大小了。如果它上/下方相應位置只有2個組件,那它就占2個網格大小。
gridheight能否保留住該高度取決於其左右兩邊是否有足夠的組件來占位。也就是說它的最大高度不大於左右兩邊最大的高度。比如,設置gridheight=3,如果左右兩邊組件只有1行,則它僅僅只有1行的高度,有2行則占2行的高度。
5、weightx—指行的權重,告訴布局管理器如何分配額外的水平空間;
6、weighty—指列的權重,告訴布局管理器如何分配額外的垂直空間;
用來設置窗口變大時,各組件跟着變大的比例。當數字越大,表示組件能得到更多的空間,默認值皆為0。比如組件A的weightx=0.5,組件B的weightx=1,那么窗口X軸變大時剩余的空間就會以1:2的比例分配給組件A和B;
7、anchor—告訴布局管理器組件在表格空間中的位置,當組件小於其顯示區域時使用此字段;
有CENTER(默認值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST選擇。
8、fill—如果顯示區域比組件的區域大的時候,可以用來控制組件的行為。控制組件是垂直填充,還是水平填充,或者兩個方向一起填充;
9、insets—指組件與表格空間四周邊緣的空白區域的大小,內邊距,算入組件自身大小中。它有四個參數,分別是上,左,下,右,默認為(0,0,0,0)。
10、ipadx— 組件間的橫向間距,組件的寬度就是這個組件的最小寬度加上ipadx值;
11、ipady— 組件間的縱向間距,組件的高度就是這個組件的最小高度加上ipady值。
外邊距,最終組件占用的大小是組件自身大小加上外邊距。
下面是測試用例:
GridBagLayoutTest.java:
import java.awt.Font; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; /* * GridBagLayoutTest.java,source code from java核心技術 卷1 基礎知識,P379 */ public class GridBagLayoutTest { public static void main(String[] args) { // TODO Auto-generated method stub FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class FontFrame extends JFrame { int DEUAULT_WIDTH = 300; int DEUAULT_HEIGHT = 200; private JComboBox face; private JComboBox size; private JCheckBox bold; private JCheckBox italic; private JTextArea sample; public FontFrame() { setTitle("GridBagLayoutTest"); setSize(DEUAULT_WIDTH, DEUAULT_HEIGHT); GridBagLayout layout = new GridBagLayout(); setLayout(layout); ActionListener listener = new FontAction(); JLabel faceLabel = new JLabel("face:"); face = new JComboBox(new String[] { "serif", "sansSerif", "Monospaced", "Dialog", "DialogInput", }); face.addActionListener(listener); JLabel sizeLabel = new JLabel("Size:"); size = new JComboBox(new String[] { "8", "10", "12", "15", "18", "24", "36", "48" }); size.addActionListener(listener); bold = new JCheckBox("Bold"); bold.addActionListener(listener); italic = new JCheckBox("Italic"); italic.addActionListener(listener); sample = new JTextArea(); sample.setText("The quick brown fox jump over the lazy dog."); sample.setEditable(false); sample.setLineWrap(true); sample.setBorder(BorderFactory.createEtchedBorder()); add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST)); add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0) .setInsets(1)); add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST)); add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0) .setInsets(1)); add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100)); add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100)); add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100)); } private class FontAction implements ActionListener { public void actionPerformed(ActionEvent event) { String fontFace = face.getSelectedItem().toString(); int fontStyle = (bold.isSelected() ? Font.BOLD : 0) + (italic.isSelected() ? Font.ITALIC : 0); int fontSize = Integer.parseInt(size.getSelectedItem().toString()); Font font = new Font(fontFace, fontStyle, fontSize); sample.setFont(font); sample.repaint(); } } }
GBC.java:
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; /* * GBC.java,source code from java核心技術 卷1 基礎知識,P381 */ public class GBC extends GridBagConstraints{ /* * constructs a GBC with a given gridx and gridy position and all other grid * bag constraint values set to the default * @param gridx the gridx position * @param gridy the gridy position */ public GBC(int gridx, int gridy){ this.gridx = gridx; this.gridy = gridy; } public GBC(int gridx, int gridy, int gridWidth, int gridHeight){ this.gridx = gridx; this.gridy = gridy; this.gridwidth = gridWidth; this.gridheight = gridHeight; } /* * sets the anchor * @param anchor the anchor style * @return this object for further modification */ public GBC setAnchor(int anchor){ this.anchor = anchor; return this; } /* * sets the fill direction * @param fill the fill direction * @return this object for further modification */ public GBC setFill(int fill){ this.fill = fill; return this; } /* * sets the cell weights * @param weightx the cell weight in x direction * @param weighty the cell weight in y direction * @return this object for further modification */ public GBC setWeight(int weightx, int weighty){ this.weightx = weightx; this.weighty = weighty; return this; } /* * sets the insets of this cell * @param insets distance ths spacing to use in all directions * @return this object for further modification */ public GBC setInsets(int distance){ this.insets = new Insets(distance, distance, distance, distance); return this; } /* * sets the insets of this cell * @param top distance ths spacing to use on top * @param bottom distance ths spacing to use on bottom * @param left distance ths spacing to use to the left * @param right distance ths spacing to use to the right * @return this object for further modification */ public GBC setInsets(int top, int left,int bottom,int right){ this.insets = new Insets(top, left, bottom, right); return this; } /* * sets the Ipad of this cell * @param Ipad distance ths spacing to use in all directions * @return this object for further modification */ public GBC setIpad(int ipadx, int ipady){ this.ipadx = ipadx; this.ipadx = ipadx; return this; } }

運行效果圖
