JavaFx GridPane - how to center elements

前端 未结 3 843
抹茶落季
抹茶落季 2021-01-04 10:25

I have a GridPane filled with 1-letter-labels.

Here is an image:

Here is the code:

int charSpacing = 1;
int charsInWid         


        
相关标签:
3条回答
  • 2021-01-04 11:13
    • You can use this GridPane.setHalignment(tmp[currArrPos], HPos.CENTER); Using GridPane to layout controls
    0 讨论(0)
  • 2021-01-04 11:23

    oh, that was easy. i did the alignment on the wrong place. adding this will do the job:

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

    thanks anyway.

    0 讨论(0)
  • 2021-01-04 11:29

    You can use the setAligment(Pos.CENTER) property of your element-

    or you can define a contraint to the GridPane that contains the elements

    <columnConstraints>
        <ColumnConstraints halignment="CENTER" />
    </columnConstraints>
    

    Example:

    <?import javafx.geometry.Insets?>
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.ColumnConstraints?>
    
    <GridPane fx:controller="app.graphics.Controller"
              xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
        <columnConstraints>
            <ColumnConstraints halignment="CENTER" />
        </columnConstraints>
    </GridPane>
    
    0 讨论(0)
提交回复
热议问题