·首先獲得Form的體內容它是一個Composite對象
·設置其布局為GridLayout
·通過FormToolkit創建一個超鏈接(Hyperlink)控件
·添加超鏈接事件偵聽器響應超鏈接的點擊
()添加通用控件
·由於Form的體內容是一個Composite對象所以允許在其中創建SWT控件
·但是SWT控件是被設計為適合窗口對話框的所以在Form中使用是有問題的
·在Form中使用FormToolkit創建對應的通用控件
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parentgetDisplay());
form = toolkitcreateScrolledForm(parent);
formsetText(Hello Eclipse Forms);
Composite body = formgetBody();
GridLayout layout = new GridLayout();
bodysetLayout(layout);
Hyperlink link = toolkitcreateHyperlink(body Click hereSWTWRAP);
linkaddHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
Systemoutprintln(Link activated!);
}
});
layoutnumColumns = ;
GridData gd = new GridData();
gdhorizontalSpan = ;
linksetLayoutData(gd);
Label label = toolkitcreateLabel(body Text field label:);
Text text = toolkitcreateText(body );
textsetLayoutData(new GridData(GridDataFILL_HORIZONTAL));
textsetData(FormToolkitKEY_DRAW_BORDER FormToolkitTEXT_BORDER);
Button button = toolkitcreateButton(bodyAn example of a checkbox in a form SWTCHECK);
gd = new GridData();
gdhorizontalSpan = ;
buttonsetLayoutData(gd);
toolkitpaintBordersFor(body);
}
·上面的例子添加了三個通用控件LabelText和CheckBox
·由於缺省創建的Text控件的外觀是D的而要達到象PDE一樣的FLAT外觀需要做些額外工作
) 調用setData()方法添加重畫邊框的附加信息
) 調用FormToolkit的paintBordersFor()方法重畫FLAT外觀的邊框
[] [] []
From:http://tw.wingwit.com/Article/program/Java/ky/201311/29034.html