0

我正在尝试启动一个 javafx 窗口,每当我运行它时,我都会得到java.lang.reflect.InaccessibleObjectException: Unable to make field private javafx.scene.web.WebView com.vendify.FX.FXMLDocumentController.webView accessible: module com.vendify.FX does not "opens com.vendify.FX" to module javafx.fxml

目前,我的设置如下所示:

应用程序.java

package com.vendify.FX;

import java.io.File;
import java.io.IOException;

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

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

    @Override
    public void start(Stage stage) throws IOException {
        File webViewFXML = new File("src/main/java/com/vendify/FX/test.fxml");
        FXMLLoader loader = new FXMLLoader(webViewFXML.toURI().toURL());
        Parent root = loader.load();
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();

    }

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

}

测试.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.web.WebView?>


<Pane 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.vendify.FX.FXMLDocumentController">
   <children>
      <WebView fx:id="webView" layoutX="-1.0" layoutY="61.0" prefHeight="339.0" prefWidth="600.0"/>
   </children>
</Pane>

FXMLDocumentController.java

    package com.vendify.FX;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.web.WebView;

public class FXMLDocumentController implements Initializable {
    @FXML
    private WebView webView;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        System.out.print("\n\n\n\n\n\ninitialize called\n\n\n\n");
        // WebEngine engine = webView.getEngine();
        // engine.load("http://www.newgrounds.com/");
    }

    public void reloadPage() {
        System.out.print("buttonClicked");
    }
}

模块信息.java

module com.vendify.FX {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;

exports com.vendify.FX;

}

最后是 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>com.vendify</groupId>
    <artifactId>FX</artifactId>
    <version>0.0.1-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>16</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>16</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>16</version>
        </dependency>

        <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>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.vendify.FX.App</mainClass>
                            <argLine>
                                --illegal-access=permit
                            </argLine>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我在 jdk 14 上运行(高于 jdk 11 要求),我真的很卡住。在我的 .fxml 中,如果我删除fx:controller="com.vendify.FX.FXMLDocumentController"它,它似乎运行良好,但没有正确调用 Initialize(),因此,webView 实际上并没有加载页面。

我对这个 javaFX 有点陌生,所以非常感谢任何帮助!

4

0 回答 0