升级到 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
这是否意味着我应该将它们公开?这是否使@FXML
注释在 Java 9 和 10 中基本上没有用?
由于您使用的是模块,因此默认情况下不允许反射访问类的私有成员。该异常基本上告诉您需要做什么:
module tech.flexpoint.dashman {
...
// allow everyone to access classes in tech.flexpoint.dashman.controllers.configurator via reflection
opens tech.flexpoint.dashman.controllers.configurator;
}
or
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;
}
这并不使@FXML
无用。仍然需要标记非public
成员认为FXMLLoader
允许使用,只需要明确声明允许反射覆盖对成员的访问。 (FXMLLoader
使用反射所以至少javafx.fxml
模块需要这种访问才能使注入工作。)
根据包的内容,将控制器移动到它自己的子包以不允许对非控制器类进行反射访问可能是有益的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)