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

SWT/JFace ViewForm 容器實戰:工具列與表格的靈活佈局組合

     編輯:Java 相關知識

前言

ViewForm 是 SWT Custom 套件中特殊的容器,專為「頂部工具列/標籤 + 內容區」的經典介面模式設計。它繼承自 Composite,內建四個錨點區域:TopLeft、TopCenter、TopRight、Content。本文展示基本用法與結合 JFace TableViewer、ToolBarManager 的實戰整合模式。

ViewForm 核心 API

方法說明
setTopLeft(Control)左上角區域(通常放工具列、標籤)
setTopCenter(Control)中上區域(標題、搜尋框)
setTopRight(Control)右上區域(動作按鈕、狀態)
setContent(Control)主內容區(表格、樹、編輯器)
getTopLeft()/getTopCenter()/getTopRight()/getContent()獲取對應控件

基礎範例:簡單標籤佈局

public class ViewFormExample {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        ViewForm viewForm = new ViewForm(shell, SWT.BORDER);
        
        Label topLabel = new Label(viewForm, SWT.NONE);
        topLabel.setText("Top Center Title");
        viewForm.setTopCenter(topLabel);

        shell.setSize(400, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

實戰整合:TableViewer + ToolBarManager

場景:表格上方顯示工具列,工具列由 JFace ActionGroup 管理動作。

// 原主方法中的 open() 修改如下:
public void open() {
    // ... 原有初始化代碼 ...
    
    // 1. Shell 使用 FillLayout 作為基座
    shell.setLayout(new FillLayout());
    
    // 2. 建立 ViewForm 作為佈局容器
    ViewForm viewForm = new ViewForm(shell, SWT.NONE);
    viewForm.setLayout(new FillLayout()); // Content 區填滿
    
    // 3. 建立 TableViewer,父容器設為 viewForm
    TableViewer tv = new TableViewer(viewForm, SWT.BORDER | SWT.FULL_SELECTION);
    // ... 配置 Column、LabelProvider、ContentProvider、Input ...
    
    // 4. 建立工具列,父容器同為 viewForm
    ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT);
    ToolBarManager toolBarManager = new ToolBarManager(toolBar);
    
    // 5. 將 ActionGroup 的動作注入工具列
    actionGroup.fillActionToolBars(toolBarManager);
    
    // 6. 關鍵:設置 ViewForm 佈局位置
    viewForm.setContent(tv.getControl());   // 主體:表格
    viewForm.setTopLeft(toolBar);           // 頂端左側:工具列
    
    // 7. 開啟視窗
    shell.open();
    // ... 事件循環 ...
}

佈局效果示意

+-------------------------------------------------------+
| ViewForm (Shell)                                      |
| +-------------------+-------------------+-------------+ |
| | ToolBar (TopLeft) | TopCenter (empty) | TopRight    | |
| | [New] [Edit] [Del]|                   | (empty)     | |
| +-------------------+-------------------+-------------+ |
| |                                                   | |
| |                  TableViewer                      | |
| |                  (Content)                        | |
| |                                                   | |
| +---------------------------------------------------+ |
+-------------------------------------------------------+

進階用法

1. 三區頂部佈局

// 左側:工具列
viewForm.setTopLeft(toolBar);

// 中間:搜尋框
Text search = new Text(viewForm, SWT.SEARCH | SWT.BORDER);
search.setMessage("Search...");
viewForm.setTopCenter(search);

// 右側:篩選下拉
Combo filter = new Combo(viewForm, SWT.READ_ONLY);
filter.setItems("All", "Active", "Archived");
filter.select(0);
viewForm.setTopRight(filter);

2. 動態切換內容

// 切換到不同 Viewer
viewForm.setContent(newTableViewer.getControl());
viewForm.layout(true); // 重新佈局

3. 結合 ScrolledComposite 實現內容捲動

ScrolledComposite sc = new ScrolledComposite(viewForm, SWT.H_SCROLL | SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);

Composite content = new Composite(sc, SWT.NONE);
content.setLayout(new GridLayout());
// ... 添加大量控件 ...

sc.setContent(content);
sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));

viewForm.setContent(sc);

ViewForm vs 手動 GridLayout/FormLayout

維度ViewFormGridLayout 手動FormLayout 手動
代碼量極少(4 行設定)中等(需 GridData span/grab)較多(需 FormAttachment 鏈)
語義清晰度高(TopLeft/Content 直觀)低(需閱讀 GridData)中(附著關係隱性)
彈性固定四區域高(任意網格)最高(像素級控制)
適用場景標準「工具列+內容」介面複雜表單、多區域精確對話框、重疊介面

常見問題

  1. 工具列不顯示:確認 viewForm.setTopLeft(toolBar) 在 shell.open() 前呼叫,且 toolBar 父容器是 viewForm。
  2. 內容區不填滿:ViewForm 本身需有佈局(如 FillLayout),或父容器給足空間。
  3. 高度計算異常:Top 區域高度由其內容決定,Content 區域填滿剩餘。若 Top 區域過高,檢查其中控件的 computeSize。

總結

ViewForm 是「工具列 + 內容區」標準介面的最佳實踐容器:

  • 零佈局代碼:四個 setXxx() 完成專業介面結構。
  • JFace 整合無縫:ToolBarManager + ActionGroup 直接注入。
  • 語義明確:代碼即文檔,維護極易。
  • 輕量級:僅薄封裝 Composite,無效能負擔。

當你的介面符合「頂部操作區 + 底部內容區」模式時,優先選擇 ViewForm。

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