在 Java 9 或 10 中创建 FXML 成员的正确方法是什么?
What's the correct to make FXML members in Java 9 or 10?
升级到 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
这是否意味着我应该制作它们 public?这是否会使 @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;
}
或
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
模块需要这种访问权限才能使注入工作。)
根据您的包的内容,将控制器移动到它自己的子包中以不允许对非控制器的反射访问可能是有益的类。
升级到 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
这是否意味着我应该制作它们 public?这是否会使 @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;
}
或
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
模块需要这种访问权限才能使注入工作。)
根据您的包的内容,将控制器移动到它自己的子包中以不允许对非控制器的反射访问可能是有益的类。