【问题标题】:Cannot import JavaFX WebView in Maven project无法在 Maven 项目中导入 JavaFX WebView
【发布时间】:2021-06-21 16:26:00
【问题描述】:

我使用我在此处找到的原型创建了一个 Maven JavaFX 项目:https://github.com/openjfx/javafx-maven-archetypes

一切运行顺利,我可以运行一个带有一些标签、一个文本框和一些按钮的项目而不会出现问题。但是,当我尝试添加 WebView 元素时(尽管 IntelliJ 中的 Scene Builder 可以识别),我无法让导入工作。

我的 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>untitled3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>16</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>org.dianavrabie.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的主要 fxml 文档来描述视图:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<VBox alignment="TOP_CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.dianavrabie.PrimaryController">
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
   <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Label layoutX="14.0" layoutY="14.0" text="POLYNOMIAL CALCULATOR" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
               <font>
                  <Font name="System Bold" size="36.0" />
               </font></Label>
            <Label layoutY="53.0" text="Polynomial 1" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Label layoutY="90.0" text="Polynomial 2" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <TextField layoutX="124.0" layoutY="54.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
            <TextField layoutX="117.0" layoutY="91.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
            <Button layoutY="136.0" mnemonicParsing="false" text="ADD" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="57.0" layoutY="136.0" mnemonicParsing="false" text="SUBTRACT" AnchorPane.leftAnchor="57.0">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="156.0" layoutY="136.0" mnemonicParsing="false" text="MULTIPLY">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="251.0" layoutY="137.0" mnemonicParsing="false" text="DIVIDE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="324.0" layoutY="137.0" mnemonicParsing="false" text="DERIVATIVE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="432.0" layoutY="137.0" mnemonicParsing="false" text="INTEGRATE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="538.0" layoutY="137.0" mnemonicParsing="false" text="CLEAR" textFill="#fc5400">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            
         </children>
      </AnchorPane>
   </children>
</VBox>

还有主应用类

package org.dianavrabie;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Scene scene;


    @Override
    public void start(Stage stage) throws IOException {

        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {

        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

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

}

我需要做什么才能让 WebView 工作?我对此很陌生,似乎找不到解决方案。

【问题讨论】:

  • 您好!它可能没有用,但是您是否尝试使用相同的 JavaFx 组合版本?您使用 Java 版本 11,Javafx 版本 13 用于控制,fxml 和 Javafx 版本 16 用于 javafx web...我会尝试使用同质版本以避免冲突。
  • 我在添加 WebView 时也遇到了问题,但我有一个异常说“模块 javafx.graphics 不会将 com.sun.javafx.sg.prism 导出到未命名的模块”(以及一堆错误同类型)。它可以通过添加“--add-exports javafx.graphics/com.sun.prism=ALL-UNNAMED”(以及每个错误的许多其他导出)来解决,但是添加所有这些很奇怪,我一定遗漏了一些东西.

标签: java maven javafx


【解决方案1】:

如果你想使用 Maven。你可以。最好与3个依赖的版本保持一致。截至今天:

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo1</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>5.7.1</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>17.0.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>17.0.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>17.0.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.6</version>
            <executions>
                <execution>
                    <!-- Default configuration for running with: mvn clean javafx:run -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>com.example.demo1/com.example.demo1.HelloApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

    【解决方案2】:

    我最终通过命令行工具启动我的应用程序的简单 VM 选项来修复这些 WebView 问题:

    --add-modules javafx.web
    

    不需要其他模块或导出,就像我想的那样,使用默认的 javaFx 模块信息文件一切正常。

    【讨论】:

    • 这个答案对我来说效果更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2019-07-20
    • 2020-01-10
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多