前言
很多時候,我總會有一些莫名其妙的想法,但是光會后端就只能通過main函數(shù)調用來實現(xiàn)我的想法,這種實踐手法就有點low,后來我學會了vue-cli構建前端工程,但這個工作量有點大,僅適合閑時較多的時候去做。這個時候我撿起了以前用過的javafx,它和swing其實也差不多,但是更方便,再加上以前我做過C#開發(fā),有很多東西是互通的,減少了很多學習成本。
步驟說明
因為springboot的使用比spring方便太多了,這里我采用的基于springboot來構建javafx的工程,然后我采用的編譯器是idea
步驟1
首先我們創(chuàng)建一個springboot項目(這個過程就跳過了,這個教程百度上很多的),然后創(chuàng)建后面要放代碼的文件夾,方便后續(xù)代碼管理,如下所示。

image.png
- constant文件夾用于存放靜態(tài)變量
- controller用于存放頁面控制器代碼
- model用于對象類
- template這個是我這個項目中用
- utils用于存放工具類
- view用于存放自定義的展示頁面控件
- css用于存放靜態(tài)的css文件,css是用來美化展示頁面的,一個好看的頁面對于一個桌面應用來說尤為重要
- image用于存放靜態(tài)圖片資源
- view用于存放fxml,相當于html,這里可以配置展示頁面的各個空間的位置,大小,顏色什么的
步驟2
然后引入需要依賴的jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wcf.test</groupId>
<artifactId>test-pest</artifactId>
<version>1.0</version>
<name>test-pest</name>
<description>I'm afraid of test the performance of my code</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 這個必須要引入,是用來讓javafx支持springboot的,必須要引入
且這個jar的版本和springboot版本必須要對應,否則程序會報錯 -->
<dependency>
<groupId>de.roskenet</groupId>
<artifactId>springboot-javafx-support</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
步驟3
改造啟動類,如下所示
這里說明一下,因為我需要用戶手動配置數(shù)據庫連接信息,所以我把DataSourceAutoConfiguration給移除了,不然會報錯。
@SpringBootApplication(scanBasePackages = {"com.wcf.test.testpest"},exclude = {DataSourceAutoConfiguration.class})
public class TestPestApplication extends AbstractJavaFxApplicationSupport {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("測試工具");
primaryStage.setResizable(false);
// 啟動時將主窗口注入到容器中
ContainerUtils.setPrimaryStage(primaryStage);
// 初始化線程池
ContainerUtils.createThreadPool();
// 這里是我自定義的退出確認提示窗口
primaryStage.setOnCloseRequest(windowEvent -> {
windowEvent.consume();
MakeSureExitPage sure = new MakeSureExitPage(windowEvent, primaryStage);
sure.show();
});
super.start(primaryStage);
}
public static void main(String[] args) {
launch(TestPestApplication.class, MainView.class, new InitializationView(), args);
}
}
步驟4
到這里還有兩個類沒有交代MainView和InitializationView,前者是用于喚起主窗口的,后者是用于初始化頁面加載的,你可以設置過場動畫什么的
package com.wcf.test.testpest.view;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;
/**
* @author wangcanfeng
* @time 2019/5/10
* @function
**/
@FXMLView(value = "/view/main.fxml")
public class MainView extends AbstractFxmlView {
}
package com.wcf.test.testpest.view;
import de.felixroske.jfxsupport.SplashScreen;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.image.ImageView;
/**
* @author wangcanfeng
* @time 2019/5/17
* @function
**/
public class InitializationView extends SplashScreen {
@Override
public String getImagePath() {
return "/image/chain.JPG";
}
@Override
public Parent getParent() {
Group gp = new Group();
ImageView imageView = new ImageView(this.getClass().getResource(this.getImagePath()).toExternalForm());
gp.getChildren().add(imageView);
return gp;
}
}
步驟5
編寫主窗口控制器
package com.wcf.test.testpest.controller;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
/**
* @author wangcanfeng
* @time 2019/5/10
* @function
**/
public class MainController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("成功啟動主窗口");
}
}
步驟6
在view文件夾中創(chuàng)建fxml

image.png
然后在fxml中配置好對應的controller
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.wcf.test.testpest.controller.MainController"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>
步驟7
打包成exe的相關配置,這里它打完包之后就會把對應的java運行需要的jar都裝進去,估計會有200M左右

image.png

image.png
步驟8
進行打包

image.png