java的GUI之SWT框架 JavaFX框架 配置開發環境(包含但不限於WindowBuilder完整教程,解決Unknown GUI toolkit報錯,解決導入SWT包錯誤)


官網(資料挺多的,API文檔截圖以及示例都有):https://www.eclipse.org/swt/

克隆官方倉庫

git clone --depth=1 git://git.eclipse.org/gitroot/platform/eclipse.platform.swt.git

 

  里面只有 org.eclipse.swt.snippets 目錄里面有 300多個代碼片段,初學直接看這部分源碼即可。

  其他 example 目錄是成品小 demo,學完snippet再看,然后就能做東西了

 

本次環境:

Eclipse IDE for Java Developers

Version: 2018-12 (4.10.0)

Build id: 20181214-0600

JAVA:JDK11 

Windows10 Pro 預覽版 18334——19H1

 

去網上找了配置SWT開發環境的,發現很多教程都過時了。SWT是一個利用Java的JNI對接原生系統的框架

(做到了系統原生里有的GUi Api 就盡量用系統的,沒有的就繪制,大大提高了運行效率,是真正可以考慮的貼合系統的GUI,不過理論上移植性比Swing Awt差點)

 

 

被GFW和Dns污染光環之下的請換源(享受秒開的喜悅):https://lug.ustc.edu.cn/wiki/mirrors/help/eclipse#使用科大鏡像更新插件

 一,Eclipse里安裝WindowBuilder來可視化設計SWT程序的完整教程

(完美解決Unknown GUI toolkit或無法導入swt包的錯誤報錯):

如果僅僅是想在Eclipse里使用WindowBuilder來可視化設計SWT程序,可以參考按這里做eclipse安裝WindowBuilder插件以及簡單使用(近更) - Akatsuki - CSDN博客 (如果last good build不行可以選第一個Lastest)  

注意:上面這教程中的

“然后進入到工程,右鍵src->new->other->WindowBuilder->Swing Designer->Application Window->next->起名->Finish”

應改為:

“然后進入到工程,右鍵src->new->Packages,(必須勾上Create package-info.java),再右擊src->other->WindowBuilder->Swing Designer->SWT->Application Window->next->起名->Finish”

 

然后如果design界面還是用不了,提示Unknown GUI toolkit    The parser parsed the compilation unit, but can't identifyany GUI toolkit,   If you wish to use SWT,please add the appropriate SWT jars to your classpath, or create a new SWT/JF

點擊Switch to code切換到代碼區

然后就如下圖操作(點擊代碼區import所在行號欄里的×號):

點擊+號展開import,再雙擊 Add 'requires org.*' to module-info.java

 

 

 

然后打開同工程里的module-info.java,鼠標懸浮在requires后面的org.eclipse.swt.win32.win32.x86_64上,然后雙擊Move classpath entry 'org.eclipse.swt.win32*',

如果有  requires swt; 那一行就刪掉(因為我截圖里寫錯了又懶得改)

為什么需要在module-info.java里添加requires?這是Java9引入的新特性,至於作用和用法,我還在思考....

 

 

 

 然后SWT報錯少了一些,嗯,快成功了。

先下載swt.jar並解壓到任意一個目錄,下載請看這里:下載SWT

導入SWT的ClassPath

在Eclipse里添加額外的Class Folder,選擇解壓好的目錄(即swt.jar所在目錄)    

 

然后再添加DLL支持,具體操作實例:

在Eclipse安裝目錄下的plugins目錄下,找到文件org.eclipse.swt.win32.win32.x86_64_3.109.0.v20181204-1801.jar(x86_64后面的是版本號,你我的可能版本略微不同),復制出來並解壓到某一目錄(該jar文件里面有4個dll文件)

然后在Eclipse包資源管理器中,右擊項目名 → 導入 → 常規 → 文件系統 → 下一步 → 瀏覽 並選擇dll文件所在目錄,勾選4個dll文件確認即可。

 

發現包資源管理器都沒紅×了吧!那就是成功了

最后點Main.java(Eclipse生成的swt程序示例),然后切換design界面,點Reparse重新加載可視化界面即可

 

 

 

 

最后效果:

 

 

 

測試添加組件(一切正常)

附上包資源一欄截圖:

 

 

 

 

 


 

 

    

 

 二,在非Eclipse里配置SWT開發環境,例如VSCode

1,下載SWT:

進入https://www.eclipse.org/swt/

在Lastest Release里點擊你Eclipse對應的版本號。

 

 

進入后你會發現這里有你選擇的Eclipse版本所對應的全部SDK及其運行環境(如下圖),

