升级到 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 中基本上毫无用处?
最佳答案
由于您使用的是模块,因此默认情况下不允许反射访问类的私有(private)成员。异常基本上告诉您需要做什么:
module tech.flexpoint.dashman {
...
// allow everyone to access classes in tech.flexpoint.dashman.controllers.configurator via reflection
opens tech.flexpoint.dashman.controllers.configurator;
}
或
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
变得无用。仍然需要标记允许 FXMLLoader
使用的非 public
成员,只需要显式声明允许反射覆盖对成员的访问即可。 (FXMLLoader
使用反射,因此至少 javafx.fxml
模块需要这种访问才能使注入(inject)工作。)
根据包的内容,将 Controller 移动到它自己的子包中可能会有所帮助,以不允许对非 Controller 类进行反射访问。
关于javafx - 在 Java 9 或 10 中创建 FXML 成员的正确做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347107/