【问题标题】:module javafx.base cannot access class application.customer (in module FCR) because module FCR does not open application to javafx.base模块 javafx.base 无法访问类 application.customer(在模块 FCR 中),因为模块 FCR 没有打开应用程序到 javafx.base
【发布时间】:2021-10-16 04:55:40
【问题描述】:

我正在尝试在 javafx 中的 tableView 中显示数据。

即使我使用了 SimpleStringProperty 和 SimpleIntegerProperty,但每次单击“检查客户”按钮时,我都会收到此错误:

原因:java.lang.IllegalAccessException:模块 javafx.base 无法访问类 application.customer(在模块 FCR 中),因为模块 FCR 没有打开应用程序到 javafx.base

在 javafx.base/com.sun.javafx.property.MethodHelper.invoke(MethodHelper.java:69)
在 javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:197)
... 98 更多

这是客户的代码:

public class customer {

private final IntegerProperty ID;
private final StringProperty  firstName;
private final StringProperty  lastName;
private final StringProperty  carPlate;
private final IntegerProperty telephoneNum;

public customer(int ID,String firstName, String lastName, String carPlate, int telephoneNum) {
    this.ID = new SimpleIntegerProperty();
    this.firstName = new SimpleStringProperty();
    this.lastName = new SimpleStringProperty();
    this.carPlate = new SimpleStringProperty();
    this.telephoneNum = new SimpleIntegerProperty();

    setID(ID);
    setfirstName(firstName);
    setlastName(lastName);
    setcarPlate(carPlate);
    settelephoneNum(telephoneNum);
}

public final StringProperty firstNameProperty() {
    return firstName;
}

public final String getfirstName() {
    return firstName.get();
}

public final void setfirstName(String fname) {
    firstName.set(fname);
}

public final IntegerProperty IDProperty() {
    return ID;
}

public final int getID() {
    return ID.get();
}

public final void setID(int iD) {
    ID.set(iD);
}

public final StringProperty lastNameProperty() {
    return lastName;
}

public final String getlastName() {
    return lastName.get();
}

public final void setlastName(String lname) {
    lastName.set(getfirstName());;
}

public final IntegerProperty telephoneNumProperty() {
    return telephoneNum;
}

public final int gettelephoneNum() {
    return telephoneNum.get();
}

public final void settelephoneNum(int tnum) {
    telephoneNum.set(tnum);;
}

public final StringProperty carPlateProperty() {
    return carPlate;
}

public final String getcarPlate() {
    return carPlate.get();
}

public final void setcarPlate(String platenum) {
    carPlate.set(platenum);
}

这里是控制器:

public class checkCustomers {

private customersData q = Main.q;

@FXML
private TableView table;


@FXML
private TableColumn firstName;
@FXML
private TableColumn lastName;
@FXML
private TableColumn carPlate;
@FXML
private TableColumn telephoneNumber;


@FXML
public void initialize() {
    
System.out.println(1);

firstName.setCellValueFactory(new PropertyValueFactory<customer, String>("firstName"));
lastName.setCellValueFactory(new PropertyValueFactory<customer, String>("lastName"));
carPlate.setCellValueFactory(new PropertyValueFactory<customer, String>("carPlate"));
telephoneNumber.setCellValueFactory(new PropertyValueFactory<customer, Integer>("telephoneNumber"));
table.setItems(FXCollections.observableArrayList(q.getAllCustomers()));
}

module-info.java 代码为:

module FCR {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires javafx.base;

    opens application to javafx.graphics, javafx.fxml;
}

我想我应该在模块信息中添加一些东西,有人可以帮忙吗?

【问题讨论】:

  • 为什么不包括 opens application to javafx.graphics, javafx.fxml, javafx.base; ?或者 export application 虽然不清楚什么是应用程序。
  • 成功了!非常感谢!!!!
  • 请遵守 java 命名约定

标签: java eclipse javafx


【解决方案1】:

打开你的包裹到javafx.base

PropertyValueFactory 类使用反射来获取命名属性。您必须授予该类的模块 (javafx.base) 对代码的必要访问权限才能使此反射起作用。

变化:

opens application to javafx.graphics, javafx.fxml;

收件人:

opens application to javafx.graphics, javafx.fxml, javafx.base;

使用 Lambda 表达式

另一个选项是不使用PropertyValueFactory。这个类很可能是创建的,因为 JavaFX 是在 Java 8 中添加 lambda 表达式之前发布的。它使得声明值工厂不那么冗长,但它有两个问题:

  1. 它使用反射。
    • 不一定是坏事,但应尽可能避免反思。
  2. 没有编译时安全性。
    • 如果模型类中不存在所需的方法,或者方法返回错误的类型,那么您只能在运行时发现它。

使用 lambda 表达式可以避免这两个问题。例如:

lastNameCol.setCellValueFactory(data -> data.getValue().lastNameProperty());

不要使用原始类型

查看What is a raw type and why shouldn't we use it? 问答。您正在使用 TableViewTableColumn 的原始类型。不要那样做;声明类型参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 2022-01-26
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2019-05-07
    相关资源
    最近更新 更多