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

SWT/JFace 入門指南:從零搭建開發環境到 HelloWorld

     編輯:Java 相關知識

本文手把手教你在 Eclipse 中搭建 SWT/JFace 開發環境,並跑通第一個 HelloWorld 程式,包含常見坑點排查。

三步驟完成環境搭建

1. 建立 Java 專案

File → New → Java Project,任意專案名稱。

2. 導入核心 JAR 包

從 Eclipse 安裝目錄 plugins/ 找到並加入 Build Path:

  • org.eclipse.swt_3.x.x.jar
  • org.eclipse.jface_3.x.x.jar
  • org.eclipse.core.runtime_3.x.x.jar
  • org.eclipse.ui.workbench_3.x.x.jar

Build Path 配置

3. 配置本地庫(Windows 必做)

SWT 透過 JNI 呼叫 OS 原生 API,需載入 swt-win32-*.dll。三種方式任選一:

  1. 最簡單:將 plugins/org.eclipse.swt.win32_3.x.x.jar 解壓,把 os/win32/x86/ 下所有 .dll 複製到 JRE/bin/ 目錄。
  2. 將 DLL 所在目錄加入系統環境變數 PATH。
  3. Run Configurations → Arguments → VM Arguments 添加:-Djava.library.path=你的DLL目錄。

VM Arguments 配置

HelloWorld 完整代碼

package swtjfacesample;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class HelloSwt {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        Text helloText = new Text(shell, SWT.CENTER);
        helloText.setText("Hello, World!");
        helloText.pack();

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

執行與驗證

右鍵類 → Run As → Java Application。正常彈出視窗顯示「Hello, World!」即成功。

執行結果

常見錯誤排查 Q&A

現象原因與對策
編譯錯誤:找不到 Display、Text 等類核心 JAR 未正確加入 Build Path,檢查第 2 步
運行拋出 UnsatisfiedLinkError: no swt-win32-xxxx in java.library.pathDLL 未正確載入,按第 3 步重新配置

環境就緒,後續課程將直接聚焦 API 實戰,不再重複配置步驟。

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