JavaFX 非模块化开发环境搭建
创建maven项目 File -New-Maven Project
添加依赖项绕过模块化的重点是直接通过classifier选择当前运行的平台win 如果是linux或mac
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vk</groupId>
<artifactId>vkfx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javafx.version>21.0.8-ea+1</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>${javafx.version}</version>
<classifier>win</classifier>
</dependency>
</dependencies>
</project>
示例代码 注意main函数不能写在Application子类中当前即是(VKFxApplication),否则会出现错误
错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
package com.vk;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class VKFxApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Label b1 =new Label("Victorkevin");
pane.setCenter(b1);
Scene scene = new Scene(pane, 400, 400);
stage.setScene(scene);
stage.setTitle("Vk demo");
stage.show();
}
}
运行启动方式代码,注意的是通过LauncherImpl释放应用程序 效果如下图
package com.vk;
import com.sun.javafx.application.LauncherImpl;
public class VKMain {
public static void main(String[] args) {
System.out.println("start it");
LauncherImpl.launchApplication(VKFxApplication.class, args);
}
}