简体   繁体   English

在JavaFx中将标签设置为GridPane

[英]Setting Labels to a GridPane in JavaFx

I have a GridPane called matrix in a FXML file, and in the controller I am adding a label to each cell of the GridPane, however I am not able to center that label in the GridPane. 我在FXML文件中有一个称为矩阵的GridPane,在控制器中,我向GridPane的每个单元格添加了一个标签,但是我无法在GridPane中居中放置该标签。 I have tried setting all kinds of alignments. 我尝试设置各种对齐方式。 Here is some of my code: This is all in a for loop by the way 这是我的一些代码:顺便说一下,这些都在for循环中

for (int i = 0; i < label.length; i++) {
            for (int j = 0; j < label[i].length; j++) {
               label[i][j] = new Label();
               label[i][j].setTextAlignment(TextAlignment.CENTER);
               label[i][j].setAlignment(Pos.CENTER);
               label[i][j].setCenterShape(true);
               label[i][j].setText("" + randomNumber);
               matrix.add(label[i][j], j, i);
            }
}

How would I horizontally center the label in matrix cell. 我将如何在矩阵单元格中水平放置标签。 Please Help. 请帮忙。 Thank you! 谢谢!

Try 尝试

GridPane.setHalignment(label[i][j], HPos.CENTER);

( Javadocs ) Javadocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM