前言
最近在学习SpringBoot和javaFX,准备做个小项目练练手,本文章记录一下使用SpringBoot开发javaFX的环境配置。
准备
IDEA
JDK11以上(在JDK11里面将Java FX独立开了,所以要使用JDK11开发JavaFX应用就要将相关的库导入)
javaFX库:
https://gluonhq.com/products/javafx/
JavaFX Scene Builder 2.0(可视化工具,加速JavaFX图形界面的开发,强烈建议安装)
JavaFX Scene Builder 2.0
步骤
1.创建SpringBoot应用
然后。。。默认就行
完成:
调整目录
这里我把src设为源文件夹,创建view和controller包
2.配置JavaFX Scene Builder 2.0
文件>设置>语言和框架>javaFX
填入你的安装路径:
3.Maven依赖引入
pom.xml
推荐一个查询最新Maven的网站:https://mvnrepository.com/
<?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 https://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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rightstar</groupId>
<artifactId>javafx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>javafx</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>12</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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springBoot-javaFX支持 这个库比较重要-->
<dependency>
<groupId>de.roskenet</groupId>
<artifactId>springboot-javafx-support</artifactId>
<version>2.1.6</version>
</dependency>
<!--javaFX依赖-->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>16-ea+5</version>
</dependency>
<!--maven扩展依赖 和打包工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.1</version>
</dependency>
<!--
报错先不用
<dependency>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- <plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>rightstar.MainSpringBoot.main</mainClass>
</configuration>
</plugin>-->
</plugins>
</build>
</project>
4.编写第一个界面
使用SceneBuilder 编写InitScene.fxml
resource文件夹下创建InitScene.fxml文件,打开
添加一个Pane和Button
ctrl+s保存
编写绑定的InitSceneView类(继承AbstractFxmlView)
package com.rightstar.view;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;
@FXMLView("/InitScene.fxml")
public class InitSceneView extends AbstractFxmlView {
}
编写启动类JavafxApplication(继承AbstractJavaFxApplicationSupport)
package com.rightstar;
import com.rightstar.view.InitSceneView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JavafxApplication extends AbstractJavaFxApplicationSupport {
public static void main(String[] args) {
//改成launch方法,
launch(JavafxApplication.class, InitSceneView.class,args);
}
}
启动(重点需要配置VM)
–module-path换成你安装的javaFXSDK路径,(示例):
--module-path "D:\javaTools\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml
启动
ok
后言
欢迎继续关注作者后面的博文更新,后面我将更新如何使用SpringBoot+javaFX编写一个简单的文件树显示程序
————————————————
版权声明:本文为CSDN博主「RightStar_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_33886316/article/details/111146522