如何在 javafx2 中使用 FXML 创建 ScrollBar?

ril*_*l3y 4 java user-interface javafx fxml

编辑 我正在寻找“ScrollPane”而不是 ScrollBar。

<ScrollPane  fitToWidth="true" fx:id="sasd">
<content>
    <VBox prefWidth="200" alignment="center" fx:id="Left">
        <children>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
Run Code Online (Sandbox Code Playgroud)

一切正常。

我有一个 VBox,我想向其中添加许多标签。我希望 VBox 能够在添加这些行时“滚动”。

现在这就是我的 FXML 的样子。它在一个 BorderPane 中。但是我省略了不相关的部分。

<left>
    <VBox prefWidth="200" alignment="center" fx:id="Left">
        <children>
            <ScrollBar orientation="VERTICAL" fx:id="sasd">
              <children>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x121 y13 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x10 y113 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x121 y13 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x10 y113 z23"/>
             <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
             </children>
             </ScrollBar>
        </children>
    </VBox>
Run Code Online (Sandbox Code Playgroud)

但是,这给了我错误并编译并且不起作用。我也试图移除孩子。没有运气..有什么想法吗?我发现在 Javafx 2.0 中很难找到“FXML”方式来做事。使用代码非常简单...

Ser*_*nev 5

ScrollPane没有 property children,它有contenttype Node。下一个 fxml 将为您工作:

<ScrollPane fx:id="sasd">
    <content>
        <VBox prefWidth="200" alignment="center" fx:id="Left">
            <children>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x121 y13 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x10 y113 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x121 y13 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x10 y113 z23"/>
                <Label alignment="center" fx:id="gcProgramLine" text="g0x100 y123 z23"/>
            </children>
        </VBox>
    </content>
</ScrollPane>
Run Code Online (Sandbox Code Playgroud)

  • 有人会认为 `ScrollPane` 是 `Pane` 的子类,但事实并非如此。相反,层次结构(从 Java 8 开始)是 `ScrollPane` &lt; `Control` &lt; `Region` &lt; `Parent` &lt; `Node`(其中 &lt; 表示扩展)。这些类都没有像“Pane”那样采用受保护的方法“Parent#getChildren()”并使其公开。 (4认同)