JavaFx - 2 个液柱和 ScrollPane 上的固定中心

JavaFx - 2 liquid columns and fixed center on ScrollPane

我如何在 SceneBuilder 中将左侧液体左列、固定中心(这里我将放置 GridPane - 主要部分)和液体右列放在 ScrollPane 上。这个想法很简单 - 如果 window 的宽度 > 主要部分的宽度,那么主要部分必须位于 window 的中心。如果 window 的宽度 < 主要部分的宽度,则显示水平条。我想获得 scenebuilder 的解决方案,因为我需要很多这样的表格,通过 SB 比通过代码更容易做到

创建容器的自定义实现要容易得多,例如 VBox,您可以在其中操作 visiblemanaged 属性'horizontal bar' 取决于容器的宽度。

您可以在 Scene Builder

中使用此容器

您可以使用 StackPane,因为它始终将其子元素居中。在窗格内,您将创建一个 ScrollPane,其中 prefWidth 设置为与内容中的 prefWidth 相同的值。同时将 maxWidth 设置为 USE_PREF_SIZE.

然后创建内容窗格并将其放入 ScrollPane

示例输出 fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
           minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
           xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ScrollPane fitToHeight="true" maxWidth="-Infinity" minWidth="50.0" 
                  prefHeight="200.0" prefWidth="550.0">
         <content>
            <GridPane gridLinesVisible="true" maxHeight="1.7976931348623157E308" 
                      maxWidth="-Infinity" minHeight="-Infinity" 
                      minWidth="-Infinity" prefHeight="500.0" 
                      prefWidth="550.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
            </GridPane>
         </content>
      </ScrollPane>
   </children>
</StackPane>