gpt4 book ai didi

java - 用标签填充 GridPane

转载 作者:行者123 更新时间:2023-11-30 06:50:35 26 4
gpt4 key购买 nike

我有这个构造函数,我正在尝试用标签填充网格 Pane 。我碰壁了,不知道出了什么问题。我需要在 1 行中创建 13 个标签。

构造函数:

public class Labels {
@FXML
GridPane gridPane = new GridPane();

public Labels(String labelname, int columnIndex, int rowIndex) {
Label label = new Label();
gridPane.setColumnIndex(label, columnIndex);
gridPane.setRowIndex(label, rowIndex);
label.setId(labelname+columnIndex);
label.setVisible(true);
label.setText("test");
}

}

Controller 循环:

for(int i2=0; i2<13; i2++){

Labels labels = new Labels("label", i2, 3);
}

最佳答案

您没有将Label添加到GridPane。此外,您可以为每个Label使用新的GridPane,并且永远不要在任何地方使用这些GridPane

public class Labels {

private GridPane gridPane = new GridPane();

public GridPane getGridPane() {
return gridPane;
}

public void addLabel(String labelname, int columnIndex, int rowIndex) {
Label label = new Label();
GridPane.setColumnIndex(label, columnIndex);
GridPane.setRowIndex(label, rowIndex);
label.setId(labelname+columnIndex);
label.setText("test");

gridPane.getChildren().add(label);
}

}
Labels labels = new Labels();

for(int i2=0; i2<13; i2++){
labels.addLabel("label", i2, 3);
}

GridPane gridPane = labels.getGridPane();
// TODO: display gridPane

关于java - 用标签填充 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com