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

SWT/JFace FontRegistry 字體註冊表使用指南與示例

     編輯:Java 相關知識

在 SWT/JFace 應用開發中,FontRegistry 是管理字體資源的核心工具類。它提供字體集中註冊、複用、自動釋放機制,避免重複創建 Font 對象導致的資源洩漏與性能開銷。本文詳解其用法並提供完整示例。

核心優勢

  • 資源複用:同一鍵值返回同一 Font 實例
  • 自動生命週期管理:Display 釋放時自動釋放所有註冊字體
  • 跨平台字體回退:支持 FontData 數組定義字體優先級

完整代碼示例

package swt_jface.demo;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class FontRegistryExample {
    Display display = new Display();
    Shell shell = new Shell(display);
    FontRegistry fontRegistry;

    public FontRegistryExample() {
        init();
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private void init() {
        shell.setLayout(new GridLayout(2, false));

        // 1. 創建 FontRegistry,綁定 Display
        fontRegistry = new FontRegistry(display);

        // 2. 註冊字體:鍵值 + FontData 數組(支持回退)
        fontRegistry.put("button-text", new FontData[]{
            new FontData("Arial", 9, SWT.BOLD)
        });
        fontRegistry.put("code", new FontData[]{
            new FontData("Courier New", 10, SWT.NORMAL)
        });

        // 3. 使用字體
        Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
        text.setFont(fontRegistry.get("code"));  // 獲取字體實例
        text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
        text.setText("public static void main() {\n\tSystem.out.println(\"Hello\");\n}");

        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = 2;
        text.setLayoutData(gd);

        Button executeButton = new Button(shell, SWT.PUSH);
        executeButton.setText("Execute");
        executeButton.setFont(fontRegistry.get("button-text"));

        Button cancelButton = new Button(shell, SWT.PUSH);
        cancelButton.setText("Cancel");
        cancelButton.setFont(fontRegistry.get("button-text"));
    }

    public static void main(String[] args) {
        new FontRegistryExample();
    }
}

關鍵點解析

1. FontData 數組機制

// 系統會按順序嘗試,找到第一個可用字體
new FontData[] {
    new FontData("PingFang SC", 12, SWT.NORMAL),  // macOS 首選
    new FontData("Microsoft YaHei", 12, SWT.NORMAL), // Windows 首選
    new FontData("Sans", 12, SWT.NORMAL)           // Linux/兜底
}

2. 字體樣式常量

  • SWT.NORMAL:常規
  • SWT.BOLD:粗體
  • SWT.ITALIC:斜體
  • 可組合:SWT.BOLD | SWT.ITALIC

3. 最佳實踐建議

  1. 應用啟動時統一註冊:避免分散在各控件創建處
  2. 語義化命名鍵值:如 header-font、code-font、button-font
  3. 配合 JFace ResourceManager:JFaceResources.getFontRegistry() 獲取全局單例
  4. 避免手動創建 Font:除非極少數一次性使用場景

常見問題

Q: 字體顯示不生效?
A: 檢查系統是否安裝該字體,FontData 數組需提供回退字體。
Q: 內存洩漏?
A: FontRegistry 隨 Display 生命週期自動釋放,無需手動 dispose。
Q: 動態切換字體?
A: 重新 put 同鍵值新 FontData,後續 get() 返回新實例。

FontRegistry 是 SWT/JFace 資源管理的基石組件,善用它能讓桌面應用的字體管理更優雅、高效、穩定。

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