1. 创建项目并配置主模块
首先在IDEA中新建一个项目,这里我使用的是创建基于maven的自定义项目,需要手动引入spring依赖,使用Spring Initializr应该是可行的,同时还能省下引入Spring的过程。
创建完成后的生成的模块即为父模块,如果不需要分模块管理项目可以忽略下面创建子模块的过程
2. 创建子模块,并绑定父模块
这里最好还是使用默认的生成器,避免依赖的重复引入
以下是生成的子模块的pom.xml文件,与一般pom.xml文件不同的是它多了parent,用于绑定父模块
<parent>
<groupId>com.sinevil</groupId>
<artifactId>spring-javafx-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
创建完子模块后,父模块的pom.xml文件也被自动添加了<modules>标签,用于指向子模块
<modules>
<module>moudle_demo1</module>
</modules>
3. 依赖引入
父模块依赖引入
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
<junit.version>5.9.2</junit.version>
<lombok.version>1.18.24</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
子模块引入javaFX依赖
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21-ea+24</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21-ea+24</version>
</dependency>
</dependencies>
4. 编写javaFx代码
配置module时,后面的模块名貌似不是必须和所处模块保持一致
但是controller的包必须向javafx.fxml开放,否则会报javafx.fxml.LoadException
module com.demo.moudledemo {
requires javafx.controls;
requires javafx.fxml;
requires spring.boot;
requires spring.boot.autoconfigure;
opens com.demo.controller to javafx.fxml;
exports com.demo;
}
启动窗口
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("/hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
Application.launch(HelloApplication.class, args);
SpringApplication.run(DemoApplication.class, args);
}
}