简体   繁体   English

使用 JavaFX 将 ScrollPane 的内容居中,fitToWidth=false

[英]centering content of ScrollPane with fitToWidth=false using JavaFX

Hi I have following situation:您好,我有以下情况:

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

It looks like this:它看起来像这样:

Full Screen with alignment全屏对齐

全屏对齐

However when you make it smaller on width scrollpane is not working.但是,当您在宽度滚动窗格上缩小它时不起作用。

Resized with alignment调整大小并对齐

调整大小并对齐

When I changed value of fitToWidth from true to false I get:当我将 fitToWidth 的值从 true 更改为 false 时,我得到:

Full screen with scrolling全屏滚动

全屏滚动

so there is no horizontal alignment to center.所以没有水平对齐到中心。 However scrolling is working:但是滚动正在工作:

Resized with scrolling滚动调整大小

滚动调整大小

Is there a possibility to have alignment and scrolling working at the same time?是否有可能同时进行对齐和滚动?

It is strange that height scrolling and vertical alignment are working properly.奇怪的是,高度滚动和垂直对齐工作正常。

Cannot add more than 2 picture, sorry for that.不能添加超过 2 张图片,抱歉。

When the viewport width is smaller than the content width, then of course the position of the content is determined by the position of the horizontal scroll bar, which is presumably what you want.当视口宽度小于内容宽度时,当然内容的位置是由水平滚动条的位置决定的,这大概就是你想要的。

When the viewport width is bigger than the content width, then the position of child node of the content is determined by the layout of the content, and any alignment you set on it.当视口宽度大于内容宽度时,内容的子节点的位置由内容的布局以及您在其上设置的任何对齐方式决定。 The position of the content itself in the viewport is a function of the viewport's layout, and you have limited control over that (as far as I can see).视口中内容本身的位置是视口布局的函数,您对此的控制有限(据我所知)。

So one option is just to bind the minimum width of the content to the actual width of the viewport, forcing the content to be at least as big as the viewport.因此,一种选择是将内容的最小宽度绑定到视口的实际宽度,强制内容至少与视口一样大。 You can do this in FXML:您可以在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM