javafx - 在 Java 9 或 10 中创建 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
这是否意味着我应该公开它们?这是否使@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
用处。仍然需要标记允许使用的非public
成员,FXMLLoader
只需要明确声明允许反射覆盖对成员的访问。(FXMLLoader
使用反射,所以至少javafx.fxml
模块需要这种访问才能使注入工作。)
根据包的内容,将控制器移动到它自己的子包以不允许反射访问非控制器类可能是有益的。
推荐阅读
- python - Python数据存储方法
- docker - 如何在 Dockerfile 中提取 Docker 映像?
- angular - Jasmine 单元测试,ActivatedRoute 上的 .subscribe 问题
- php - 在PHP中获取python程序的运行状态
- c# - 播种 ServiceStack 数据库
- serial-port - 提示硬线返回 fcntl:无效参数
- scala - 将scala列表转换为数组
- mesos - 在 Aurora 和 Mesos 上部署 Heron 调度程序文件时如何找到它?
- python - Pythoncom - 如何收听 Outlook 传出消息?
- arduino - 来自 Arduino (ESP8266WiFi) 的 Web2Py GET 请求