SWT GridLayout 網格佈局

SWT GridLayout 網格佈局

GridLayout是一個非常強大的佈局管理器,它可以實現很多複雜的佈局,名字中暗示它將所有控件放置在類似網格的佈局中.^__^GridLayout有兩個構造函數. 

GridLayout的構造函數 
構造函數描述 
public GridLayout()建立一個默認的GridLayout佈局. 
public GridLayout(int numColumns, boolean makeColumnsEqualWidth)建立一個GridLayout佈局,擁有numColumns列,如果makeColumnsEqualWidth爲true,則全部組件將擁有相同的寬度. 
GridLayout有6個公共的數據成員,相對重要的是numColumns. 

GridLayout數據成員 
屬性描述 
int horizontalSpacing控制一行中兩個網格間組件的寬度,像素爲單位. 
boolean makeColumnsEqualWidth如果爲true,強制全部的列擁有相同的寬度. 
int marginHeight控制頂部和底部組件離邊緣的距離空間,以像素爲單位. 
int marginWidth控制左邊和右邊組件離邊緣的距離空間,以像素爲單位. 
int numColumns此GridLayout的列數目. 
int verticalSpacing控制一列中兩個網絡間組件的寬度,像素爲單位. 
你能使用GridLayout設置GridData類來佈局更多的複雜組件.GridData有兩個構造函數. 

GridData構造函數 
構造函數描述 
public GridData()創建默認一個默認的GridData對象. 
public GridData(int style)初始化指定的style風格佈局. 
和其它的佈局類一樣,GridLayout也提供一些公共的數據成員控制它的狀態,它也提供一些常量,你可以在在構造成函數中使用.你可以用過位標誌"|"來連接幾個不同的效果,但請注意風格不要起衝突. 

GridData數據成員 
屬性描述 
boolean grabExcessHorizontalSpace如果爲true,指示佈局器中網格自動填充多餘的水平空間,默認爲false. 
boolean grabExcessVerticalSpace如果爲true,指示佈局器中網格自動填寫多餘的垂直空間,默認爲false. 
int heightHint該行中最小的高度,以像素爲單位,默認爲SWT.DEFAULT. 
int horizontalAlignment網格中組件水平對齊方式,可能的值爲BEGINNING, CENTER, END,和FILL. 
int horizontalIndent與左邊網格的水平縮進,默認爲0 
int horizontalSpan設置網格佔有的列數目,默認爲1 
int verticalAlignment網格中組件垂直對齊方式,可能的值爲BEGINNING, CENTER, END,和FILL. 
int verticalSpan設置網格佔有的行數目,默認爲1 
int widthHint該列中最小的寬度,以像素爲單位,默認爲SWT.DEFAULT. 

GridData常量 
常量描述 
BEGINNING非style值,指定水平或垂直的對齊方式. 
CENTER非style值,指定組件在網格中居中,水平或垂直. 
END非style值,指定水平或垂直的對齊方式. 
FILL非style值,指定組件填充網格,水平或垂直. 
FILL_BOTH設置horizontalAlignment和verticalAlignment爲FILL,設置grabExcessHorizontalSpace和grabExcessVerticalSpace爲true. 
FILL_HORIZONTAL設置horizontalAlignment爲FILL,和設置grabExcessHorizontalSpace爲true. 
FILL_VERTICAL設置verticalAlignment爲FILL,和設置grabExcessVerticalSpace爲true. 
GRAB_HORIZONTAL設置grabExcessHorizontalSpace爲true. 
GRAB_VERTICAL設置grabExcessVerticalSpace爲true. 
HORIZONTAL_ALIGN_BEGINNING設置horizontalAlignment爲BEGINNING. 
HORIZONTAL_ALIGN_CENTER設置horizontalAlignment爲CENTER. 
HORIZONTAL_ALIGN_END設置horizontalAlignment爲END. 
HORIZONTAL_ALIGN_FILL設置horizontalAlignment爲FILL. 
VERTICAL_ALIGN_BEGINNING設置verticalAlignment爲BEGINNING. 
VERTICAL_ALIGN_CENTER設置verticalAlignment爲CENTER. 
VERTICAL_ALIGN_END設置verticalAlignment爲END. 
VERTICAL_ALIGN_FILL設置verticalAlignment爲FILL. 

下面我們來看幾個例子: 
代碼片段: 

GridLayout gridLayout = new GridLayout(); 
gridLayout.numColumns = 2; 
shell.setLayout(gridLayout); 

new Button(shell, SWT.PUSH).setText("one"); 
new Button(shell, SWT.PUSH).setText("two"); 
new Button(shell, SWT.PUSH).setText("three"); 
new Button(shell, SWT.PUSH).setText("four");運行效果: 


圖中可以看到各個Button的寬度是依靠字體長度來自動設定的,假如你想設置所有的Button都使用相同的大小該怎麼辦?使用makeColumnsEqualWidth = true;嗎?不,這樣做不行,它回會將各網格的大小設爲相同,我們該使用GridData中相關屬性來設置Button.下面程序中使用了GridData.FILL_BOTH屬性,它將填充所在網格水平或垂直空間,並將多餘空間佔有,這樣不論窗口調整多大,四個Button都會有相同空間. 
當然GridLayout能做的不只是這些,要更復雜的效果,可能多個Composite配合使用,具體請參見原書第二部分第四章:Apress.The.Definitive.Guide.to.SWT.and.JFace.eBook-LiB.chm 
運行效果 
初始化: 

調整大小後: 

完整代碼: 
package chapter4; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

/** 
*  

*/ 
public class GridLayoutDemo { 

public static void main(String[] args) { 

Display display = new Display(); 
Shell shell = new Shell(display); 

GridLayout gridLayout = new GridLayout(); 
//設置爲2列 
gridLayout.numColumns = 2; 
gridLayout.makeColumnsEqualWidth = true; 
shell.setLayout(gridLayout); 

GridData gd = new GridData(GridData.FILL_BOTH); 
Button butOne = new Button(shell, SWT.PUSH); 
butOne.setText("one"); 
butOne.setLayoutData(gd); 

gd = new GridData(GridData.FILL_BOTH); 
Button butTwo = new Button(shell, SWT.PUSH); 
butTwo.setText("two"); 
butTwo.setLayoutData(gd); 

gd = new GridData(GridData.FILL_BOTH); 
Button butThree = new Button(shell, SWT.PUSH); 
butThree.setText("three"); 
butThree.setLayoutData(gd); 

gd = new GridData(GridData.FILL_BOTH); 
Button butFour = new Button(shell, SWT.PUSH); 
butFour.setText("four"); 
butFour.setLayoutData(gd); 

shell.pack(); 
shell.open(); 

while (!shell.isDisposed()) { 
if (!display.readAndDispatch()) { 
display.sleep(); 



display.dispose(); 


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章