2012-10-10 16 views
6

Ho uno GridPane pieno di etichette con 1 lettera.JavaFx GridPane - come centrare gli elementi

Ecco un'immagine:

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

Ecco il codice:

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

Come faccio a centrare i personaggi?

Li ho inseriti in un HBox e ho dato loro la spaziatura. Ho provato a creare lo textAlignment dell'etichetta su CENTER, ma ovviamente non funziona.

ho provato anche questo:

gp.setAlignment(Pos.CENTER); 

Qualcuno ha un'idea? Grazie!

risposta

10

oh, è stato facile. Ho fatto l'allineamento nel posto sbagliato. aggiungendo questo farà il lavoro:

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

grazie comunque.

Problemi correlati