国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

實(shí)戰(zhàn)SWT布局

2019-11-18 14:27:09
字體:
供稿:網(wǎng)友

  標(biāo)準(zhǔn)的SWT布局類
FillLayout:在容器中以相同的大小單行或單列的排列組件
RowLayout:以單行或多行的方式使用幾個選項(xiàng)(fill,wrap,spacing,justify,type)定制組件的排列方式
GridLayout:類似于swing的GridLayout的方式以格子的方式排列組件
FormLayout(SWT 2.0的新特性):通過定義組件四個邊的“粘貼”位置來排列組件,被引用的相對的組件可以父組件,也可以是同一容器中的其它組件。
在SWT中,可以由用戶自定義布局類。

在簡單的布局中,使用FillLayout和RowLayout也許就夠用了,也比較簡單。但是通常的布局都比較復(fù)雜,而且要求很精確。無論復(fù)雜程度如何,都可以由GridLayout或FormLayout來完成。通常GridLayout與FormLayout可以做成同樣的效果,但是使用FormLayout更有效,不會像GridLayout產(chǎn)生resize導(dǎo)致的布局錯位,也更簡單。下面通過一個例子簡單介紹FormLayout的使用。

布局效果


布局實(shí)施
1.首先定義窗口和它的空白邊
Display.getDefault().dispose(); //移去平臺核心啟動畫面
display = new Display();
shell = new Shell(display, SWT.TITLE);
FormLayout layout = new FormLayout();
layout.marginHeight = 10;
layout.marginWidth = 20;
shell.setLayout(layout);
shell.setText("用戶登錄");

2.創(chuàng)建窗口上的元素,其中下面的兩個button由一個使用RowLayout的組件來包容。
name = new Label(shell, SWT.NONE);
name.setText("用戶名");
nameText = new Text(shell, SWT.SINGLE SWT.BORDER);
pass = new Label(shell, SWT.NONE);
pass.setText("密碼");
passText = new Text(shell, SWT.SINGLE SWT.BORDER);
passText.setEchoChar(´*´);
passText.setTabs(2);
bottom = new Composite(shell, SWT.NONE);
RowLayout rowLayout = new RowLayout();
rowLayout.justify = true; //justify定義組件在容器中分散開,效果類似于swing的FlowLayout
bottom.setLayout(rowLayout);

3.定義name標(biāo)簽的位置。它的頂邊離父組件(窗口shell)的空白邊距離是父組件clientArea(除空白邊以外的空間)高度(height)的15%,偏移的點(diǎn)數(shù)(points)為0。
FormData data = new FormData();
data.top = new FormAttachment(15, 0);
name.setLayoutData(data);

4.定義name文本輸入的位置。它的頂邊在name標(biāo)簽的中心位置(這不是正確的表達(dá),但程序是這樣解釋,事實(shí)上它的中心位置與name標(biāo)簽在同一條水平線上),左邊距name標(biāo)簽的右邊有10個點(diǎn)。
data = new FormData();
data.top = new FormAttachment(name, 0, SWT.CENTER);
data.left = new FormAttachment(name, 10, SWT.RIGHT);
nameText.setLayoutData(data);

5.定義pass標(biāo)簽的位置。它的頂邊距name標(biāo)簽的底邊有10個點(diǎn)數(shù)的偏移。
data = new FormData();
data.top = new FormAttachment(name, 10, SWT.BOTTOM);
pass.setLayoutData(data);

6.定義pass文本輸入的位置。它的頂邊在name標(biāo)簽的中心位置(同上),左邊與name文本框的左邊對齊。
data = new FormData();
data.top = new FormAttachment(pass, 0, SWT.CENTER);
data.left = new FormAttachment(nameText, 0, SWT.LEFT);
passText.setLayoutData(data);