往下翻找到SWT Binary and Source,點擊swt-<Eclipse版本號>-win32-win32-x86_64.zip下載即可

下載完成。

 

寫好HelloWorld 並按照以下目錄結構放置

+ lib

  - swt.jar

+ src

  - HelloWorld.java

 1 package org.yu;
 2 
 3 /**
 4  * Hello world!
 5  *
 6  */
 7 
 8 import org.eclipse.swt.widgets.Display;
 9 import org.eclipse.swt.widgets.Shell;
10 import org.eclipse.swt.SWT;
11 
12 public class App {
13     public static void main(String[] args){
14         Display aDisplay = new Display();
15         Shell shell = new Shell(aDisplay,SWT.);
16         shell.setLocation(200, 200);
17         shell.setSize(400, 300);
18         shell.setText("Title is Mine");
19         shell.open();
20 
21         while (!shell.isDisposed())         // 窗體是否關閉
22         {
23             if (!aDisplay.readAndDispatch())         // 檢驗 Display 線程狀態是否忙
24                 aDisplay.sleep();            // Display 類線程休眠
25         }
26 
27         aDisplay.dispose();                 // 注銷 Display 對象資源
28     }
29 }
View Code

 

測試一下

javac -classpath "../lib/swt.jar" HelloWorld.java 

 

2,配置SWT環境(以VSCode為例)

配好JDK環境變量,然后按照VSCode官方說明安裝好Java插件,然后有幾種方法:

 

一:把 swt.jar 加入CLASSPATH變量如 E:\japi\swt.jar,然后在VSCode里設置,特別麻煩....(但是可以用到最新版4.924)

二:當然我更推薦用Maven(記得換阿里源,不然牆體太厚)
F1 -> Create Maven Project -> 選Quick Start 那一個,版本最新,剩下的就和maven命令行確認一樣了
然后再按Github倉庫說的添加到pom.xml,保存,下面Maven插件提示,點Now導入即可
Maven 很容易用,例如最簡單的: https://www.runoob.com/maven/maven-tutorial.html
以及會用到的: maven集中定義版本號pom
可以參考我的pom.xml
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>org.yu</groupId>
  8   <artifactId>demoSwt</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10 
 11 
 12 
 13   <name>demoSwt</name>
 14   <!-- FIXME change it to the project's website -->
 15   <url>http://www.example.com</url>
 16 
 17 <repositories>
 18     <repository>
 19         <id>maven-eclipse-repo</id>
 20         <url>http://maven-eclipse.github.io/maven</url>
 21     </repository>
 22 </repositories>
 23 
 24   <properties>
 25     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 26     <maven.compiler.source>1.8</maven.compiler.source>
 27     <maven.compiler.target>1.8</maven.compiler.target>
 28     <swt.version>4.6.1</swt.version>
 29   </properties>
 30 
 31   <dependencies>
 32     <dependency>
 33       <groupId>junit</groupId>
 34       <artifactId>junit</artifactId>
 35       <version>4.11</version>
 36       <scope>test</scope>
 37     </dependency>
 38     <!-- <dependency>
 39       <groupId>org.eclipse.swt</groupId>
 40       <artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
 41       <version>${swt.version}</version>
 42       - To use the debug jar, add this -
 43       <classifier>debug</classifier>
 44     </dependency> -->
 45     <!-- <dependency>
 46       <groupId>org.eclipse.swt</groupId>
 47       <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
 48       <version>${swt.version}</version>
 49     </dependency> -->
 50     <!-- <dependency>
 51       <groupId>org.eclipse.swt</groupId>
 52       <artifactId>org.eclipse.swt.gtk.linux.x86</artifactId>
 53       <version>${swt.version}</version>
 54       <classifier>debug</classifier>
 55     </dependency> -->
 56     <dependency>
 57       <groupId>org.eclipse.swt</groupId>
 58       <artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
 59       <version>${swt.version}</version>
 60       <classifier>debug</classifier>
 61     </dependency>
 62     <!-- <dependency>
 63       <groupId>org.eclipse.swt</groupId>
 64       <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
 65       <version>${swt.version}</version>
 66     </dependency> -->
 67 </dependencies>
 68 
 69   <build>
 70     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 71       <plugins>
 72         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 73         <plugin>
 74           <artifactId>maven-clean-plugin</artifactId>
 75           <version>3.1.0</version>
 76         </plugin>
 77         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 78         <plugin>
 79           <artifactId>maven-resources-plugin</artifactId>
 80           <version>3.0.2</version>
 81         </plugin>
 82         <plugin>
 83           <artifactId>maven-compiler-plugin</artifactId>
 84           <version>3.8.0</version>
 85         </plugin>
 86         <plugin>
 87           <artifactId>maven-surefire-plugin</artifactId>
 88           <version>2.22.1</version>
 89         </plugin>
 90         <plugin>
 91           <artifactId>maven-jar-plugin</artifactId>
 92           <version>3.0.2</version>
 93         </plugin>
 94         <plugin>
 95           <artifactId>maven-install-plugin</artifactId>
 96           <version>2.5.2</version>
 97         </plugin>
 98         <plugin>
 99           <artifactId>maven-deploy-plugin</artifactId>
