【问题标题】:Center an object in BorderPane在 BorderPane 中将对象居中
【发布时间】:2015-11-18 06:27:27
【问题描述】:

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

我有:

borderPane.setBottom(hBox);

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

【问题讨论】:

    标签: java javafx


    【解决方案1】:

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

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

    要更改此以用于不同的对齐方式:

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

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

    hBox.setAlignment(Pos.CENTER);
    

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

    【讨论】:

    • 不幸的是,这对我不起作用。我试图居中的标签仍显示为左对齐。
    • @MoritzSchmidt 添加:BorderPane.alignment="CENTER"
    【解决方案2】:

    你可以像这样使用Alignment方法

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

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 2015-04-15
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      相关资源
      最近更新 更多