[英]Center text in GridPane
I am a beginner at Java and I have just recently started to take a look at GUI and JavaFX. 我是Java的初学者,最近才开始研究GUI和JavaFX。 And at the moment I am stuck trying to center a text inside a GridPane and would need som help with this, and an explaination to what I am doing wrong.
目前,我一直试图在GridPane中居中放置文本,因此需要Som帮助,以及对我做错事的解释。
This is my code: 这是我的代码:
GridPane grid = new GridPane();
grid.setMinSize(100, 100);
Text text = new Text("Hello World!");
text.setTextAlignment(TextAlignment.CENTER);
grid.add(text, 0, 1);
grid.setStyle("-fx-background-color: #D8BFD8;");
This is not working (the text does not center), so I asume I am doing something wrong. 这是行不通的(文本不会居中),所以我假设我做错了什么。 But I cant understand what I am doing wrong?
但是我不明白我在做什么错?
I would use Labels instead of Text, that way you can set the horizontal alignment: 我将使用标签而不是文本,这样您可以设置水平对齐方式:
Label label = new Label("Hello World!");
gridpane.add(label, 0, 1);
GridPane.setHalignment(label, HPos.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.