在Java 9或10中成为FXML成员的正确方法是什么? [英] What's the correct to make FXML members in Java 9 or 10?

查看:91
本文介绍了在Java 9或10中成为FXML成员的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java 8升级到Java 10后,出现以下错误:

After upgrading to Java 10 (from 8), I'm getting these errors:

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中基本上无用吗?

Does it mean I should make them public? Does that make the @FXML annotation essentially useless in Java 9 and 10?

推荐答案

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

Since you're using a module, reflection is not allowed to access private members of your classes by default. The exception basically tells you what needs to be done:

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模块需要这种访问才能使注入工作.)

This does not make @FXML useless. It's still needed to mark non-public members that FXMLLoader is allowed to use, it's just required to explicitly state that reflection is allowed to override access to members. (FXMLLoader uses reflection so at least the javafx.fxml module needs this kind of access for injection to work.)

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

Depending on the contents of your package it could be beneficial to move the controller(s) to it's own subpackage to not allow reflective access to non-controller classes.

这篇关于在Java 9或10中成为FXML成员的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