我有这个构造函数,我正在尝试用标签填充gridpane。 我碰到了一堵砖墙,不知道出了什么问题。 我需要在一行中创建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");
}
}
在控制器中循环:
for(int i2=0; i2<13; i2++){
Labels labels = new Labels("label", i2, 3);
}
答案 0 :(得分:1)
您没有将Label
添加到GridPane
。此外,您为每个GridPane
使用新的Label
,并且绝不会在任何地方使用这些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
答案 1 :(得分:0)
(始终发布mcve)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class LabelsRow extends Application {
GridPane gridPane;
@Override
public void start(Stage primaryStage) {
gridPane = new GridPane();
for(int i2=0; i2<13; i2++){
new Labels("label "+i2 , i2, 3);
}
Scene scene = new Scene(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
public class Labels {
Labels labels;
Labels(String labelname, int columnIndex, int rowIndex) {
Label label = new Label();
//gridPane.setColumnIndex(label, columnIndex);
//gridPane.setRowIndex(label, rowIndex);
gridPane.add(label, columnIndex, rowIndex);
label.setId(labelname+columnIndex);
label.setVisible(true);
label.setText(labelname);
}
}
public static void main(String[] args) {
launch(args);
}
}