java的GUI之SWT框架 JavaFX框架 配置开发环境(包含但不限于WindowBuilder完整教程,解决Unknown GUI toolkit报错,解决导入SWT包错误)
官网(资料挺多的,API文档截图以及示例都有):https://www.eclipse.org/swt/
克隆官方仓库
git clone --depth=1 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
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 }测试一下
javac -classpath "../lib/swt.jar" HelloWorld.java
2,配置SWT环境(以VSCode为例)
配好JDK环境变量,然后按照VSCode官方说明安装好Java插件,然后有几种方法:
一:把 swt.jar 加入CLASSPATH变量如 E:\japi\swt.jar,然后在VSCode里设置,特别麻烦....(但是可以用到最新版4.924)
二:当然我更推荐用Maven(记得换阿里源,不然墙体太厚) https://github.com/maven-eclipse/maven-eclipse.github.io F1 -> Create Maven Project -> 选Quick Start 那一个,版本最新,剩下的就和maven命令行确认一样了 然后再按Github仓库说的添加到pom.xml,保存,下面Maven插件提示,点Now导入即可 Maven 很容易用,例如最简单的:https://www.runoob.com/maven/maven-tutorial.html 以及会用到的:maven集中定义版本号pom 可以参考我的pom.xml1 <?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.0modelVersion> 6 7 <groupId>org.yugroupId> 8 <artifactId>demoSwtartifactId> 9 <version>1.0-SNAPSHOTversion> 10 11 12 13 <name>demoSwtname> 14 15 <url>http://www.example.comurl> 16 17 <repositories> 18 <repository> 19 <id>maven-eclipse-repoid> 20 <url>http://maven-eclipse.github.io/mavenurl> 21 repository> 22 repositories> 23 24 <properties> 25 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 26 <maven.compiler.source>1.8maven.compiler.source> 27 <maven.compiler.target>1.8maven.compiler.target> 28 <swt.version>4.6.1swt.version> 29 properties> 30 31 <dependencies> 32 <dependency> 33 <groupId>junitgroupId> 34 <artifactId>junitartifactId> 35 <version>4.11version> 36 <scope>testscope> 37 dependency> 38 45 50 56 <dependency> 57 <groupId>org.eclipse.swtgroupId> 58 <artifactId>org.eclipse.swt.gtk.linux.x86_64artifactId> 59 <version>${swt.version}version> 60 <classifier>debugclassifier> 61 dependency> 62 67 dependencies> 68 69 <build> 70 <pluginManagement> 71 <plugins> 72 73 <plugin> 74 <artifactId>maven-clean-pluginartifactId> 75 <version>3.1.0version> 76 plugin> 77 78 <plugin> 79 <artifactId>maven-resources-pluginartifactId> 80 <version>3.0.2version> 81 plugin> 82 <plugin> 83 <artifactId>maven-compiler-pluginartifactId> 84 <version>3.8.0version> 85 plugin> 86 <plugin> 87 <artifactId>maven-surefire-pluginartifactId> 88 <version>2.22.1version> 89 plugin> 90 <plugin> 91 <artifactId>maven-jar-pluginartifactId> 92 <version>3.0.2version> 93 plugin> 94 <plugin> 95 <artifactId>maven-install-pluginartifactId> 96 <version>2.5.2version> 97 plugin> 98 <plugin> 99 <artifactId>maven-deploy-pluginartifactId> 100 <version>2.8.2version> 101 plugin> 102 103 <plugin> 104 <artifactId>maven-site-pluginartifactId> 105 <version>3.7.1version> 106 plugin> 107 <plugin> 108 <artifactId>maven-project-info-reports-pluginartifactId> 109 <version>3.0.0version> 110 plugin> 111 plugins> 112 pluginManagement> 113 build> 114 project>附上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>alimavenid> 10 <name>aliyun mavenname> 11 <url>http://maven.aliyun.com/nexus/content/groups/public/url> 12 <mirrorOf>centralmirrorOf> 13 mirror> 14 mirrors> 15 settings>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.0modelVersion> 6 7 <groupId>com.demogroupId> 8 <artifactId>BookManageartifactId> 9 <version>1.0-SNAPSHOTversion> 10 11 <name>BookManagename> 12 13 <url>http://www.example.comurl> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 18 <maven.compiler.source>11maven.compiler.source> 19 <maven.compiler.target>11maven.compiler.target> 20 <fx.version>11fx.version> 21 properties> 22 <dependencies> 23 24 <dependency> 25 <groupId>org.openjfxgroupId> 26 <artifactId>javafx-controlsartifactId> 27 <version>${fx.version}version> 28 dependency> 29 <dependency> 30 <groupId>org.openjfxgroupId> 31 <artifactId>javafx-fxmlartifactId> 32 <version>${fx.version}version> 33 dependency> 34 35 36 <dependency> 37 <groupId>junitgroupId> 38 <artifactId>junitartifactId> 39 <version>4.12version> 40 <scope>testscope> 41 dependency> 42 43 44 <dependency> 45 <groupId>org.projectlombokgroupId> 46 <artifactId>lombokartifactId> 47 <version>1.18.12version> 48 <scope>providedscope> 49 dependency> 50 51 52 dependencies> 53 54 <build> 55 <resources> 56 <resource> 57 <directory>src/main/javadirectory> 58 <includes> 59 <include>**/*.*include> 60 <include>**/*.ymlinclude> 61 <include>**/*.propertiesinclude> 62 <include>**/*.cssinclude> 63 <include>**/*.fxmlinclude> 64 includes> 65 <filtering>falsefiltering> 66 resource> 67 <resource> 68 <directory>src/main/resourcesdirectory> 69 <includes> 70 <include>**/*.*include> 71 <include>**/*.ymlinclude> 72 <include>**/*.propertiesinclude> 73 <include>**/*.cssinclude> 74 <include>**/*.fxmlinclude> 75 includes> 76 <filtering>falsefiltering> 77 resource> 78 resources> 79 80 <pluginManagement> 81 <plugins> 82 83 <plugin> 84 <groupId>org.openjfxgroupId> 85 <artifactId>javafx-maven-pluginartifactId> 86 <version>0.0.4version> 87 <configuration> 88 <mainClass>com.demo.AppmainClass> 89 configuration> 90 plugin> 91 92 93 94 <plugin> 95 <artifactId>maven-clean-pluginartifactId> 96 <version>3.1.0version> 97 plugin> 98 99 <plugin> 100 <artifactId>maven-resources-pluginartifactId> 101 <version>3.0.2version> 102 plugin> 103 <plugin> 104 <artifactId>maven-compiler-pluginartifactId> 105 <version>3.8.0version> 106 <configuration> 107 <source>11source> 108 <target>11target> 109 configuration> 110 plugin> 111 <plugin> 112 <artifactId>maven-surefire-pluginartifactId> 113 <version>2.22.1version> 114 plugin> 115 <plugin> 116 <artifactId>maven-jar-pluginartifactId> 117 <version>3.0.2version> 118 plugin> 119 <plugin> 120 <artifactId>maven-install-pluginartifactId> 121 <version>2.5.2version> 122 plugin> 123 <plugin> 124 <artifactId>maven-deploy-pluginartifactId> 125 <version>2.8.2version> 126 plugin> 127 128 <plugin> 129 <artifactId>maven-site-pluginartifactId> 130 <version>3.7.1version> 131 plugin> 132 <plugin> 133 <artifactId>maven-project-info-reports-pluginartifactId> 134 <version>3.0.0version> 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包的依赖可以用命令
jdeps --list-deps *.jar效果图
按照此贴打包出完整jar -> 在JRE上试运行 -> 精简jre,然后用exe4j把 [jar] 打成exe ,再用innosetup把 [exe和JRE以及资源文件] 合成一个新的exe
如果是JDK9+,可以使用命令
教程作者说打包后,exe所在路径就是Java 的当前项目路径
其中,用于打包完整依赖的Maven插件建议用 shade,而不是作者用的那个 assembly
插件示例配置如下
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-shade-pluginartifactId> <version>3.2.1version> <executions> <execution> <phase>packagephase> <goals> <goal>shadegoal> goals> execution> executions> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.demo.AppLaunchermainClass> 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