笔记

万物寻其根,通其堵,便能解其困。
  博客园  :: 新随笔  :: 管理

关于JAVAFX的非jdk8版本构建

Posted on 2024-04-03 11:26  草妖  阅读(15)  评论(0)    收藏  举报

创建jdk17版本的javafx:

pom.xml

<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.namejr</groupId>
  <artifactId>JDKFXDemo17</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JDKFXDemo17</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <!-- javafx依赖 start -->
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-base</artifactId>
      <version>17.0.11</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>17.0.11</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-web</artifactId>
      <version>17.0.11</version>
    </dependency>
    <!-- javafx依赖 end -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.yml</include>
          <include>**/*.xml</include>
          <include>**/*.json</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <!-- 注册webapp目录为资源目录 -->
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
          <include>**/**</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <!-- javafx打包工具 -->
      <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.8</version>
        <configuration>
          <mainClass>Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Main.java

package com.namejr;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {
    private int tempScreenWidth = 1360; // 屏幕宽度
    private int tempScreenHeight = 768;

    @Override
    public void start(Stage stage) throws Exception {
        // 获取屏幕宽度
        Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
        if (primScreenBounds.getWidth() < tempScreenWidth) {
            // 如果屏幕过小,调整
            tempScreenWidth = (int) primScreenBounds.getWidth();
        }
        if (primScreenBounds.getHeight() < tempScreenHeight) {
            // 如果屏幕过小,调整
            tempScreenHeight = (int) primScreenBounds.getHeight();
        }
        // 设置stage透明
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("便捷工具(JAVAFX V1版本)");

        WebView view = new WebView();
        view.setPrefSize(tempScreenWidth, tempScreenHeight);
        // 关闭右键菜单
        view.setContextMenuEnabled(true);

        WebEngine tempWebEngine = view.getEngine();

        Scene scene = new Scene(view, tempScreenWidth, tempScreenHeight);
        stage.setScene(scene);
        // 将stage置于窗口中心
        stage.centerOnScreen();
        // 显示
        stage.show();
        // 加载Url
        tempWebEngine.load("http:///www.baidu.com");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

注:此时运行Main.main()回报下面错误:

"C:\Program Files\Java\jdk-17.0.10\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:19327,suspend=y,server=n -javaagent:C:\Users\namejr\AppData\Local\JetBrains\IntelliJIdea2023.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Users\namejr\JavaCode\JDKFXDemo17\target\classes;C:\Users\namejr\.m2\repository\org\openjfx\javafx-base\17.0.11\javafx-base-17.0.11.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-base\17.0.11\javafx-base-17.0.11-win.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-controls\17.0.11\javafx-controls-17.0.11.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-controls\17.0.11\javafx-controls-17.0.11-win.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-graphics\17.0.11\javafx-graphics-17.0.11.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-graphics\17.0.11\javafx-graphics-17.0.11-win.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-web\17.0.11\javafx-web-17.0.11.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-web\17.0.11\javafx-web-17.0.11-win.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-media\17.0.11\javafx-media-17.0.11.jar;C:\Users\namejr\.m2\repository\org\openjfx\javafx-media\17.0.11\javafx-media-17.0.11-win.jar;C:\Users\namejr\idea\IntelliJ IDEA 2023.2.2\lib\idea_rt.jar" com.namejr.Main
已连接到地址为 ''127.0.0.1:19327',传输: '套接字'' 的目标虚拟机
错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
已与地址为 ''127.0.0.1:19327',传输: '套接字'' 的目标虚拟机断开连接

进程已结束,退出代码为 1

解决方案:再创建一个java文件,启动类修改成下面文件不再使用Main.main(),内容如下:

详情请查看:用maven创建javafx项目 解决“错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序” - 小甲点点 - 博客园 (cnblogs.com)

package com.namejr;

public class AppMainStart {
    public static void main(String[] args) {
        Main.main(args);
    }
}

 

 

构建(包含javafx22版本代码创建):

pom.xml

  <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.namejr</groupId>
  <artifactId>FirstDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>FirstDemo</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</artifactId>
      <version>22</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>22</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-fxml</artifactId>
      <version>22</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-web</artifactId>
      <version>22</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

StartApplicationMain.java

package com.namejr;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class StartApplicationMain extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            // 设置stage透明
            primaryStage.initStyle(StageStyle.UNDECORATED);
            primaryStage.setTitle("便捷工具(JAVAFX V1版本)");
            // 获取屏幕宽度
            Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
            WebView view = new WebView();
            view.setPrefSize(primScreenBounds.getWidth(), primScreenBounds.getHeight());
            // 关闭右键菜单
            view.setContextMenuEnabled(true);
            WebEngine tempWebEngine = view.getEngine();
            Scene scene = new Scene(view, primScreenBounds.getWidth(), primScreenBounds.getHeight());
            primaryStage.setScene(scene);
            // 将stage置于窗口中心
            primaryStage.centerOnScreen();

            // 显示
            primaryStage.show();

            // 设置全屏
            primaryStage.setFullScreen(true);

            // 加载Url
            tempWebEngine.load("https://www.baidu.com");
        } catch (Exception err) {
            err.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

 


需要配置启动选项
--module-path "E:\javafx\javafx-sdk-18.0.2\lib" --add-modules javafx.controls,javafx.fxml,javafx.web 

 

 

问题解决
Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK
删除原来的构建(工件),改为下面jar构建