Java 在 BorderPane 中居中对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33773179/
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 15:01:24  来源:igfitidea点击:

Center an object in BorderPane

javajavafx

提问by Dylan Czenski

In JavaFX, how to center an object in the top or bottom compartment in BorderPane layout?

在 JavaFX 中,如何在 BorderPane 布局的顶部或底部隔间中居中对象?

I have:

我有:

borderPane.setBottom(hBox);

And hBox appears on the left side of the bottom compartment of BorderPane borderPane.And I want it to be in the center of the bottom. Thanks.

并且 hBox 出现在 BorderPane 底部隔间的左侧borderPane。我希望它位于底部的中心。谢谢。

采纳答案by Uluk Biy

The default alignment for borderpane positions are:

边框位置的默认对齐方式是:

top: Pos.TOP_LEFT
bottom: Pos.BOTTOM_LEFT
left: Pos.TOP_LEFT
right: Pos.TOP_RIGHT
center: Pos.CENTER

To change this for different alignment use:

要针对不同的对齐方式更改此设置,请使用:

BorderPane.setAlignment(child, Pos.CENTER);
BorderPane.setMargin(child, new Insets(12,12,12,12)); // optional
borderPane.setBottom(child);

You may also change the child HBox alignment as:

您还可以将子 HBox 对齐方式更改为:

hBox.setAlignment(Pos.CENTER);

Refer to java API documentation for more info.

有关更多信息,请参阅 java API 文档。

回答by Collins Abitekaniza

You can use the Alignment method like

您可以使用 Alignment 方法,例如

borderPane.setBottom(hBox);
borderPane.setAlignment(hBox,Pos.CENTER);