使用Maven快速集成搭建和开发JavaFX进行桌面应用开发

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

保姆级教程,包教包会

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

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

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

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

项目结构:

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

准确说是这样:

先右键resources——New——Directory

 再右键org——New——Directory

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

 

 再在里面放入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,点击:

 如果这一步访问maven仓库过慢,可以参考我的另一篇文章换国内源:

IDEA两步完成Maven换源加快下载速度_Stallion_X的博客-CSDN博客

出现窗口即为成功:

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

本文参考了这几篇文章,并在其基础上踩坑改进:

使用Maven 构建、开发和打包 JavaFX 项目_javafx maven_一枚码农404的博客-CSDN博客

Maven方式开发JavaFX_weixin_44260298的博客-CSDN博客

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值