2012-10-10 12 views
6

मेरे पास 0 -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); 

मैं पात्रों कैसे केंद्रित कर सकता है:

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

यहाँ कोड है:

यहाँ एक छवि है?

मैंने उन्हें HBox में रखा है और उन्हें रिक्ति दी है। मैंने लेबल के textAlignment को CENTER पर बनाने की कोशिश की, लेकिन यह बिल्कुल काम नहीं करता है।

मैं यह भी कोशिश की:

gp.setAlignment(Pos.CENTER); 

किसी को भी एक विचार है? धन्यवाद!

उत्तर

10

ओह, यह आसान था। मैंने गलत जगह पर संरेखण किया था। यह नौकरी करेगा:

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

वैसे भी धन्यवाद।

संबंधित मुद्दे