Is there an easier (lighter) way to center text in a “zone” with JavaFX 8?

前端 未结 1 479
面向向阳花
面向向阳花 2021-01-25 07:06

So, first things first, I have achieved what I wanted, and that is, display some amount of text in the center of a \"zone\". Screenshot (upper right corner):

1条回答
  •  时光取名叫无心
    2021-01-25 07:33

    Use a StackPane rather than a BorderPane. By default a StackPane will center any node you put in it. To me, this is the simplest, most intuitive solution.

    
        
    

    Another way to handle it is to set constraints on the label itself so that the label will expand to fill a resizable area and center itself within the area:

    
       
          
          
    
    

    If it is a multi-line label (e.g. it has \n new lines in the text or the available width forces a wrap because you constrain the width and set wrapText="true"), then you need an additional setting to center the multi-line text within the label: textAlignment="true". textAlignment is different from alignment; in particular, textAlignment applies only to multi-line controls.

    0 讨论(0)
提交回复
热议问题