SWT(JFace)體驗(yàn)之GridLayout布局
GridLayout布局
GridLayout 布局的功能非常強(qiáng)大,也是筆者常用的一種布局方式。GridLayout是網(wǎng)格式布局,它把父組件分成一個(gè)表格,默認(rèn)情況下每個(gè)子組件占據(jù)一個(gè)單元格的空間,每個(gè)子組件按添加到父組件的順序排列在表格中。GridLayout提供了很多的屬性,可以靈活設(shè)置網(wǎng)格的信息。另外,GridLayout 布局提供了GridData類,子組件可以設(shè)置相應(yīng)的GridData,例如 “dogPhoto.setLayoutData(gridData)”,GridData可以設(shè)置每個(gè)組件當(dāng)做單元格的信息。
GridLayout的風(fēng)格
GridLayout類提供了GridLayout 布局中劃分網(wǎng)格的信息,主要通過以下幾個(gè)參數(shù)進(jìn)行設(shè)置。
NumColumns:通過“gridLayout.numColumns”屬性可以設(shè)置父組件中分幾列顯示子組件。
MakeColumnsEqualWidth:通過“gridLayout. makeColumnsEqualWidth”屬性可以設(shè)置父組件中子組件是否有相同的列寬,當(dāng)MakeColumnsEqualWidth為true時(shí)表示每列的列寬相等。
MarginLeft:表示當(dāng)前組件距離父組件左邊距的像素點(diǎn)個(gè)數(shù)。
MarginRight:表示當(dāng)前組件距離父組件右邊距的像素點(diǎn)個(gè)數(shù)。
MarginTop:表示當(dāng)前組件距離父組件上邊距的像素點(diǎn)個(gè)數(shù)。
MarginBottom:表示當(dāng)前組件距離父組件下邊距的像素點(diǎn)個(gè)數(shù)。
HorizontalSpacing:表示子組件的水平間距。
VerticalSpacing:表示子組件的垂直間距。
GridData的相關(guān)屬性
GridLayout布局的靈活之處在于它利用網(wǎng)格布局?jǐn)?shù)據(jù)GridData。通過GridData可以設(shè)置子組件在網(wǎng)格中的填充方式、大小邊距等信息,用戶可以通過子組件的setLayoutData方法設(shè)置網(wǎng)格布局?jǐn)?shù)據(jù)。
GridData可以控制子組件在網(wǎng)格中的位置大小等相關(guān)顯示信息。GridData可以設(shè)置如下的一些屬性。
HorizontalAlignment:表示水平對(duì)齊方式。
VerticalAlignment:表示子組件的垂直對(duì)齊方式,值和水平方式一樣。
HorizontalIndent:表示子組件水平偏移多少像素。此屬性和“horizontalAlignment = GridData.BEGINNING”屬性一起使用。
HorizontalSpan:表示組件水平占據(jù)幾個(gè)網(wǎng)格。
GrabExcessHorizontalSpace:表示當(dāng)父組件大小改變時(shí),子組件是否以水平方向搶占空間。
GrabExcessVerticalSpace:表示當(dāng)父組件大小改變時(shí),子組件是否以垂直方向搶占空間。
WidthHint:表示子組件的寬度為多少像素(前提是未設(shè)置其他相關(guān)屬性)。
HeightHint:表示子組件的高度為多少像素(前提是未設(shè)置其他相關(guān)屬性)。
另外,GridData可以通過構(gòu)造函數(shù)指定相應(yīng)的屬性值,有興趣的讀者可以參考GridData類的構(gòu)造函數(shù)。
測(cè)試代碼:
GridLayoutSample.java
package swt_jface.demo2;
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.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);
public GridLayoutSample() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
button1.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
gridData.horizontalIndent = 5;
button2.setLayoutData(gridData);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
button3.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new GridLayoutSample();
}
}
GridLayoutSampleGrabSpace.java
package swt_jface.demo2;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class GridLayoutSampleGrabSpace {
public GridLayoutSampleGrabSpace() {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
Label label = new Label(shell, SWT.BORDER);
label.setText("label");
GridData gridData3 = new GridData();
gridData3.widthHint = 60;
gridData3.heightHint = 20;
label.setLayoutData(gridData3);
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setText("text");
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
text.setLayoutData(gridData);
Button button = new Button(shell, SWT.PUSH);
button.setText("button");
GridData gridData2 = new GridData();
gridData2.grabExcessVerticalSpace = true;
gridData2.grabExcessHorizontalSpace = true;
gridData2.verticalAlignment = GridData.FILL;
gridData2.horizontalAlignment = GridData.FILL;
button.setLayoutData(gridData2);
shell.setSize(300, 80);
//shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new GridLayoutSampleGrabSpace();
}
}
GridLayoutSampleSpan.java
package swt_jface.demo2;
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.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutSampleSpan {
Display display = new Display();
Shell shell = new Shell(display);
public GridLayoutSampleSpan() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
button1.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
button2.setLayoutData(gridData);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
GridData gridData2 = new GridData(GridData.VERTICAL_ALIGN_END);
gridData2.verticalSpan = 3;
button3.setLayoutData(gridData2);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new GridLayoutSampleSpan();
}
}
下面這個(gè)例子布局稍微復(fù)雜一點(diǎn):
package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Sample {
Display display = new Display();
Shell shell = new Shell(display);
public Sample() {
shell.setText("Book Entry Demo");
GridLayout gridLayout = new GridLayout(4, false);
gridLayout.verticalSpacing = 8;
shell.setLayout(gridLayout);
Label label = new Label(shell, SWT.NULL);
label.setText("Title: ");
Text title = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
title.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Author(s): ");
Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
authors.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Cover: ");
gridData = new GridData();
gridData.verticalSpan = 3;
label.setLayoutData(gridData);
CLabel cover = new CLabel(shell, SWT.NULL);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 1;
gridData.verticalSpan = 3;
gridData.heightHint = 100;
gridData.widthHint = 100;
cover.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Pages");
Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER);
pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Publisher");
Text pubisher = new Text(shell, SWT.SINGLE | SWT.BORDER);
pubisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Rating");
Combo rating = new Combo(shell, SWT.READ_ONLY);
rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
rating.add("5");
rating.add("4");
rating.add("3");
rating.add("2");
rating.add("1");
label = new Label(shell, SWT.NULL);
label.setText("Abstract:");
Text bookAbstract =
new Text(
shell,
SWT.WRAP
| SWT.MULTI
| SWT.BORDER
| SWT.H_SCROLL
| SWT.V_SCROLL);
gridData =
new GridData(
GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
gridData.grabExcessVerticalSpace = true;
bookAbstract.setLayoutData(gridData);
Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = GridData.END;
enter.setLayoutData(gridData);
title.setText("Professional Java Interfaces with SWT/JFace");
authors.setText("Jack Li Guojie");
pages.setText("500pp");
pubisher.setText("John Wiley & Sons");
cover.setBackground(new Image(display, "C:/eclipse32.gif"));
bookAbstract.setText(
"This book provides a comprehensive guide for \n"
+ "you to create Java user interfaces with SWT/JFace. ");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new Sample();
}
}
GridLayout 布局的功能非常強(qiáng)大,也是筆者常用的一種布局方式。GridLayout是網(wǎng)格式布局,它把父組件分成一個(gè)表格,默認(rèn)情況下每個(gè)子組件占據(jù)一個(gè)單元格的空間,每個(gè)子組件按添加到父組件的順序排列在表格中。
GridLayout提供了很多的屬性,可以靈活設(shè)置網(wǎng)格的信息。另外,GridLayout 布局提供了GridData類,子組件可以設(shè)置相應(yīng)的GridData,例如“dogPhoto.setLayoutData(gridData)”,GridData可以設(shè)置每個(gè)組件當(dāng)做單元格的信息。
14.11.1 GridLayout的風(fēng)格
GridLayout類提供了GridLayout 布局中劃分網(wǎng)格的信息,主要通過以下幾個(gè)參數(shù)進(jìn)行設(shè)置。
NumColumns:通過“gridLayout.numColumns”屬性可以設(shè)置父組件中分幾列顯示子組件,如表14-4所示。
表14-4 NumColumns效果
列 數(shù) |
顯 示 效 果 |
numColumns = 1 |
498)this.style.width=498;" border=0> |
numColumns = 2 |
498)this.style.width=498;" border=0> |
numColumns = 3 |
498)this.style.width=498;" border=0> |
MakeColumnsEqualWidth:通過“gridLayout. makeColumnsEqualWidth”屬性可以設(shè)置父組件中子組件是否有相同的列寬,當(dāng)MakeColumnsEqualWidth為true時(shí)表示每列的列寬相等。
MarginLeft:表示當(dāng)前組件距離父組件左邊距的像素點(diǎn)個(gè)數(shù)。
MarginRight:表示當(dāng)前組件距離父組件右邊距的像素點(diǎn)個(gè)數(shù)。
MarginTop:表示當(dāng)前組件距離父組件上邊距的像素點(diǎn)個(gè)數(shù)。
MarginBottom:表示當(dāng)前組件距離父組件下邊距的像素點(diǎn)個(gè)數(shù)。
HorizontalSpacing:表示子組件的水平間距。
VerticalSpacing:表示子組件的垂直間距。
欄 目:Java編程
本文標(biāo)題:SWT(JFace)體驗(yàn)之GridLayout布局
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/8532.html
您可能感興趣的文章
- 01-10SWT(JFace)體驗(yàn)之FormLayout布局
- 01-10SWT(JFace)體驗(yàn)之RowLayout布局
- 01-10SWT JFace Bookmark 制作
- 01-10SWT(JFace)小制作 BugTracker
- 01-10SWT(JFace)體驗(yàn)之StyledText類
- 01-10SWT JFace 拖曳效果
- 01-10SWT(JFace)體驗(yàn)之ApplicationWindow
- 01-10SWT(JFace)體驗(yàn)之復(fù)制粘貼
- 01-10SWT(JFace)體驗(yàn)之打開多個(gè)Form
- 01-10SWT(JFace)體驗(yàn)之Sash(活動(dòng)控件)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒有被捕獲的異常
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)