7.定義bottom組件的位置。它的頂邊距pass標(biāo)簽的底邊15個點(diǎn)數(shù),左邊與pass標(biāo)簽的左邊對齊,右邊與pass文本輸入的右邊對齊。
data = new FormData();
data.top = new FormAttachment(pass, 15, SWT.BOTTOM);
data.left = new FormAttachment(pass, 0, SWT.LEFT);
data.right = new FormAttachment(passText, 0, SWT.RIGHT);
bottom.setLayoutData(data);

完整的源碼
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.com.efly.clientframe.core.Hook;

/**
* @author efly
* @version 1.0.0,11/22/02
*/
public final class LoginUI {
PRivate Display display;
private Shell shell;
private Composite bottom;
private Label name;
private Label pass;
private Text nameText;
private Text passText;
private Button ok;
private Button exit;
private Rectangle clientArea;
private RootHook rootHook;

public LoginUI(Hook hook) {
rootHook = (RootHook) hook;
}

/**
* 顯示登陸界面
*/
public void show() {
Display.getDefault().dispose();
display = new Display();
clientArea = display.getClientArea();
shell = new Shell(display, SWT.TITLE);
FormLayout layout = new FormLayout();
layout.marginHeight = 10;
layout.marginWidth = 20;
shell.setLayout(layout);
shell.setText("用戶登錄");

name = new Label(shell, SWT.NONE);
name.setText("用戶名");
nameText = new Text(shell, SWT.SINGLE SWT.BORDER);
pass = new Label(shell, SWT.NONE);
pass.setText("密碼");
passText = new Text(shell, SWT.SINGLE SWT.BORDER);
passText.setEchoChar(´*´);
passText.setTabs(2);
bottom = new Composite(shell, SWT.NONE);
RowLayout rowLayout = new RowLayout();
rowLayout.justify = true;
bottom.setLayout(rowLayout);
ok = new Button(bottom, SWT.PUSH);
ok.setText("確定");
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
ok();
}
});
exit = new Button(bottom, SWT.PUSH);
exit.setText("取消");
exit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
cancel();
}
});

FormData data = new FormData();
data.top = new FormAttachment(15, 0);
name.setLayoutData(data);

data = new FormData();
data.top = new FormAttachment(name, 0, SWT.CENTER);
data.left = new FormAttachment(name, 10, SWT.RIGHT);
nameText.setLayoutData(data);

data = new FormData();
data.top = new FormAttachment(name, 10, SWT.BOTTOM);
pass.setLayoutData(data);

data = new FormData();
data.top = new FormAttachment(pass, 0, SWT.CENTER);
data.left = new FormAttachment(nameText, 0, SWT.LEFT);
passText.setLayoutData(data);

data = new FormData();
data.top = new FormAttachment(pass, 15, SWT.BOTTOM);
data.left = new FormAttachment(pass, 0, SWT.LEFT);
data.right = new FormAttachment(passText, 0, SWT.RIGHT);
bottom.setLayoutData(data);

shell.pack();
Rectangle shellBounds = shell.getBounds();
shell.setLocation(
(clientArea.width - shellBounds.width) / 2,
(clientArea.height - shellBounds.height) / 2);

shell.open();

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

private void dispose() {
display.dispose();

}

private void cancel() {
dispose();
}

private void ok() {
verify();
}

private void verify() {
rootHook.runPlatform();
}

public static void main(String[]){
new LoginUI(null).show();
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平乡县| 张家界市| 博乐市| 长宁县| 平远县| 平乐县| 吉安县| 綦江县| 财经| 大石桥市| 北川| 沁阳市| 梨树县| 孝昌县| 延寿县| 平远县| 万宁市| 河南省| 杭锦旗| 桦川县| 涞水县| 宁国市| 香格里拉县| 达拉特旗| 壶关县| 民县| 泾川县| 景宁| 吴旗县| 定南县| 丽水市| 古蔺县| 博兴县| 马鞍山市| 舒城县| 阿勒泰市| 玉树县| 嘉鱼县| 沙田区| 岳池县| 如皋市|