使用JavaFX

时间:2016-04-19 10:32:14

标签: java javafx alignment fxml scrollpane

您好我有以下情况:

<ScrollPane xmlns:fx="http://javafx.com/fxml" fx:controller="MenuController" fx:id="menuPane" stylesheets="/fxml/styles/menu_style.css" fitToWidth="true" fitToHeight="true" hbarPolicy="ALWAYS" vbarPolicy="ALWAYS">
  <VBox alignment="CENTER">
    <HBox alignment="CENTER">
      <VBox fx:id="menuView">
        <elements></elements>
      </VBox>
    </HBox>
  </VBox>
</ScrollPane>

看起来像这样:

对齐全屏

Full Screen with alignment

但是当你在宽度上缩小时,滚动窗格不起作用。

调整大小

Resized with alignment

当我将fitToWidth的值从true更改为false时,我得到:

滚动全屏

Full screen with scrolling

所以中心没有水平对齐。但滚动工作正常:

通过滚动调整

Resized with scrolling

是否有可能同时对齐和滚动工作?

奇怪的是,高度滚动和垂直对齐工作正常。

无法添加超过2张图片,对不起。

1 个答案:

答案 0 :(得分:1)

当视口宽度小于内容宽度时,当然内容的位置由水平滚动条的位置决定,这可能是你想要的。

当视口宽度大于内容宽度时,内容的子节点的位置由内容的布局以及您在其上设置的任何对齐方式确定。视口中内容本身的位置是视口布局的函数,您对此的控制有限(据我所见)。

因此,一个选项只是将内容的最小宽度绑定到视口的实际宽度,从而强制内容至少与视口一样大。您可以在FXML中执行此操作:

<ScrollPane xmlns:fx="http://javafx.com/fxml" fx:controller="MenuController" fx:id="menuPane" stylesheets="/fxml/styles/menu_style.css" fitToWidth="true" fitToHeight="true" hbarPolicy="ALWAYS" vbarPolicy="ALWAYS">
  <VBox alignment="CENTER" minWidth="${menuPane.viewportBounds.width}">
    <HBox alignment="CENTER">
      <VBox fx:id="menuView">
        <elements></elements>
      </VBox>
    </HBox>
  </VBox>
</ScrollPane>