麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

SWT(JFace)體驗之GridLayout布局

2020-01-31 16:51:39
字體:
來源:轉載
供稿:網友
GridLayout布局

GridLayout 布局的功能非常強大,也是筆者常用的一種布局方式。GridLayout是網格式布局,它把父組件分成一個表格,默認情況下每個子組件占據一個單元格的空間,每個子組件按添加到父組件的順序排列在表格中。GridLayout提供了很多的屬性,可以靈活設置網格的信息。另外,GridLayout 布局提供了GridData類,子組件可以設置相應的GridData,例如 “dogPhoto.setLayoutData(gridData)”,GridData可以設置每個組件當做單元格的信息。

GridLayout的風格

GridLayout類提供了GridLayout 布局中劃分網格的信息,主要通過以下幾個參數進行設置。

NumColumns:通過“gridLayout.numColumns”屬性可以設置父組件中分幾列顯示子組件。

MakeColumnsEqualWidth:通過“gridLayout. makeColumnsEqualWidth”屬性可以設置父組件中子組件是否有相同的列寬,當MakeColumnsEqualWidth為true時表示每列的列寬相等。
MarginLeft:表示當前組件距離父組件左邊距的像素點個數。
MarginRight:表示當前組件距離父組件右邊距的像素點個數。
MarginTop:表示當前組件距離父組件上邊距的像素點個數。
MarginBottom:表示當前組件距離父組件下邊距的像素點個數。
HorizontalSpacing:表示子組件的水平間距。
VerticalSpacing:表示子組件的垂直間距。

GridData的相關屬性

GridLayout布局的靈活之處在于它利用網格布局數據GridData。通過GridData可以設置子組件在網格中的填充方式、大小邊距等信息,用戶可以通過子組件的setLayoutData方法設置網格布局數據。

GridData可以控制子組件在網格中的位置大小等相關顯示信息。GridData可以設置如下的一些屬性。

HorizontalAlignment:表示水平對齊方式。

VerticalAlignment:表示子組件的垂直對齊方式,值和水平方式一樣。
HorizontalIndent:表示子組件水平偏移多少像素。此屬性和“horizontalAlignment = GridData.BEGINNING”屬性一起使用。

HorizontalSpan:表示組件水平占據幾個網格。

GrabExcessHorizontalSpace:表示當父組件大小改變時,子組件是否以水平方向搶占空間。
GrabExcessVerticalSpace:表示當父組件大小改變時,子組件是否以垂直方向搶占空間。
WidthHint:表示子組件的寬度為多少像素(前提是未設置其他相關屬性)。
HeightHint:表示子組件的高度為多少像素(前提是未設置其他相關屬性)。

另外,GridData可以通過構造函數指定相應的屬性值,有興趣的讀者可以參考GridData類的構造函數。

測試代碼:

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();
}
}

下面這個例子布局稍微復雜一點:
復制代碼 代碼如下:

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 布局的功能非常強大,也是筆者常用的一種布局方式。GridLayout是網格式布局,它把父組件分成一個表格,默認情況下每個子組件占據一個單元格的空間,每個子組件按添加到父組件的順序排列在表格中。

GridLayout提供了很多的屬性,可以靈活設置網格的信息。另外,GridLayout 布局提供了GridData類,子組件可以設置相應的GridData,例如“dogPhoto.setLayoutData(gridData)”,GridData可以設置每個組件當做單元格的信息。

14.11.1 GridLayout的風格

GridLayout類提供了GridLayout 布局中劃分網格的信息,主要通過以下幾個參數進行設置。

NumColumns:通過“gridLayout.numColumns”屬性可以設置父組件中分幾列顯示子組件,如表14-4所示。

表14-4  NumColumns效果

列    數

顯 示 效 果

numColumns = 1

 

numColumns = 2

 

numColumns = 3

 


MakeColumnsEqualWidth:通過“gridLayout. makeColumnsEqualWidth”屬性可以設置父組件中子組件是否有相同的列寬,當MakeColumnsEqualWidth為true時表示每列的列寬相等。

MarginLeft:表示當前組件距離父組件左邊距的像素點個數。

MarginRight:表示當前組件距離父組件右邊距的像素點個數。

MarginTop:表示當前組件距離父組件上邊距的像素點個數。

MarginBottom:表示當前組件距離父組件下邊距的像素點個數。

HorizontalSpacing:表示子組件的水平間距。

VerticalSpacing:表示子組件的垂直間距。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91aa.app | 视频一区二区中文字幕 | 欧美精品v国产精品v日韩精品 | 欧美一级精品片在线看 | 福利在线免费 | 国产在线精品一区二区三区不卡 | 精品在线观看一区二区三区 | 一区二区三高清 | 国产91av视频 | 久久这 | 色就色 综合偷拍区91网 | 粉嫩av一区二区三区四区在线观看 | 2021免费日韩视频网 | 国产亚洲福利 | 国产精品久久久久影院老司 | 午夜色片 | 精品国产99久久久久久宅男i | 精品国产96亚洲一区二区三区 | 操碰网| 国产成人综合在线视频 | 一区二区三区国产视频 | 久久精品一级片 | 精品一区二区免费视频视频 | 红桃一区 | 欧美高清在线精品一区二区不卡 | 国产亚洲精品综合一区91555 | 亚洲视频观看 | 在线播放91 | 午夜色片| 成人精品一区二区 | 91成人一区 | 国产一区二区三区精品在线观看 | 久久精品国产一区二区电影 | 午夜久 | 在线观看视频毛片 | 国产福利视频在线观看 | 中文字幕精品在线播放 | 中文字幕国产一区 | 免费1级做55爰片l在线观看 | 亚洲国产美女视频 | 国产精品免费看 |