java中如何将JavaFX程序转化为exe可执行文件

日常编程中,我们经常会想写一些小工具程序,作为Java工程师来说首选就是JavaFX,GUI界面太过于老旧,一般我们都不会使用,那么我们如何将写完的JavaFX程序打包成为exe程序呢?那么接下来小编就带领大家如何使用JSmooth软件生成exe程序。

一、编写JavaFX程序

程序代码如下:

import com.tobiasy.file.filestorage.utils.JavaFxUtils;
import com.tobiasy.file.filestorage.utils.StringUtils;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.Window;

/**
 * @author java.xiong
 * @date 2023/3/6
 */
public class FeedbackApplication extends Application {

    private Stage primary;
    private AnchorPane root;
    private VBox vBox;

    public FeedbackApplication() {
    }

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

    @Override
    public void start(Stage primaryStage) {

        primary = primaryStage;
        primary.setResizable(false);
        root = new AnchorPane();
        Scene scene = new Scene(root);
        primary.setTitle("意见反馈");
        JavaFxUtils.icon(primary);

        vBox = new VBox();
        vBox.setPrefWidth(750);
        vBox.setPrefHeight(380);
        vBox.setPadding(new Insets(30, 50, 20, 50));

        Label label = new Label("反馈信息:");
        TextArea textArea = new TextArea();
        textArea.setPromptText("请留下您宝贵的意见");
        textArea.setPrefRowCount(12);
        textArea.setPrefColumnCount(40);
        HBox.setMargin(textArea, new Insets(0, 0, 0, 10));

        HBox hBox = new HBox();
        hBox.getChildren().addAll(label, textArea);
        vBox.getChildren().addAll(hBox);
        root.getChildren().add(vBox);

        Button button = new Button("提交");
        button.setPrefWidth(80);
        button.setPrefHeight(30);
        AnchorPane.setTopAnchor(button, 260.0);
        AnchorPane.setLeftAnchor(button, 550.0);
        root.getChildren().add(button);
        button.setOnMouseClicked(event -> {
            if (StringUtils.isBlank(textArea.getText())) {
                new AlertApplication("反馈信息不能为空!").start(new Stage());
                return;
            }
            // 业务逻辑
            Window window = root.getScene().getWindow();
            Stage st = (Stage) window;
            st.close();
        });

        primary.setScene(scene);
        primary.setWidth(750);
        primary.setHeight(380);
        primary.show();

        root.setOnKeyPressed(event -> {
            if (KeyCode.ESCAPE.equals(event.getCode())) {
                primary.close();
            }
        });
    }
}

二、将JavaFX程序打包

1、配置打包插件

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <!--给jar包起的别名-->
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--添加项目中主类-->
                            <mainClass>com.xxx.FeedbackApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

2、使用maven命令打包

mvn clean package -Dmaven.test.skip=true

3、打包完成

查看target目录下面,如果出现xxx-jar-with-dependencies.jar,则打包成功!

根据程序创建文件夹,比如feedback

将打包完成后的jar包复制到该文件夹

三、下载打包软件JSmooth

1、下载JSmooth软件

点击下载

四、使用JSmooth生成EXE

1、打开JSmooth后,主界面如下:

2、点击左侧骨架

Windows Wrapper选择:Windowed Wrapper,骨架属性勾选第一个:在EXE进程内启动Java应用:

3、选择左侧可执行文件

可配置的二进制文件:输入xxx.exe,即生成的exe文件名,例如我的:feedback.exe

可执行的图标:输入图标文件目录(该目录最好是同级目录或者同级子目录),也可以不设置

4、点击左侧应用程序

主类输入全类名,设置程序参数(没有的话就不用设置),选择嵌入jar包

5、点击左侧JVM选择

1)设置java版本

2)选择jdk目录并复制jre到上面创建的文件夹中,并在JSmooth中设置JVM绑定包为复制后的jre目录,这里主要是需要让程序依附于独立的JRE,而不是每台电脑上都需要安装JRE

6、左侧单击JVM配置

这里若无特殊要求,默认直接跳过

到此可保存当前配置信息到新建文件夹目录,后续可以直接打开即可打包

7、生成exe文件

点击如下图标即可生成exe可执行文件

出现如下提示框,生成成功

返回文件夹目录,此时文件夹中即可看到生成的exe文件:

双击打开试试看:

好啦,今天的课程到此结束,下课!

猜你喜欢

转载自blog.csdn.net/m0_37649480/article/details/129494438