在 fxml 中显示整个链表

display a whole linked list in fxml

我正在创建一个 LinkedList,其中包含某人最喜欢的电影。

我想用 FXML 显示整个列表,但是 Label 不支持多行输出。

这是我试过的代码(filmFavoritenLinkedList):

static LinkedList<Film> filmFavoriten = new LinkedList<>();

@FXML
private void displayList() throws IOException {
    GridPane pane = FXMLLoader.load(getClass().getClassLoader().getResource("displayList.fxml"));
    rootpane.getChildren().setAll(pane);

    for (int r = 0; r < filmFavoriten.size(); r++) {
        Label listTable = new Label();
        listTable.setText((r + 1) + ". " + filmFavoriten.get(r).title);
    }
}

请推荐。

你做得对,但循环中新创建的 Label 不会自动添加到 GridPane

for (...) {
    ...
    pane.getChildren().add(listTable);
}

... but a Label doesn't support multiline output

根据您的代码,每部电影都有一个没有换行符的标签。电影被打印成单独的 Labels,因此不需要通过 '\n'.

合并行

我建议使用 ListView<Film>Vbox(参见下面的示例)。

VBox verticalBox = new VBox();

for (...) {
    ...
    verticalBox.getChildren().add(listTable);
}

pane.getChildren().add(verticalBox);