??? 作SWT程序界面時很多窗體的大小是可變。在大小不固定的窗體里怎么使里面的控件在任意大小下都能保持美觀是將讓人頭疼的事情。FormLayout的布局方式就是解決這一問題的靈丹妙藥。上圖是王道,看圖先:
我胖

我瘦

我標準

以上就是一個窗體在不同大小下的效果,控件隨著窗體的大小改變大小和位置,這就是FormLayout的布局方式的的功勞。
FormLayout的布局方式的精髓就兩個字“相對”。
下面先看看設計界面:

在設計器里面點擊文本框[name_text]可以看到上下左右各出現一個小箭頭,這四個箭頭就是用來設置該控件在四個方向上的相對位置。
先看看上:
點擊上箭頭會出現五個小按鈕
,
第一個是設置控件[上邊]相對容器[上邊],偏移量為具體數字。
第二個是置控件[上邊]相對容器[底邊],偏移量為具體數字。
第三個是設置控件[上邊]相對容器[上邊],偏移量為百分比。
第四個是設置控件[上邊]相對容器內某控件的位置,偏移量為具體數字。
第五個是取消設置。
左
箭頭、右
箭頭和下
箭頭點擊以后與此類似。
舉例說明:
對于
文本框[name_text],我們這樣設置:
[上邊]相對容器[上邊],偏移量為具體數字。[左邊]相對容器[左邊],偏移量為具體數字。[右邊]相對容器[右邊],偏移量為具體數字。(當容器改變寬度的時候,控件寬度跟著改變)
[下邊]相對容器[上邊],偏移量為具體數字。(當容器高度變化時,高度不變)
對于文本框[name_remark],我們這樣設置:
[上邊]相對容器[上邊],偏移量為具體數字。[左邊]相對容器[左邊],偏移量為具體數字。[右邊]相對容器[右邊],偏移量為具體數字。(當容器改變寬度的時候,控件寬度跟著改變)
[下邊]相對容器[下邊],偏移量為具體數字。(當容器高度變化時,控件高度跟著改變)
通過A相對B這種方式。我們能制作出非常實用美觀的界面。
以下是代碼:
import?org.eclipse.swt.SWT;
import?org.eclipse.swt.layout.FillLayout;
import?org.eclipse.swt.layout.FormAttachment;
import?org.eclipse.swt.layout.FormData;
import?org.eclipse.swt.layout.FormLayout;
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;
public?class?TestFormLayout?{
????private?Text?text_remark;
????private?Text?text_name;
????protected?Shell?shell;
????/**
?????*?Launch?the?application
?????*?@param?args
?????*/
????public?static?void?main(String[]?args)?{
????????try?{
????????????TestFormLayout?window?=?new?TestFormLayout();
????????????window.open();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
????/**
?????*?Open?the?window
?????*/
????public?void?open()?{
????????final?Display?display?=?Display.getDefault();
????????createContents();
????????shell.open();
????????shell.layout();
????????while?(!shell.isDisposed())?{
????????????if?(!display.readAndDispatch())
????????????????display.sleep();
????????}
????}
????/**
?????*?Create?contents?of?the?window
?????*/
????protected?void?createContents()?{
????????shell?=?new?Shell();
????????shell.setLayout(new?FillLayout());
????????shell.setSize(500,?375);
????????shell.setText("SWT?Application");
????????Label?label_remark;
????????final?Composite?composite?=?new?Composite(shell,?SWT.NONE);
????????composite.setLayout(new?FormLayout());
????????final?Label?label_name?=?new?Label(composite,?SWT.NONE);
????????final?FormData?formData?=?new?FormData();
????????formData.top?=?new?FormAttachment(0,?25);
????????formData.left?=?new?FormAttachment(0,?30);
????????formData.right?=?new?FormAttachment(0,?60);
????????label_name.setLayoutData(formData);
????????label_name.setText("姓名");
????????text_name?=?new?Text(composite,?SWT.BORDER);
????????formData.bottom?=?new?FormAttachment(text_name,?0,?SWT.BOTTOM);
????????final?FormData?formData_1?=?new?FormData();
????????formData_1.top?=?new?FormAttachment(0,?25);
????????formData_1.right?=?new?FormAttachment(100,?-32);
????????formData_1.bottom?=?new?FormAttachment(0,?43);
????????formData_1.left?=?new?FormAttachment(label_name,?5,?SWT.DEFAULT);
????????text_name.setLayoutData(formData_1);
????????text_remark?=?new?Text(composite,?SWT.BORDER);
????????final?FormData?formData_2?=?new?FormData();
????????formData_2.bottom?=?new?FormAttachment(100,?-16);
????????formData_2.right?=?new?FormAttachment(100,?-32);
????????formData_2.top?=?new?FormAttachment(0,?62);
????????formData_2.left?=?new?FormAttachment(0,?65);
????????text_remark.setLayoutData(formData_2);
????????label_remark?=?new?Label(composite,?SWT.NONE);
????????final?FormData?formData_3?=?new?FormData();
????????formData_3.top?=?new?FormAttachment(44,?0);
????????formData_3.bottom?=?new?FormAttachment(51,?0);
????????formData_3.right?=?new?FormAttachment(0,?60);
????????formData_3.left?=?new?FormAttachment(0,?30);
????????label_remark.setLayoutData(formData_3);
????????label_remark.setText("說明");
????????
posted on 2006-12-25 13:23
交口稱贊 閱讀(5063)
評論(6) 編輯 收藏 所屬分類:
Eclipse RCP SWT