1.使用idea创建JavaFX项目
创建后运行尝试:
可以看到目前是能正常运行 Hello Word项目的,这个时候还没有maven支持。接下来开始引入maven
2.JavaFX 项目 maven引入
2.1 添加新框架支持
接下来的弹出选中maven,如下图所示:
引入后需要在maven的pom.xml文件添加以下内容:
$title(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>groupId</groupId>
<artifactId>testfx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>sample.Main</mainClass>
<javafx.version>11.0.2</javafx.version>
<javafx.maven.plugin.version>0.0.1</javafx.maven.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<!--<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>-->
<!--使用shade插件直接把Javafx的相关包打入到jar里面-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- 加载主类 -->
<mainClass>${mainClass}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
接下来刷新下maven项目:
刷新后的项目结构如下:
maven的引入,多了一个resources资源目录,其他相似。这个时候再次运行Main,和最初运行结果一样,看到Hello Word
【提示】:jdk环境必须是11哟,我这里引入的是11的包;
【提示】:
如果这一步启动发生了错误:Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
请参考:Exception in thread "WindowsNativeRunloopThreadjava.lang.NoSuchMethodError: <init>
【提示】:
如果启动报错:Caused by: java.lang.RuntimeException: Exception in Application start method
请参考:Caused by: java.lang.RuntimeException: Exception in Application start method
原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/243825.html