在Java 9或10中创建FXML成员的正确性是什么?

pup*_*eno 4 javafx fxml java-9 java-10 java-module

升级到Java 10(从8开始)后,我遇到了这些错误:

InaccessibleObjectException: Unable to make field private javafx.scene.control.Button tech.flexpoint.dashman.controllers.configurator.RegistrationController.registerButton accessible: module tech.flexpoint.dashman does not "opens tech.flexpoint.dashman.controllers.configurator" to module javafx.fxml
Run Code Online (Sandbox Code Playgroud)

这是否意味着我应该将它们公之于众?这是否使得@FXML注释在Java 9和10中基本无用?

fab*_*ian 9

由于您使用的是模块,因此默认情况下不允许反射访问类的私有成员.该异常基本上告诉您需要做什么:

module tech.flexpoint.dashman {
    ...

    // allow everyone to access classes in tech.flexpoint.dashman.controllers.configurator via reflection
    opens tech.flexpoint.dashman.controllers.configurator;
}
Run Code Online (Sandbox Code Playgroud)

要么

module tech.flexpoint.dashman {
    ...

    // allow only module javafx.fxml access classes in tech.flexpoint.dashman.controllers.configurator via reflection
    opens tech.flexpoint.dashman.controllers.configurator to javafx.fxml;
}
Run Code Online (Sandbox Code Playgroud)

这不会@FXML没用.仍然需要标记允许使用的非public成员,FXMLLoader只需要明确声明允许反射覆盖对成员的访问.(FXMLLoader使用反射,所以至少javafx.fxml模块需要这种注入的访问才能工作.)

根据程序包的内容,将控制器移动到自己的子程序包以不允许反向访问非控制器类是有益的.