JDK14新建javafx+Maven项目

JDK14新建javafx+Maven项目

1.在IDEA中创建Maven项目:

JDK14新建javafx+Maven项目_第1张图片

2.完成新建后项目结构如下:
JDK14新建javafx+Maven项目_第2张图片
3.选择项目环境结构(New创建JDK指定到自己的JDK14所在目录就可以了)
JDK14新建javafx+Maven项目_第3张图片
4.在src/main/java中加入AppLauncher 启动类(原因:可以绕过Application启动检查,不然会在打包后报错"缺少javafx运行时组件"),代码如下

package com.xzl;

import javafx.application.Application;

public class AppLauncher {
    public static void main(String[] args) {
        Application.launch(JavaFxApplication.class,args);
    }
}

5.在src/main/java中加入javafx启动类,代码如下

/**
 * Author:   徐志林
 * Date:     2020/3/12/012 19:50
 */
package com.xzl;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @author xzl
 * @create 2020/3/12
 */
public class Applicaion extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage stage){
        Text text = new Text("JDK14,javafx11瘦身啦");
        HBox sceneBox = new HBox(text);
        sceneBox.setStyle("-fx-alignment: center;-fx-text-fill: red;-fx-background-color: transparent;-fx-border-color: green;-fx-font-size: 1.8em;");
        Scene scene = new Scene(sceneBox,600,400);
        stage.setScene(scene);
        stage.show();
    }
}

6.在pom.xml中配置如下(注:当前项目中只用到了javafx相关,加入netty是为了后面项目打包示范)

<?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>com.xzl</groupId>
    <artifactId>xzlDemo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.25.Final</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.hq.AppLauncher</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

7.点击运行AppLauncher.java
JDK14新建javafx+Maven项目_第4张图片
重点: 注意pom文件中的打包方式–将Maven依赖打入jar包,是后续瘦身打包的前提!

jpackage工具参考:JDK14打包工具jpackage的使用

下一篇 – JDK14+JAVAFX14+Maven定制jre打包瘦身,必成版

你可能感兴趣的:(JDK14新建javafx+Maven项目)