2012-10-10 15 views
6

J'ai un GridPane rempli d'étiquettes de 1 lettre.JavaFx GridPane - comment centrer les éléments

est ici une image:

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

Voici le code:

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

Comment puis-je centrer les personnages? Je les ai mis dans un HBox et leur ai donné un espacement de

J'ai essayé de faire le textAlignment de l'étiquette à CENTER, mais cela ne fonctionne pas bien sûr.

J'ai essayé aussi:

gp.setAlignment(Pos.CENTER); 

Quelqu'un at-il une idée? Merci!

Répondre

10

oh, c'était facile. J'ai fait l'alignement au mauvais endroit. ajouter ceci fera l'affaire:

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

merci quand même.

Questions connexes