我的GameBoard需要的GridPane(应该容纳模拟器地形所具有的“平铺”量)无法正确地容纳到ScrollPane中。

围绕层次结构转移并使用彩色容器来确定问题,这无助于解决问题本身。

包含GameBoard的主容器FXML



    <SplitPane dividerPositions="0.5" BorderPane.alignment="CENTER">
                    <items>
                        <AnchorPane>
                            <children>
                                <TextArea layoutX="1.0" layoutY="-14.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                            </children>
                        </AnchorPane>
                      <ScrollPane prefWidth="800" prefHeight="500" hbarPolicy="ALWAYS" vbarPolicy="ALWAYS" >

                      <GameBoardCC fx:id="gameBoard" >

                      </ScrollPane>
                    </items>
                </SplitPane>




自定义控件本身:



<fx:root xmlns:fx="http://javafx.com/fxml/1" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.221">

  <GridPane fx:id="gameBoard"
            style="-fx-background-color: green">
  </GridPane>

</fx:root>




基本上是添加这些项目的代码,我敢肯定我从Stackoverflow中得到了大部分

 for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                // create node
                TerrainNode node = new TerrainNode ("Item " + i + "/" + j, i * 100, j *100, 100, 100);
                node.setOpacity (0.5);
                // add node to group
                gameBoard.getChildren ( ).add (node);
            }
        }


滑块行为的图片,由于某种原因,这些节点没有添加到Gridpane中,而是添加到了Gridpane中?

Pictures of the Slider Situation

我希望将滑块“卡住”在50%的位置上(尽管这不是我现在主要关注的问题),并且由于我的模拟应该具有可变的,固定瓷砖大小的瓷砖网格大小,所以滚动条应显示在例如20x20的网格,或者将来客户端太小时。

最佳答案

按照@fabian指定,让GridPane做它的事情。 JavaFX提供的每个布局窗格均具有特定功能。我建议您浏览不同类型的布局窗格,并了解其基本用途。请参考下图中的所有绿色框:)
java - JavaFX容器结构滚动 Pane-LMLPHP

当然,您选择的布局(GridPane)是正确的布局。但是执行是不正确的。 GridPane根据提供的列/行索引对其子级进行布局。您无需处理那里的布局定位。

以下是该想法的简要示例:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import java.util.stream.Stream;

public class GameBoardDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        ToggleGroup tg = new ToggleGroup();
        RadioButton size1 = new RadioButton("5 X 5");
        RadioButton size2 = new RadioButton("5 X 10");
        RadioButton size3 = new RadioButton("10 X 10");
        RadioButton size4 = new RadioButton("10 X 15");
        RadioButton size5 = new RadioButton("15 X 15");
        Stream.of(size1, size2, size3, size4, size5).forEach(rb -> {
            rb.setToggleGroup(tg);
            rb.setId(rb.getText());
        });
        GridPane options = new GridPane();
        options.setHgap(10);
        options.addRow(0, size1, size2, size3, size4, size5);

        GridPane board = new GridPane();
        board.setPrefSize(800, 500);
        ScrollPane scrollBoard = new ScrollPane(board);
        VBox.setVgrow(scrollBoard, Priority.ALWAYS);

        tg.selectedToggleProperty().addListener((obs, old, val) -> {
            RadioButton rb = (RadioButton) val;
            double rows, columns;
            switch (rb.getText()) {
                case "5 X 5":
                    rows = 5;
                    columns = 5;
                    break;
                case "5 X 10":
                    rows = 5;
                    columns = 10;
                    break;
                case "10 X 10":
                    rows = 10;
                    columns = 10;
                    break;
                case "10 X 15":
                    rows = 10;
                    columns = 15;
                    break;
                default:
                    rows = 15;
                    columns = 15;
            }
            board.getChildren().clear();
            for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
                for (int columnIndex = 0; columnIndex < columns; columnIndex++) {
                    Rectangle node = new Rectangle(100, 100);
                    node.setFill(Color.LIGHTBLUE);
                    node.setStroke(Color.BLACK);
                    StackPane terrainNode = new StackPane(node, new Label("Item " + rowIndex + "-" + columnIndex));
                    board.add(terrainNode, columnIndex, rowIndex);
                }
            }
        });
        VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setSpacing(15);
        root.getChildren().addAll(options, scrollBoard);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("BoardGame");
        primaryStage.show();
    }
}

关于java - JavaFX容器结构滚动 Pane ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58756198/

10-11 17:56