安装Java 和 JavaFX 不多说了。
新建项目
pom.xml 文件
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>test.Test</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
在 java 根目录 创建 module-info.java 文件
module my.javafx {
requires javafx.controls;
requires javafx.fxml;
exports test;
}
Test.java 文件
package test;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
/**
* Test
*
* @date 2020-05-25 17:59
*/
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
System.out.println("Hello");
AnchorPane root = new AnchorPane();
root.getChildren().add(new Button("test"));
stage.setScene(new Scene(root));
stage.setWidth(200);
stage.setHeight(200);
stage.show();
}
}
看看效果
注意事项
如果运行:mvn clean javafx:run 报错,看看JAVA_HOME是否配置的java11