FlowPane 中的 JavaFX 矩形

JavaFX Rectangles inside FlowPane

我在绘制这张图片时遇到了问题(在 link 下方)我在流程窗格的中间有他的,但只有一半!如何绘制全图>?

http://s16.postimg.org/lrus832dx/Capture2.png

http://s27.postimg.org/hsujjp1s3/Capture1.png

好的,我想我现在明白你的问题了。我建议你改用 GridPane,然后你可以这样做:

@Override
public void start(Stage primaryStage) {

    GridPane root = new GridPane();
    int rows = 2;
    int columns = 2;
    int rectangleWidth = 50;
    int rectangleHeight = 50;

    root.setMaxSize(rectangleWidth*columns, rectangleHeight*rows);

    for(int row = 0; row < rows; row++)
    {
        for(int col= 0; col< columns; col++)
        {
            Rectangle rectangle = new Rectangle(rectangleWidth, rectangleHeight);
            root.add(rectangle, col, row);

            if(row % 2 == 0)
            {
                rectangle.setFill(col % 2 == 0 ? Color.ORANGE : Color.BLACK);
            }
            else
            {
                rectangle.setFill(col % 2 != 0 ? Color.ORANGE : Color.BLACK);
            }
        }
    }

    Scene scene = new Scene(new StackPane(root), 700, 400);
    primaryStage.setScene(scene);
    primaryStage.show();
}

我们需要将包含 RectangleGridPane 包裹在 StackPane 中,使其在其父项中居中。