一、简介

√ \color{#FF7D00}{√}  用 Java 语言开发桌面应用,官网

√ \color{#FF7D00}{√}  JDK8 中自带 JavaFX 开发包,JDK11 需要手动导入

二、创建一个基础应用

  1. GUI
public class LoginWindow extends Application {

    public void start(Stage primaryStage) throws IOException {
        Parent parent = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("ui/login.fxml")));
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.setTitle("测试title");
        primaryStage.setHeight(350);
        primaryStage.setWidth(425);
        // 窗体缩放
        primaryStage.setResizable(true);
        // 关闭窗体,正常退出jvm
//        primaryStage.setOnCloseRequest(event -> {
//            System.exit(0);
//        });
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  1. ui
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>

<!--- 登录页面 -->

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100" prefWidth="100" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zilong.fx.controller.TestController">

</Pane>
  1. Controller
public class TestController {
}
  1. 运行效果
    在这里插入图片描述

三、打包成exe文件(需要go环境)

  1. 修改启动类
@SpringBootApplication
public class FxApplication extends Application implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(FxApplication.class);
        application.setHeadless(false);
        application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Platform.runLater(() -> {
            try {
                new LoginWindow().start(new Stage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
  1. maven插件
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>go</executable>
                <workingDirectory>./packaging</workingDirectory>
                <arguments>
                    <argument>build</argument>
                    <argument>-o</argument>
                    <argument>./distribution/${project.build.finalName}.exe</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. go脚本
package main

import (
	"github.com/gonutz/ide/w32"
	"os/exec"
)

func main() {
	HideConsole()
	cmd := exec.Command("./jre/bin/java.exe", "-jar", "F:\\java\\projects\\fx\\target\\fx-0.0.1-SNAPSHOT.jar")
	buf, _ := cmd.Output()
	println(buf)
}

func HideConsole() {
	ShowConsoleAsync(w32.SW_HIDE)
}

func ShowConsole() {
    ShowConsoleAsync(w32.SW_SHOW)
}

func ShowConsoleAsync(commandShow uintptr) {
	console := w32.GetConsoleWindow()
	if console != 0 {
		_, consoleProcID := w32.GetWindowThreadProcessId(console)
		if w32.GetCurrentProcessId() == consoleProcID {
			w32.ShowWindowAsync(console, commandShow)
		}
	}
}
  1. install 后生成 .exe 文件,双击运行

代 码 地 址 : \color{red}{代码地址:} https://gitee.com/zilong123666/fx.git

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