100           <version>2.8.2</version>
101         </plugin>
102         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
103         <plugin>
104           <artifactId>maven-site-plugin</artifactId>
105           <version>3.7.1</version>
106         </plugin>
107         <plugin>
108           <artifactId>maven-project-info-reports-plugin</artifactId>
109           <version>3.0.0</version>
110         </plugin>
111       </plugins>
112     </pluginManagement>
113   </build>
114 </project>
View Code

 

附上Maven換源方法:

 1 ~/.m2$ cat settings.xml 
 2 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
 5                           https://maven.apache.org/xsd/settings-1.0.0.xsd">
 6 
 7       <mirrors>
 8         <mirror>  
 9             <id>alimaven</id>  
10             <name>aliyun maven</name>  
11             <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
12             <mirrorOf>central</mirrorOf>          
13         </mirror>  
14       </mirrors>
15 </settings>
View Code

 

Maven 方式只在 Linux 下測試通過,Windows 可能還需用把dll文件放到class生成目錄里(可能),MacOSX(買不起....)

 但是Maven私人倉庫的已經停更了,懂的可以自己做個私人倉庫更新到最新版,然后按照原作者那樣放 github 就行(不用服務器)

 

 

 

效果圖(由於SWT的原理,所以皮膚會和系統一致)

 沒有關閉按鈕:只是因為用了SWT.NO這個常量

 

Ps.如果提示:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
那么就把32位庫拿開,只用64位庫,用Maven的只需改一下pom.xml

 

最后放個快速入門資料(在兩個qq群找到的,非常感謝他們,侵權請聯系):

鏈接: https://pan.baidu.com/s/1zid8jDgl4wQFSle2OD3lcg

提取碼: dbw5

當然在 百度文檔搜 SWT 也是能找到不少資料的(想不到吧hh)

引用一句話:JFace與SWT的關系好比Microsoft的MFC與SDK的關系

 

 

 


 

最后,現在建議使用 JavaFX,官方支持的,參照了 SWT 調用系統底層的方式,這樣保證穩定性和樣式與系統的一致性
學過JAVA ,想學GUI。網友說swing awt被淘汰了。請教現在主流的JAVA gui開發學啥?- 知乎

 

 

JavaFX,更先進的圖形庫

中文准官網 https://openjfx.cn  其主頁的文檔收集的比官網 openjfx.io 好很多

以及 Maven 庫

 

JavaFx的lib如下,可見包含了SWT Swing Fxml等優點,JDK8自帶,而從JDK9開始JavaFX獨立了出來(可能是考慮到全球桌面軟件市場萎縮的原因)

 

 JavaFX資料

https://www.jetbrains.com/help/idea/javafx.html#

https://openjfx.io/openjfx-docs/#IDE-Intellij

javafx自動縮放例子 按照淘寶自適應框架方法便編寫-Main.java 公式(DPR = 物理像素 寬/高 ,  fontsize/= DPR)

