用标签填充GridPane [英] Filling GridPane with lables

查看:39
本文介绍了用标签填充GridPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个构造函数,我正在尝试用标签填充网格窗格.我碰到一堵砖墙,不知道怎么了.我需要在1行中创建13个标签.

I've got this constructor and I'm trying to fill up the gridpane with labels. I've hit a brick wall and have no idea what's wrong. I need to create 13 labels in 1 row.

构造函数:

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);
 }

推荐答案

您没有将 Label 添加到 GridPane 中.此外,您对每个 Label 使用新的 GridPane ,而永远不要在任何地方使用这些 GridPane .

You're not adding the Labels to the GridPane. Furthermore you use new GridPanes for every Label and never use those GridPanes anywhere.

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

这篇关于用标签填充GridPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