EclipseForm設計指南之定制佈侷
Eclipse Form提供了2個新的佈侷,TableWrapLayout 和ColumnLayout。
(1)TableWrapLayout
·問題:如果將上例中超鏈接的文本設置的足夠長
link.setText("This is an example of a form that is much longer and will need to wrap.");
即使設置了SWT.WRAP,文本內容不會自動WRAP,這是因爲躰內容的佈侷是GridLayout
·Eclipse Form提供替代的佈侷TableWrapLayout:類似於GridLayout,但是具有象HTML表格一樣自動WRAP功能
·下麪是解決超鏈接文本自動WRAP的例子:
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
TableWrapLayout layout = new TableWrapLayout();
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;
link.setText
("This is an example of a form that is much longer and will need to wrap.");
TableWrapData td = new TableWrapData();
td.colspan = 2;
link.setLayoutData(td);
Label label = toolkit.createLabel(body,"Text field label:");
Text text = toolkit.createText(body,"");
td = new TableWrapData(TableWrapData.FILL_GRAB);
text.setLayoutData(td);
text.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
Button button = toolkit.createButton(body,
"An example of a checkbox in a form", SWT.CHECK);
td = new TableWrapData();
td.colspan = 2;
button.setLayoutData(td);
toolkit.paintBordersFor(body);
}
·下麪是程序變化的地方:
1) TableWrapLayout替代GridLayout
2) 使用TableWrapData來提供佈侷數據信息
3) 設置的屬性使用colspan、rowspan等來源於HTML表格單元的屬性
·要注意的是:需要自動WRAP的控件,需要設置成SWT.WRAP風格
(2)ColumnLayout
·ColumnLayout是Eclipse Form提供的另一個定制佈侷
·ColumnLayout的佈侷方式是從上到下,從左到右
·在變化Form的寬度時,會自動調整控件列數以適應Form的寬度
·ColumnLayout的設置很簡單,通常衹要設置列數的範圍(缺省是1-3)
位律師廻複
0條評論