可以用Maven或Gradle來管理依賴,這是我 javaFx 項目里的 pom.xml (最重要的是兩個 JavaFX 的依賴,以及一個JavaFX的maven plugin)

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>com.demo</groupId>
  8     <artifactId>BookManage</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10 
 11     <name>BookManage</name>
 12     <!-- FIXME change it to the project's website -->
 13     <url>http://www.example.com</url>
 14 
 15 
 16     <properties>
 17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18         <maven.compiler.source>11</maven.compiler.source>
 19         <maven.compiler.target>11</maven.compiler.target>
 20         <fx.version>11</fx.version>
 21     </properties>
 22     <dependencies>
 23 <!--        JavaFX-->
 24         <dependency>
 25             <groupId>org.openjfx</groupId>
 26             <artifactId>javafx-controls</artifactId>
 27             <version>${fx.version}</version>
 28         </dependency>
 29         <dependency>
 30             <groupId>org.openjfx</groupId>
 31             <artifactId>javafx-fxml</artifactId>
 32             <version>${fx.version}</version>
 33         </dependency>
 34 
 35 
 36         <dependency>
 37             <groupId>junit</groupId>
 38             <artifactId>junit</artifactId>
 39             <version>4.12</version>
 40             <scope>test</scope>
 41         </dependency>
 42 
 43         <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
 44         <dependency>
 45             <groupId>org.projectlombok</groupId>
 46             <artifactId>lombok</artifactId>
 47             <version>1.18.12</version>
 48             <scope>provided</scope>
 49         </dependency>
 50 
 51 
 52     </dependencies>
 53 
 54     <build>
 55         <resources>
 56             <resource>
 57                 <directory>src/main/java</directory>
 58                 <includes>
 59                     <include>**/*.*</include>
 60                     <include>**/*.yml</include>
 61                     <include>**/*.properties</include>
 62                     <include>**/*.css</include>
 63                     <include>**/*.fxml</include>
 64                 </includes>
 65                 <filtering>false</filtering>
 66             </resource>
 67             <resource>
 68                 <directory>src/main/resources</directory>
 69                 <includes>
 70                     <include>**/*.*</include>
 71                     <include>**/*.yml</include>
 72                     <include>**/*.properties</include>
 73                     <include>**/*.css</include>
 74                     <include>**/*.fxml</include>
 75                 </includes>
 76                 <filtering>false</filtering>
 77             </resource>
 78         </resources>
 79 
 80         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 81             <plugins>
 82 <!--                javaFX JDK11+ needed-->
 83                 <plugin>
 84                     <groupId>org.openjfx</groupId>
 85                     <artifactId>javafx-maven-plugin</artifactId>
 86                     <version>0.0.4</version>
 87                     <configuration>
 88                         <mainClass>com.demo.App</mainClass>
 89                     </configuration>
 90                 </plugin>
 91 
 92 
 93                 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 94                 <plugin>
 95                     <artifactId>maven-clean-plugin</artifactId>
 96                     <version>3.1.0</version>
 97                 </plugin>
 98                 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 99                 <plugin>
100                     <artifactId>maven-resources-plugin</artifactId>
101                     <version>3.0.2</version>
102                 </plugin>
103                 <plugin>
104                     <artifactId>maven-compiler-plugin</artifactId>
105                     <version>3.8.0</version>
106                     <configuration>
107                         <source>11</source>
108                         <target>11</target>
109                     </configuration>
110                 </plugin>
111                 <plugin>
112                     <artifactId>maven-surefire-plugin</artifactId>
113                     <version>2.22.1</version>
114                 </plugin>
115                 <plugin>
116                     <artifactId>maven-jar-plugin</artifactId>
117                     <version>3.0.2</version>
118                 </plugin>
119                 <plugin>
120                     <artifactId>maven-install-plugin</artifactId>
121                     <version>2.5.2</version>
122                 </plugin>
123                 <plugin>
124                     <artifactId>maven-deploy-plugin</artifactId>
125                     <version>2.8.2</version>
126                 </plugin>
127                 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
128                 <plugin>
129                     <artifactId>maven-site-plugin</artifactId>
130                     <version>3.7.1</version>
131                 </plugin>
132                 <plugin>
133                     <artifactId>maven-project-info-reports-plugin</artifactId>
134                     <version>3.0.0</version>
135                 </plugin>
136             </plugins>
137         </pluginManagement>
138     </build>
139 </project>
pom.xml

然后在配置IDEA的運行程序的configuration,添加Maven選項,然后填入參數 javafx:run 即可(這個javafx:run 就是上面pom里的plugin)

 JDK14+JAVAFX14+Maven定制jre打包瘦身,必成版

按照此貼打包出完整jar -> 在JRE上試運行 -> 精簡jre,然后用exe4j把 [jar] 打成exe ,再用innosetup把 [exe和JRE以及資源文件] 合成一個新的exe

如果是JDK9+,可以使用命令 

教程作者說打包后,exe所在路徑就是Java 的當前項目路徑

其中,用於打包完整依賴的Maven插件建議用 shade,而不是作者用的那個 assembly

插件示例配置如下 

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.demo.AppLauncher</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </plugin>
pom.xml中的打包插件示例

調用該打包插件的命令:

mvn package shade:shade

 

 

在 IDEA 里配置 Java SceneBuilder

 

添加以下代碼到start()

//        for print version
        System.out.println(System.getProperty("java.version") +" and "+ System.getProperty("javafx.version")); // JBR = OPENJDK11.0.5 + 10.0.2-internal JavaFx
        //--module-path "D:\Program Files\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml
        // Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 10.0.2-internal

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM