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

时间:2012-02-11 18:02:06

标签: 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"/>

一切正常。

                                                                                                                                                                                                                

我有一个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>

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

1 个答案:

答案 0 :(得分:4)

ScrollPane没有属性children,其content类型为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>