首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Eclipse Forms 快速入門:構建專業級 UI 的表單式編輯器框架

     編輯:Java 程式設計入門

Eclipse Forms (org.eclipse.ui.forms) 是 Eclipse 3.0 引入的 UI 工具套件,專為構建「表單式」編輯器/檢視設計 — 類似屬性頁、Wizard、PDE 插件清單編輯器。本文從 HelloWorld 到佈局管理、自定義控制項、邊框繪製,帶您快速掌握核心 API。

核心類別速覽

類別角色
`FormToolkit`工廠/管理者:建立 Form、控制項、管理色彩/字型/邊框繪製、資源釋放
`ScrolledForm`可捲動 Form 容器,繼承 `Form`,含 `getBody()` 取得內容區 `Composite`
`Form`表單根容器,提供標題區、工具列、訊息區、內容區
`Section`可摺疊區塊容器,支援標題、客製化控制項
`Hyperlink`、`ImageHyperlink`超連結控制項,支援 `HyperlinkListener`
`TableWrapLayout`/`TableWrapData`HTML 表格式佈局管理器,支援跨行跨列、對齊、權重

階段 1:HelloWorld — 最小 Form 檢視


public class FormView extends ViewPart {
    private FormToolkit toolkit;
    private ScrolledForm form;

    public void createPartControl(Composite parent) {
        toolkit = new FormToolkit(parent.getDisplay());
        form = toolkit.createScrolledForm(parent);
        form.setText("Hello, Eclipse Forms");
    }

    public void setFocus() { form.setFocus(); }

    public void dispose() {
        toolkit.dispose(); // 必須釋放色彩、字型、圖片資源
        super.dispose();
    }
}

plugin.xml 關鍵配置


<extension point="org.eclipse.ui.views">
  <view class="com.example.FormView" name="Form Sample" id="FormView"/>
</extension>
<requires>
  <import plugin="org.eclipse.ui.forms"/> <!-- 必須相依 -->
</requires>

階段 2:添加內容與超連結


Composite body = form.getBody();
GridLayout layout = new GridLayout(); // 或 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!");
    }
});

階段 3:使用 FormToolkit 建立標準控制項 (扁平化外觀)

直接使用 SWT 控制項會呈現 3D 原生外觀,不符合 Forms 風格。必須透過 `FormToolkit` 建立:


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 checkbox = toolkit.createButton(body, "An example of a checkbox in a form", SWT.CHECK);
gd = new GridData(); gd.horizontalSpan = 2;
checkbox.setLayoutData(gd);

// 統一重繪所有工具包建立控制項的邊框
toolkit.paintBordersFor(body);

關鍵點解析

  • `FormToolkit.createText()` 建立的 Text 預設無邊框,需設定 `KEY_DRAW_BORDER` 客屬性啟用扁平邊框。
  • `paintBordersFor(Composite)` 遞迴重繪該容器下所有工具包管理控制項的邊框 (Focus、Hover 狀態)。
  • 色彩/字型統一由 `FormToolkit` 管理,支援高對比度、系統主題切換自動適配。

階段 4:TableWrapLayout — HTML 表格式彈性佈局


TableWrapLayout twLayout = new TableWrapLayout();
twLayout.numColumns = 3;
body.setLayout(twLayout);

Label l1 = toolkit.createLabel(body, "Name:");
Text t1 = toolkit.createText(body, "");
TableWrapData td1 = new TableWrapData(TableWrapData.FILL_GRAB);
td1.colspan = 2; // 跨兩欄
t1.setLayoutData(td1);

Section section = toolkit.createSection(body, Section.TITLE_BAR | Section.TWISTIE);
section.setText("Advanced Options");
section.setLayout(new TableWrapLayout());
section.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
// ... 向 section.getClient() 添加子控制項

資源管理鐵律

  • `FormToolkit` 持有色彩、字型、圖片等裝置資源,**必須在 `dispose()` 中呼叫 `toolkit.dispose()`**。
  • 建議在 `ViewPart`/`EditorPart` 的 `dispose()` 中釋放,或使用 `try-with-resources` 模式 (需自行實作 `AutoCloseable` 包裝)。

進階應用場景

  1. 多頁編輯器: `FormEditor` + `FormPage`,每頁一個 Form (如 PDE plugin.xml 編輯器)。
  2. Wizard 頁面: `WizardPage` 中使用 FormToolkit 建立一致風格頁面。
  3. 屬性視圖: `PropertySheetPage` 整合 Forms 控制項。
  4. 客製化繪製: 繼承 `FormToolkit` 覆寫 `paintBorder()`、`createButton()` 實現品牌化 UI。

Eclipse Forms 以「工具包統一風格 + 表格式佈局 + 豐富表單控制項」三位一體,成為 Eclipse 平台專業應用 UI 的標準范式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved