在JavaFx中创建可滚动的VBox

Kaa*_*ner 1 java javafx scrollbar

我通过VBox在右边使用创建了一个可折叠的侧栏BorderPane.在这个侧栏中,我显示所选类的源代码(称为气泡)作为列表,一个在另一个下面.每个气泡的宽度为300px,高度为700px.但是,当我向侧栏添加新气泡时,列表中的其他气泡开始缩小.而不是这个,我想在这个侧栏VBox中有一个滚动条.我怎么能做到这一点?

void initSideBarActions() {
    sideBar = new SideBarView(borderPane);
    borderPane.setRight(sideBar);

    String sourceCode = "public class FXMLDocumentController implements Initializable {\n" +
            "    @FXML\n" +
            "    AnchorPane apMain;\n" +
            "\n" +
            "    @Override\n" +
            "    public void initialize(URL url, ResourceBundle rb)\n" +
            "    {\n" +
            "         BubbleShape bs = new BubbleShape(\"Hello world!\");\n" +
            "         bs.setLayoutX(50.0);\n" +
            "         bs.setLayoutY(50.0);\n" +
            "\n" +
            "         BubbleShape bs2 = new BubbleShape(\"Bye world!\");\n" +
            "         bs2.setLayoutX(400);\n" +
            "         bs2.setLayoutY(400);\n" +
            "         apMain.getChildren().addAll(bs, bs2);\n" +
            "    }\n" +
            "}";

    BubbleView bubble1 = new BubbleView(sourceCode, "SampleClassOne");
    BubbleView bubble2 = new BubbleView(sourceCode, "SampleClassTwo");
    BubbleView bubble3 = new BubbleView(sourceCode, "SampleClassThree");

    sideBar.addNewBubble(bubble1);
    sideBar.addNewBubble(bubble2);
    sideBar.addNewBubble(bubble3);
}
Run Code Online (Sandbox Code Playgroud)

这是截图:

当前版本的屏幕截图

这是.fxml文件:

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

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.layout.StackPane?>

<BorderPane fx:id="borderPane" fx:controller="com.kaanburaksener.octoUML.src.controller.ClassDiagramController" xmlns:fx="http://javafx.com/fxml">
    <top>
        <VBox>
            <ToolBar fx:id="aToolBar" orientation="HORIZONTAL">
                <HBox fx:id="umlBox">
                    <Button text="Create" fx:id="createBtn"/>
                    <Button text="Enumeration" fx:id="enumBtn"/>
                    <Button text="Package" fx:id="packageBtn"/>
                    <Button text="Edge" fx:id="edgeBtn"/>
                    <Button text="Draw" fx:id="drawBtn"/>
                </HBox>
                <HBox fx:id="utilBox">
                    <Button text="Select" fx:id="selectBtn"/>
                    <Button text="Move" fx:id="moveBtn"/>
                </HBox>
                <HBox fx:id="undoBox">
                    <Button text="Delete" fx:id="deleteBtn"/>
                    <Button text="Undo" fx:id="undoBtn"/>
                    <Button text="Redo" fx:id="redoBtn"/>
                </HBox>
                <HBox fx:id="recognizeBox">
                    <Button text="Source Code" fx:id="sourceCodeBtn"/>
                    <Button text="Recognize" fx:id="recognizeBtn"/>
                    <Button text="Voice" fx:id="voiceBtn"/>
                </HBox>
            </ToolBar>
        </VBox>
    </top>
    <bottom>
        <StackPane fx:id="infoPane">
            <ToolBar fx:id="zoomPane">
                <Pane HBox.hgrow="ALWAYS" />
                <VBox alignment="CENTER">
                    <Slider fx:id="zoomSlider" min="10"  max="200" value="100"/>
                    <Label fx:id="zoomLabel" text="Zoom" />
                </VBox>
                <Pane HBox.hgrow="ALWAYS" />
            </ToolBar>
            <ColorPicker fx:id="colorPicker" StackPane.alignment="CENTER_LEFT"/>
            <Label fx:id="serverLabel" StackPane.alignment="CENTER_RIGHT"/>
        </StackPane>
    </bottom>
    <center>
        <ScrollPane fx:id="scrollPane" pannable="true" BorderPane.alignment="CENTER">
            <content>
                <Pane fx:id="drawPane" prefHeight="8000.0" prefWidth="8000.0">
                </Pane>
            </content>
        </ScrollPane>
    </center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

即使我已经阅读了这个问题(如何在JavaFX中创建可滚动的组件面板?),我也无法将解决方案应用于我的项目.

Jam*_*s_D 5

只需将侧栏包裹在滚动窗格中即可.如果您希望它只能垂直滚动,请致电setFitToWidth(true).

sideBar = new SideBarView(borderPane);

ScrollPane sideBarScroller = new ScrollPane(sideBar);
sideBarScroller.setFitToWidth(true);

borderPane.setRight(sideBarScroller);
Run Code Online (Sandbox Code Playgroud)