首先先创建一个maven项目。
先配置pom文件。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.openjfx</groupId> <artifactId>hellofx</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>13</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.3</version> <configuration> <mainClass>AppStart</mainClass> </configuration> </plugin> </plugins> </build> </project>
然后在src路径下,建立一个启动类和一个app类。
其中
package com.oracle.cuber4you; public class AppStart { public static void main(String[] args) { App.main(args); } }
app类中
package com.oracle.cuber4you; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * Hello world! * */ public class App extends Application { public static void main( String[] args ) { System.out.println( "Hello World!" ); launch(); } @Override public void start(Stage primaryStage) throws Exception { // TODO Auto-generated method stub String javaVersion = System.getProperty("java.version"); String javafxVersion = System.getProperty("javafx.version"); Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "."); Scene scene = new Scene(new StackPane(l), 640, 480); primaryStage.setScene(scene); primaryStage.show(); } }
即可启动项目。
注意,不能直接启动App,会出现“错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序”此错误。
下面是javaFx的网站。
https://openjfx.io/openjfx-docs/#maven