事情很简单.我想创建一个支持WevView的javafx项目

  • 打开intellij,创建新项目,在左侧 Select javafx,并在右侧 Select maven,完成.
  • 我在pom中添加了javafx web依赖项.xml
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-web</artifactId>
    <version>16</version>
</dependency>
  • 在running的配置中,我创建了一个Application配置,其中的VM选项是--add-modules javafx.web,而不是S.O. question
  • 我运行了应用程序,预构建的示例运行得非常好
  • 使用Gluon SceneBuilder打开.fxml.
  • 添加了WebView,给了它一个id,然后保存了它
  • 访问控制器类中的元素,但出现错误

错误:

无法解析符号"WebView"

我的主课

package com.example.webbrowser;

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

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

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

我的控制器类:

package com.example.webbrowser;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.net.URL;
import java.util.ResourceBundle;

public class HelloController implements Initializable {
    @FXML
    private WebView web;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

我的fxml文件:

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.webbrowser.HelloController">
   <children>
      <WebView fx:id="web" layoutX="225.0" layoutY="87.0" prefHeight="200.0" prefWidth="200.0" />
   </children>
</AnchorPane>

我的pom.xml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

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

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


        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>16</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>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.webbrowser/com.example.webbrowser.HelloApplication</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

What changes should i make to make the WebView work correctly??

推荐答案

将第requires javafx.web;行添加到module-info.java行.

Java相关问答推荐

Java Swing绘制gif作为多个JSYS和JLabels的背景

使用json参数通过单击jSP文件中的按钮来运行server时出现问题

更新我们的一个文物后出现了严重的符号引用错误

查找最大子数组的和

Java中不同包中的类之间的配置共享

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

为什么我们不能实现两个接口,其中一个接口有相同的签名,其中一个接口有默认的实现在java?'

我无法获取我的Java Spring应用程序的Logback跟踪日志(log)输出

测试期间未执行开放重写方法

我可以在MacOS上使用什么Java函数来在适当的设备上以适当的音量播放适当的alert 声音?

如何在Cosmos DB(Java SDK)中增加默认响应大小

删除打印语句会影响功能...腐败在起作用?

从LineChart<;字符串、字符串和gt;中删除数据时出现特殊的ClassCastException;

通过Java列表中的某些字段搜索值

如何在不作为类出现的表上执行原生查询?

有没有办法知道在合并中执行了什么操作?

如何在字节数组中反转UTF-8编码?

为什么mvn编译生命周期阶段不只是编译已更改的java文件?

转换为JSON字符串时,日期按天递减-Java

java 11上出现DateTimeParseException,但java 8上没有