繁体   English   中英

无法在 Maven 项目中导入 JavaFX WebView

[英]Cannot import JavaFX WebView in Maven project

我使用在这里找到的原型创建了一个 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>

和主应用程序 class

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 正常工作? 我真的很陌生,我似乎找不到解决方案。

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

--add-modules javafx.web

正如我所想,不需要其他模块或导出,默认 javaFx 模块信息文件一切正常。

在此处输入图像描述

如果要使用 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>

几个问题:

  • 版本 class 版本 <> 神器的 jar 版本兼容性?

  • module-info.java /*如果出现 */ 需要 javafx.web;

当 WebView 对编译器不可见时,缺少其中一个因素会导致项目行为

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM