JavaFx GridPane-如何居中元素 [英] JavaFx GridPane - how to center elements
本文介绍了JavaFx GridPane-如何居中元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个GridPane
充满1个字母的标签.
I have a GridPane
filled with 1-letter-labels.
这是一张图片:
这是代码:
int charSpacing = 1;
int charsInWidth = 28;
int charsInHeight = 16;
double charWidth = 15;
double charHeight = 20;
GridPane gp = new GridPane();
gp.setAlignment(Pos.CENTER);
Label[] tmp = new Label[charsInHeight*charsInWidth];
String text = "W";
int currArrPos = 0;
for(int y = 0; y < charsInHeight; y++) {
HBox hbox = new HBox(charSpacing);
for(int x = 0; x < charsInWidth; x++) {
tmp[currArrPos] = new Label(text);
tmp[currArrPos].setTextFill(Paint.valueOf("white"));
tmp[currArrPos].setMinHeight(charHeight);
tmp[currArrPos].setMinWidth(charWidth);
tmp[currArrPos].setMaxHeight(charHeight);
tmp[currArrPos].setMaxWidth(charWidth);
tmp[currArrPos].setStyle("-fx-border-color: white;");
hbox.getChildren().add(tmp[currArrPos++]);
if(x%2 == 0){
text = "I";
} else{
text = "W";
}
}
gp.add(hbox, 1, y);
}
guiDisplay.getChildren().add(gp);
如何使字符居中?
我将它们放在HBox
中并给了它们间距.我试图将标签的textAlignment
设置为CENTER
,但这当然不起作用.
I have put them in a HBox
and gave them spacing. I tried to make the textAlignment
of the label to CENTER
, but that doesn't work of course.
我也尝试过:
gp.setAlignment(Pos.CENTER);
有人有想法吗?谢谢!
推荐答案
哦,这很容易.我在错误的地方对准了.添加此功能即可完成工作:
oh, that was easy. i did the alignment on the wrong place. adding this will do the job:
tmp[currArrPos].setAlignment(Pos.CENTER);
还是谢谢.
这篇关于JavaFx GridPane-如何居中元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文