JavaFX 8 - 定位 HBox 的文本垂直中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26373130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX 8 - Positioning Text Vertical Center of HBox
提问by Sameer
I am having a very difficult time positioning Text in an HBox. I am able to set the Horizontal Alignment of the Text, but I am not able to set the Vertical Alignment of the Text. I am trying to set the Text in the Vertical Center of the Hbox, however it is appearing at the top.
我很难在 HBox 中定位文本。我可以设置文本的水平对齐方式,但无法设置文本的垂直对齐方式。我试图在 Hbox 的垂直中心设置文本,但它出现在顶部。
Any help would be much appreciated.
任何帮助将非常感激。
// Create The Updates Progress Bar At The Bottom Of The Window
HBox checkUpdatesBar = new HBox();
checkUpdatesBar.setId("checkUpdatesBar");
checkUpdatesBar.setPrefSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMinSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMaxSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
// Create The 'Check For Updates...' Text
Text checkForUpdates = new Text();
checkForUpdates.setFont(Font.font("Arial Black", FontWeight.BLACK, 15));
checkForUpdates.setWrappingWidth(CHECK_PANE_WIDTH / 2.8);
checkForUpdates.setFill(Color.WHITE);
checkForUpdates.setTextAlignment(TextAlignment.CENTER);
checkForUpdates.setText("Checking For Updates ...".toUpperCase());
checkUpdatesBar.getChildren().add(checkForUpdates);
My code produces the following: http://puu.sh/ccEkp/41a26038a4.png
我的代码产生以下内容:http: //puu.sh/ccEkp/41a26038a4.png
I like my Horizontal Positioning. I know its not in the middle because I set it to be that way. However, I just care about the Vertical Positioning.
我喜欢我的水平定位。我知道它不在中间,因为我将它设置为那样。但是,我只关心Vertical Positioning。
I have tried the following separately:
我分别尝试了以下方法:
checkForUpdates.setLayoutY(20);
checkForUpdates.setY(20);
I chose the number 20 because I wanted to move the Text 20 pixels down from the top of the HBox. I calculated this value in Photoshop. Though if there is a better way for setting the Text to the Vertical Center of the HBox, please do enlighten me.
我选择数字 20 是因为我想将文本从 HBox 顶部向下移动 20 个像素。我在 Photoshop 中计算了这个值。虽然如果有更好的方法将文本设置为 HBox 的垂直中心,请赐教。
Thanks again for all the help!
再次感谢所有的帮助!
采纳答案by ItachiUchiha
If you just want to position the text in the center of the HBox
, you can use the alignment property
of the HBox
to do it.
如果您只想将文本放置在 的中心HBox
,您可以使用alignment property
的HBox
来做到这一点。
Set the alignment as CENTER_LEFT
, to place the children, starting for the CENTER of the HBox's vertical space
将对齐设置为CENTER_LEFT
, 以放置子项,从 HBox垂直空间的 CENTER 开始
checkUpdatesBar.setAlignment(Pos.CENTER_LEFT);