宗旨:踩过一遍的坑就不要再踩了

保姆级教程,包教包会

用这个方法,免去每次都得手动配jar包的烦恼,也不需要再去管什么启动类的VM参数--module-path了

用这个方法也不需要再去管什么javafx-maven-archetypes了(现在官方maven仓库貌似都把这玩意的原型移除了,archetype-catalog.xml里面已经找不到了)

直接创建新的Maven项目,别的什么都不用管

java中的maven项目 maven javafx_maven

然后按照这个项目结构创建下面的文件

项目结构:

java中的maven项目 maven javafx_java_02

注意这里有个大坑,在resources目录下创建的目录并不是直接叫做"org.example",而是先创建org目录,再创建example目录,这样创建出的目录结构是不一样的

准确说是这样:

先右键resources——New——Directory

java中的maven项目 maven javafx_maven_03

 再右键org——New——Directory

java中的maven项目 maven javafx_intellij-idea_04

这样创建出的目录结构才是对的,之后在编译目录下资源文件才会被放对位置

 

java中的maven项目 maven javafx_maven_05

 再在里面放入application.css等资源文件即可 

 (这里主要是为了对应后续代码中的getClass()下的默认路径保证一遍成功,有懂的大佬可以忽略)

pom.xml:

<?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>

    <groupId>org.example</groupId>
    <artifactId>javafx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <excludes>
                        <exclude>module-info.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>org.example</mainClass>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <!--把src/main/java目录下的properties、xm文件打包打进程序中-->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <!--把src/main/resources目录下的properties、xml、css文件打包打进程序中-->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.fxml</include>
                    <include>**/*.css</include>
                    <include>**/*.setting</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <!--把lib/目录下第三方jar包打进程序中,如systemPath目录下的jar-->
                <directory>lib/</directory>
                <includes>
                    <include>**/*.jar</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>
    <profiles>
        <profile>
            <id>openjfx</id>
            <activation>
                <jdk>[17,)</jdk>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-controls</artifactId>
                    <version>20.0.1</version>
                </dependency>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-fxml</artifactId>
                    <version>20.0.1</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

</project>

module-info.java

module org.example {
    requires javafx.controls;
    requires javafx.fxml;

    opens org.example to javafx.fxml;
    exports org.example;
}

application.css(默认):

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */

Main类(测试最小项目):

package org.example;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.Objects;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("application.css")).toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

文件都安排好以后,右击pom.xml,按照图中找到Reload Project,点击:

java中的maven项目 maven javafx_intellij idea_06


出现窗口即为成功:

java中的maven项目 maven javafx_maven_07

 后续如果要用到JavaFX SDK中的其它包,除了在pom.xml中添加,也需要在module-info.java里面加入包名。