網格袋佈局管理器中的約束條件constrains中有如下字段需要設置:
(1)constrains.gridx和constrains.gridy
這兩個字段僅用於指定組件的起始網格座標。
(2)constrains.gridwidth和constrains.gridheight
這兩個字段用於指定組件所佔網格的列數和行數。
(3)constrains.fill
組件在網格中的填充方式。GridBagConstrains中定義了一些常量用於確定組件在網格中的填充方式:
GridBagConstrains.HORIZONTAL //水平方向上儘量擴張
GridBagConstrains.VERTICAL //垂直方向上儘量擴張
GridBagConstrains.BOTH //水平、垂直方向上均擴展
GridBagConstrains.NONE //水平、垂直方向上均不擴張
(4)constrains.insets
給字段是一個insets類型的對象。該對象用作所添加組件的外部填塞,其大小由該對象的left、top、right
及button字段決定。
(5)constrains.ipax和constrains.ipay
與constrains.insets向對應,這兩個字段稱爲組件的內部填塞。這兩個值被加到組件的最小寬度和最小高度上,從而保證組件不會收縮到它的最小尺寸之下。
(6)constrains.anchor
當組件比所在網格小的時候,可以使用該字段來設定組件在網格內部的停靠位置。GridBagContrains包含了如下的常量來表示組件的停靠位置:
GridBagConstrains.CENTER
GridBagConstrains.NORTH
GridBagConstrains.NORTHEAST
GridBagConstrains.EAST
GridBagConstrains.SOUTHEAST......
(7)constrains.weightx和constrains.weighty
這兩個字段是組建在水平方向和垂直放的擴張權重。如果不希望在某個方向上擴張組件,則將該方向上的權重重置爲0.
需要注意的是:權重字段的取值需要和fill字段相適應。
(1)constrains.gridx和constrains.gridy
這兩個字段僅用於指定組件的起始網格座標。
(2)constrains.gridwidth和constrains.gridheight
這兩個字段用於指定組件所佔網格的列數和行數。
(3)constrains.fill
組件在網格中的填充方式。GridBagConstrains中定義了一些常量用於確定組件在網格中的填充方式:
GridBagConstrains.HORIZONTAL //水平方向上儘量擴張
GridBagConstrains.VERTICAL //垂直方向上儘量擴張
GridBagConstrains.BOTH //水平、垂直方向上均擴展
GridBagConstrains.NONE //水平、垂直方向上均不擴張
(4)constrains.insets
給字段是一個insets類型的對象。該對象用作所添加組件的外部填塞,其大小由該對象的left、top、right
及button字段決定。
(5)constrains.ipax和constrains.ipay
與constrains.insets向對應,這兩個字段稱爲組件的內部填塞。這兩個值被加到組件的最小寬度和最小高度上,從而保證組件不會收縮到它的最小尺寸之下。
(6)constrains.anchor
當組件比所在網格小的時候,可以使用該字段來設定組件在網格內部的停靠位置。GridBagContrains包含了如下的常量來表示組件的停靠位置:
GridBagConstrains.CENTER
GridBagConstrains.NORTH
GridBagConstrains.NORTHEAST
GridBagConstrains.EAST
GridBagConstrains.SOUTHEAST......
(7)constrains.weightx和constrains.weighty
這兩個字段是組建在水平方向和垂直放的擴張權重。如果不希望在某個方向上擴張組件,則將該方向上的權重重置爲0.
需要注意的是:權重字段的取值需要和fill字段相適應。