【问题标题】:ScrollPane scroll does not work with other nodesScrollPane 滚动不适用于其他节点
【发布时间】:2019-08-13 23:03:46
【问题描述】:

我想将 StackPane 作为我的根窗格。我想在场景中间有一个scrollPane作为一个小盒子,在容器下方有两个按钮。

我试图通过编写以下代码来实现它:

 private StackPane root = new StackPane();
    private Scene scene = new Scene(root, 1366, 768);

    public ContinueScreen() {
        Button button1 = new ButtonBuilder("My Button1").setPrefWidth(200).build();
        Button button2 = new ButtonBuilder("My Button2").setPrefWidth(200).build();
        Button button3 = new ButtonBuilder("My Button3").setPrefWidth(200).build();
        Button button4 = new ButtonBuilder("My Button4").setPrefWidth(200).build();
        Button button5 = new ButtonBuilder("My Button5").setPrefWidth(200).build();
        Button button6 = new ButtonBuilder("My Button6").setPrefWidth(200).build();

        VBox vBox = new VBox(5);
        vBox.getChildren().addAll(button1, button2, button3, button4, button5, button6);

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(vBox);
        scrollPane.setPannable(true);
        scrollPane.setMaxSize(500, 180);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);

        root.getChildren().add(scrollPane);
        StackPane.setAlignment(scrollPane, Pos.CENTER);

    }

您可能会注意到,代码确实可以正常工作,但是直到我添加了我正在谈论的下面的按钮。我在 HBox 中将这些按钮添加为

        HBox hBox = new HBox(5);
        hBox.setAlignment(Pos.BOTTOM_CENTER);
        hBox.getChildren().addAll(new Button("cat"), new Button("dog"));
        root.getChildren().addAll(hBox);

现在滚动窗格和两个按钮都显示了。但是,由于某种原因,滚动窗格现在停止工作。显示滚动窗格及其内容,但水平或垂直滚动​​,它们都不起作用。任何人都知道为什么会发生这种情况以及如何解决?

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    当您使用 StackPane 作为 root 时,它会将节点堆叠在一起,因此顶部窗格是 HBox 而不是 ScrollPane,因此您无法使用它。

    使用 BorderPane 或 VBox 试试看。

            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 1366, 768);
            root.setCenter(scrollPane);
            HBox hBox = new HBox(5);
            hBox.getChildren().addAll(new Button("cat"), new Button("dog"));
            root.setBottom(hBox);
    

    【讨论】:

    • 谢谢,但我有一个问题。当我使用 BorderPane 作为根时,我无法将其居中到我正在谈论的位置。它就在滚动窗格的下方。我提到了其他一些问题,但这没有帮助。这是我的代码:pastebin.com/DmPqur0B
    • 你试过设置hBox.setAlignment(Pos.BASELINE_CENTER);
    • 它似乎也不起作用。它仅稍微向上移动,但仍与场景的左下角对齐。
    • @user3845934 不是BorderPane.setAlignment 你需要使用hBox.setAlignment(Pos.BASELINE_CENTER);
    【解决方案2】:

    这里的问题是允许StackPane 调整HBox 的大小,它确实如此。 HBox 涵盖了防止鼠标事件到达ScrollPane 的完整场景。您可以通过为HBox 着色来轻松看到这一点:

    hBox.setStyle("-fx-background-color: rgba(100%, 0%, 0%, 0.5)");
    

    最简单的解决方案是将HBox 设置为仅接收非(完全)透明区域的事件:

    hBox.setPickOnBounds(false);
    

    但是,您也可以设置 HBox 以占用适合内容首选大小所需的空间,并通过 StackPane 进行对齐:

    hBox.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
    hBox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
    StackPane.setAlignment(hBox, Pos.BOTTOM_CENTER);
    // hBox.setAlignment(Pos.BOTTOM_CENTER);
    

    请注意,像这样使用 StackPane 不会为您提供响应式 GUI:如果您将窗口大小调整到足够小的高度,按钮将覆盖 ScrollPane

    使用BorderPane 或将StackPaneHBox 包装在VBox 中并将VBox.vgrow 设置为Priority.ALWAYS 可能会带来更好的运气StackPane...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多