2012-10-10 3 views
6

GridPane에는 1 자루의 레이블이 있습니다.JavaFx GridPane - 요소 가운데 놓는 방법

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

가 어떻게 문자를 중심으로 수

다음

image http://s1.directupload.net/images/121010/xu8o79sr.jpg

코드입니다 : 여기

은 이미지입니다?

나는 이것을 HBox에 넣고 간격을 둡니다. 나는 CENTER이라는 레이블의 textAlignment을 만들려고했지만 물론 작동하지 않습니다.

나는이 또한 시도 :

gp.setAlignment(Pos.CENTER); 

아이디어를 사람을 했습니까? 감사!

답변

10

아, 간단했습니다. 나는 틀린 장소에서 정렬을했다. 이를 추가하면 다음과 같은 작업이 수행됩니다.

tmp[currArrPos].setAlignment(Pos.CENTER); 

감사합니다.

관련 문제