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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:23:28  来源:igfitidea点击:

JavaFX 8 - Positioning Text Vertical Center of HBox

javajavafxjavafx-8

提问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 propertyof the HBoxto do it.

如果您只想将文本放置在 的中心HBox,您可以使用alignment propertyHBox来做到这一点。

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);

回答by Damien

If you are using SceneBuilder, select your HBox and in the Inspector section, change the

如果您使用的是 SceneBuilder,请选择您的 HBox 并在 Inspector 部分中,更改

Properties : Node : Alignment

属性:节点:对齐

value to CENTER_LEFT:

CENTER_LEFT 的值:

enter image description here

在此处输入图片说明