se Form程序设计快速入门(3)
时间:2010-12-24
(3)添加通用控件
·由于Form的体内容是一个Composite对象,所以允许在其中创建SWT控件
·但是SWT控件是被设计为适合窗口、对话框的,所以在Form中使用是有问题的
在Form中,使用FormToolkit创建对应的通用控件
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
GridLayout layout = new GridLayout();
body.setLayout(layout);
Hyperlink link = toolkit.createHyperlink(body, "Click here.",SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
layout.numColumns = 2;
GridData gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Label label = toolkit.createLabel(body, "Text field label:");
Text text = toolkit.createText(body, "");
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
Button button = toolkit.createButton(body,"An example of a checkbox in a form", SWT.CHECK);
gd = new GridData();
gd.horizontalSpan = 2;
button.setLayoutData(gd);
toolkit.paintBordersFor(body);
}
上面的例子添加了三个通用控件:Label、Text和CheckBox
由于缺省创建的Text控件的外观是3D的,而要达到象PDE一样的FLAT外观,需要做些额外工作:
·调用setData()方法,添加重画边框的附加信息
·调用FormToolkit的paintBordersFor()方法重画FLAT外观的边框 |