【发布时间】:2012-09-30 17:33:10
【问题描述】:
我有一个GridPane,里面填满了 1 个字母的标签。
这是一张图片:
代码如下:
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,但这当然行不通。
我也试过这个:
gp.setAlignment(Pos.CENTER);
有人知道吗?谢谢!
【问题讨论】:
标签: java user-interface positioning javafx-2 gridpanel