java.lang.IllegalAccessException: 模块 javafx.base 无法访问 class sample.model.Artists 因为模块没有打开 sample.model 到 javafx.base

java.lang.IllegalAccessException: module javafx.base cannot access class sample.model.Artists because module does not open sample.model to javafx.base

尽管我使用了 SimpleStringProperty 和 SimpleIntegerProperty,但每次单击“List Artists”按钮时都会出现以下错误。

May 03, 2021 9:33:22 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'name' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@7bcaa928 with provided class type: class sample.model.Artists
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class sample.model.Artists (in module MusicUI) because module MusicUI does not open sample.model to javafx.base
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:184)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:154)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
    at ....

控制器 class 具有按钮“列出艺术家”的事件处理程序: 当我们调用 new Thread(task).start()

时会出现此错误
public class Controller {
    @FXML
    public TableView<Artists> artistTable;

    @FXML
    public void listArtists(){
        Task<ObservableList<Artists>> task = new GetAllArtistsTask();
        artistTable.itemsProperty().bind(task.valueProperty());
        new Thread(task).start();
    }

}
class GetAllArtistsTask extends Task {
    // following method works correct as I printed the observable list.
    @Override
    protected ObservableList<Artists> call() throws Exception {
        return FXCollections.observableArrayList(DataSource.getInstance().
                getArtists(DataSource.ORDER_BY_ASC));
    }
}

XML 文件:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
            fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <TableView fx:id="artistTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <columns>

                <TableColumn prefWidth="${artistTable.width}" text="Name" >
                    <cellValueFactory>
                        <!--                        This maps to name column in Table artists-->
                        <PropertyValueFactory property="name"/>
                    </cellValueFactory>
                </TableColumn>
            </columns>
            <BorderPane.margin>
                <Insets right="10.0" />
            </BorderPane.margin>
        </TableView>
    </center>
    <right>
        <VBox alignment="CENTER" prefHeight="200.0" prefWidth="170.00" spacing="20.0" BorderPane.alignment="CENTER">
            <BorderPane.margin>
                <Insets right="10.0"/>
            </BorderPane.margin>
            <Button onAction="#listArtists" maxWidth="Infinity" mnemonicParsing="false" text="List Artists"/>
            <Button maxWidth="Infinity" mnemonicParsing="false" text="Show Albums (artist)"/>
            <Button maxWidth="Infinity" mnemonicParsing="false" text="Update Artists"/>
        </VBox>
    </right>
    <bottom>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <ProgressBar prefWidth="200.0" progress="0.0">
                <HBox.margin>
                    <Insets left="50.0"/>
                </HBox.margin>
            </ProgressBar>
        </HBox>
    </bottom>
</BorderPane>

艺术家Class: 我使用了 SimpleIntegerProperty 和 SimpleStringProperty 而不是指定的 int 和 String:

    public class Artists {
    private final SimpleIntegerProperty _id;
    private final SimpleStringProperty name;

    public int get_id() { return _id.get(); }
    public String getName() { return name.get();}

    public Artists(int _id, String name) {
        this._id = new SimpleIntegerProperty();
        this._id.set(_id);
        this.name = new SimpleStringProperty();
        this.name.set(name);
    }
    @Override
    public String toString(){
        return "ID: "+ get_id() +", Name: "+ getName();
    }
}

需要修改module-info.java。当您将 groupId 定义为“样本”(您可以在 pom.xml 中找到它)时,这应该有效:

模块-info.java

module sample {
    requires javafx.controls;
    requires javafx.fxml;

    opens sample to javafx.fxml;
    opens sample.model to javafx.fxml;

    exports sample;
    exports sample.model;
}