方便开发,添加一些开发工具依赖,比如 lombok
在src/main/java/包名下有件菜单,选New->Swing UI Designer ->GUI Form
添加一个JLabel,并编写Hello Swing UI
完成后查看Hello.java内容为:
package com.xqlee.project;
import javax.swing.*;
public class Hello {
private JPanel panel1;
}
添加一个@Getter注解方便后续获取内部组件
import lombok.Getter;
import javax.swing.*;
@Getter
public class Hello {
private JPanel panel1;
}
App.java 内容
import javax.swing.*;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
// System.out.println( "Hello World!" );
JFrame frame = new JFrame();
frame.setContentPane(new Hello().getPanel1());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();//最迷你窗口展示,会导致setSize失效,setBounds失效
}
}
frame.pack();
//最迷你窗口展示,会导致setSize失效,setBounds失效
右键运行:
Swing UI IDEA 可视化设计入门完毕。
//...忽略其他...
//居中显示
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
int frameWidth = 800;
int frameHeight = 400;
// frame.setSize(frameWidth, frameHeight);
frame.setBounds(screenWidth/2-(frameWidth/2), screenHeight/2-(frameHeight/2), frameWidth, frameHeight);
//...忽略其他...
上面的基础运行是没啥问题,但是直接打包后运行打包文件可能出现空指针。原因是GUI Form依赖idea的相关类,但是在build的时候,并没有生成相关代码。
CTRL+ALT+S 打开IDEA的设置,搜索GUI Designer 可以看到下图
修改为:
两种输出方式,
第一种是编译成运行时的class文件,自己写的代码构建成class文件与IDEA之间的关联被忽略了,也就是说是由IDEA自己维护的;
第二种情况是将依赖的IDEA的相关类直接在build的时候生成到源码中。
默认第一种,修改成第二种。
特别注意:idea构建是菜单里面的Build里面的Build Project 不是maven的package或者compile
构建后会在form关联的Java类生成一些代码,参考如下
如果用了idea的layout
用了就要添加下面的依赖:
<dependency>
<groupId>com.github.adedayo.intellij.sdk</groupId>
<artifactId>forms_rt</artifactId>
<version>142.1</version>
</dependency>
在maven配置文件的build>plugins下添加一个插件依赖
<plugin>
<groupId>io.github.janix520</groupId>
<artifactId>maven-easypackage-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jpackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 是否是最小打包,用于精简虚拟机,有的库依赖老旧的库,解析依赖会出错,true如果打包不成功,就改成false -->
<minimum>true</minimum>
<!--应用程序名称-->
<name>${project.artifactId}</name>
<!--主运行类-->
<mainClass>com.secondsearch.SecondSearchApplication</mainClass>
<!--是否显示控制台-->
<winConsole>false</winConsole>
<!--应用程序图标-->
<icon>${project.basedir}/src/main/resources/icon/icon.ico</icon>
<!--可选app-image、exe、msi、rpm、deb、pkg、dmg,msi需要另外一个程序配合,app-image是exe绿色版,exe是安装包,其他自行搜索-->
<type>app-image</type>
<appVersion>1.0.0</appVersion>
<copyright>版权</copyright>
<vendor>厂商</vendor>
<description>描述</description>
<!-- 是否递归分析依赖,一般false就可以,改成true,增强打包兼容性,不过打包会变慢,不填此参数,默认false -->
<!--<recursive>false</recursive>-->
<!--<jarName>${project.build.finalName}.jar</jarName>-->
<!--jvm option-->
<!--<javaOptions>-Dserver.port=8888 -Djava.awt.headless=false</javaOptions>-->
<!--jar包生成目录,对应上面两个输出的libs-->
<!--<libs>libs</libs>-->
</configuration>
</plugin>
添加后构建项目则是
搞定
插件来源:GitHub - Janix520/EasyPackage: java swing javafx gui package exe dmg maven
提示:该插件需要jdk17支持
exe
msi
打包exe
msi
格式需要WiX环境,下载:Release WiX Toolset v3.14.1 · wixtoolset/wix3 · GitHub 【提示:其他高版本不知道怎么玩哈试过失败了。目前只有3.x成功了】
点中间的Install
安装成功
再次把类型设置为exe即可编译成功(环境Windows11)
先使用EasyPackage打app-image包(也就是打包城exe的绿化包),再通过Inno Setup工具进行封包。可自定义配置封包界面。
Inno Setup 封包使用教程参考:Inno Setup 封包exe工具安装-XQLEE'Blog
着重检查你pom.xml里面的javaOptions参数配置,有问题就会出现idea能运行,打包后jar可运行但是exe闪退问题。
https://blog.xqlee.com/article/250226135323015.html