簡體   English   中英

ScrollPane滾動不適用於其他節點

[英]ScrollPane scroll does not work with other nodes

我想將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);

現在,同時顯示了滾動窗格和兩個按鈕。 但是,滾動窗格現在由於某種原因停止工作。 顯示了滾動窗格及其內容,但是水平或垂直滾動​​條都無法使用。 有誰知道為什么會這樣以及如何解決?

當您使用StackPane作為根節點時,它將節點彼此堆疊,因此頂部窗格是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);

這里的問題是允許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或包裹StackPaneHBoxVBox和設置VBox.vgrowPriority.ALWAYSStackPane ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM