很多情況下,我們已經不需要通過編寫代碼來實現一個應用程序的圖形界面,而是通過強大的IDE工具通過拖拽輔以簡單的事件處理代碼即可很輕松的完成。但是我們不得不面對這樣操作存在的一些問題,有時候我們希望能夠自由改變一個程序界面的大小,通過托拽生成的界面往往是不提供這個功能的,因為定制的界面一旦改變形狀,組件間的布局會變得雜亂無章。
Java中的布局管理器應用了策略者模式(Strategy),為不同類型的組件布局提供了很好的模型。而其中的網格組布局管理器(GridBagLayout)被認為是所有布局管理器中最強大的。下面將通過一個例子講述它的使用方法。
1.實現一個WindowsXP下畫圖板的框架,下面是設計模型圖
我們可以將GridBagLayout看作沒有任何約束或限制的網格布局(GridLayout),一個組件可以占據若干行和列,而且大小設定也是自由的。從上圖我們可以清晰的看到整個畫圖板界面被划分為4行2列的一個表格,我們先不用管某個單元格的大小,只管划分。
這樣五個具體的面板(我們使用JPanel對象進行填充)就構成了整個界面,上側占據1行2列的工具選擇面板(toolSelectPanel),左側1行1列的某個工具的選項面板(toolConcretePanel),右側的drawPanel,下側的colorPanel和statePanel
2.我們通過以下步驟為界面進行GridBagLayout布局
1).設置主界面的布局管理器為GridBagLayout(不用指定行和列)
2).為界面中的每一個組件(這里是JPanel對象)指定一個GridBagConstraints對象,通過設置該對象的屬性值指出組件在
管理器中的布局方案
3).通過下面的調用添加組件極其約束條件(GridBagConstraints對象)
add(Component,constraints);
我們有必要了解一下GridBagConstraints中各個屬性的具體含義以便我們更好的進行個性化的布局
@gridx,gridy:
組件左上角所在的位置,如上圖中左側的面板在1行0列,則gridy=0,gridx=1。讀者請注意這里的行對應的是gridy,列對應的是gridx
@gridwidth,gridheight
組件占據的行數和列數,如最上面的那個面板占了1行2列,則gridwidth=2,gridheight=1
@weightx,weighty
可以簡單理解為組件大小變化的增量值,如設置weightx=100,組件會隨着單元格而變化,設置weightx=0時,組件大小不會發生變化。當然weightx,weighty也可以設置成其他的值,不過意義不大,就不再詳細介紹。
@fill
組件在所處格子(分配區域)內的填充方式
如fill= HORIZONTAL,組件就只在水平方向上填充滿單元格,取fill= BOTH則會填滿整個格子。
@anchor
組件在所處格子內的對其方式,取anchor=EAST就是指右對齊
@ipadx,ipady
內部填充,是指在組件首選大小的基礎上x方向上加上ipadx,y方向上加上ipady,這樣做就可以保證組件不會收縮到ipadx,ipady所確定的大小以下,因此我們可以用ipadx,ipady的值來指定組件的大小,而不必指定組件的大小否則會有意想不到的效果
@insets
外部填充,填充的區域是組件與所處格子邊框之間的部分,有left,top,right,bottom四個參數,不過當組件的fill=NONE時,指定insects值是無意義的
3.下面就是代碼實現了,先看下效果圖吧
這是運行生成的界面
這是拉伸之后的界面
關鍵代碼如下
- private void addGridBagPanes() {
- //上側的工具選擇面板
- JPanel toolSelectPanel = new JPanel();
- toolSelectPanel.setBackground(Color.green);
- this.add(toolSelectPanel, new GBC(0,0,2,1).
- setFill(GBC.BOTH).setIpad(200, 50).setWeight(100, 0));
- //左側的具體工具面板
- JPanel toolConcretePanel = new JPanel();
- toolConcretePanel.setBackground(Color.YELLOW);
- this.add(toolConcretePanel,new GBC(0,1).
- setFill(GBC.BOTH).setIpad(70, 90).setWeight(0, 100));
- //右側的繪圖面板
- JPanel drawPanel = new JPanel();
- drawPanel.setBackground(Color.WHITE);
- this.add(drawPanel,new GBC(1,1).setFill(GBC.BOTH));
- //下側的顏色選擇面板
- JPanel colorPanel = new JPanel();
- colorPanel.setBackground(Color.LIGHT_GRAY);
- this.add(colorPanel,new GBC(0,2,2,1).
- setFill(GBC.BOTH).setIpad(200,50).setWeight(100, 0));
- //下側的狀態面板
- JPanel statePanel = new JPanel();
- statePanel.setBackground(Color.CYAN);
- this.add(statePanel,new GBC(0,3,2,1).
- setFill(GBC.BOTH).setIpad(200, 20).setWeight(100, 0));
- }
其中的GBC類繼承於GridBagConstraints這樣做的目的是簡化每次對GridBagConstraints對象的直接操作帶來的繁瑣,而GBC的各個set方法返回的都是一個GBC對象因此可以接連調用set方法。GBC類的代碼如下:
- public class GBC extends GridBagConstraints
- {
- //初始化左上角位置
- 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;
- }
- //對齊方式
- public GBC setAnchor(int anchor)
- {
- this.anchor = anchor;
- return this;
- }
- //是否拉伸及拉伸方向
- public GBC setFill(int fill)
- {
- this.fill = fill;
- return this;
- }
- //x和y方向上的增量
- public GBC setWeight(double weightx, double weighty)
- {
- this.weightx = weightx;
- this.weighty = weighty;
- return this;
- }
- //外部填充
- public GBC setInsets(int distance)
- {
- this.insets = new Insets(distance, distance, distance, distance);
- return this;
- }
- //外填充
- public GBC setInsets(int top, int left, int bottom, int right)
- {
- this.insets = new Insets(top, left, bottom, right);
- return this;
- }
- //內填充
- public GBC setIpad(int ipadx, int ipady)
- {
- this.ipadx = ipadx;
- this.ipady = ipady;
- return this;
- }
- }