首页 > 解决方案 > @FXML annotation and FXMLLoader class not resolved to a type in Java 11 and JavaFX 11

问题描述

Earlier my project used to be on Java 8 but now I am using Java 11 along with JavaFX 11 and now JavaFX has been decoupled from Java since Java 11. I haven't download the JavaFX SDK but added below dependency in pom.xml for getting required modules and jar files which were used to be part of Java itself in earlier versions.

       <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>

But I am getting compilation error can not be resolved to a type on @FXML annotation and FXMLLoader class.

Do I need to add some other/extra dependencies to resolve this issue?

标签: javajavafxopenjfxjavafx-11

解决方案


FXML(注解、FXMLLoader...)在 Java 9 版本@FXML中移至模块中。javafx.fxml

在 JavaFX 11 之前,每当您调用与 JavaFX 相关的东西时,您都可以在 SDK 中使用所有 javafx 模块。

但现在你必须包括你需要的:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>11</version>
</dependency>

请注意,这javafx.controls取决于javafx.graphicsand java.base,因此您不需要包括那些。并且javafx.fxml直接取决于javafx.base.


推荐阅读