JavaFX, can't get GridPane to fit in the center of BorderPane

[亡魂溺海] 提交于 2020-01-05 07:18:44

问题


I'm working on a sudoku application but I'm having an issue which hinders my advancement. What I'm trying to do is to make this sudoku-board that I have created with loops, which adds a Label into a StackPane and that StackPane goes into it's designated spot into the GridPane. So I'm trying to force this board have a set size and not grow beyond that point.

I've spent the whole evening researching and trying different things, for example putting the GridPane into an AnchorPane and trying to fit the AnchorPane in the center of BorderPane, but it always seems to exceed outside of the window, when you use too many rows and columns for the sudoku board.

What I'm trying to do is to get a set size for all the boards, so if needed it gets stretched to that point, or if needed it gets shrinked. The picture attached is using a BorderPane with a GridPane put in the middle. You can see that the 16x16 fits perfectly while the 25x25 just goes outside the window.

Help would be highly appreciated.

Picture of how the window looks like when trying to create a 25x25.

http://i.imgur.com/BrJrpiY.png

Picture of how the window looks like when trying to create a 16x16. This is also using insets to push it more towards the top so it doesn't look so much out of place.

http://i.imgur.com/7a0QJx8.jpg


回答1:


All you have to do, is set the Grid Constraints to fit your needs. To do so put the Labels in a GridRow (no need for a StackPane) and set their vAlignment and hAlignment to Center. Then set vGrow and hGrow of label to Priority.ALWAYS

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane gridPane = createMainGrid(2, 2);
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private GridPane createMainGrid(int rows, int columns) {
        GridPane grid = new GridPane();

        for (int colIdx = 0; colIdx < columns; colIdx++) {
            for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
                GridPane innerGrid = createGrid(4, 4);
                grid.add(innerGrid, colIdx, rowIdx);
                GridPane.setConstraints(innerGrid, colIdx, rowIdx, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
            }
        }
        return grid;
    }

    private GridPane createGrid(int rows, int columns) {
        GridPane grid = new GridPane();

        Random random = new Random();

        for (int colIdx = 0; colIdx < columns; colIdx++) {
            for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
                Label label = new Label(String.valueOf(random.nextInt(rows * columns)));
                label.setMinSize(30, 30);
                label.setAlignment(Pos.CENTER);
                grid.add(label, colIdx, rowIdx);
                GridPane.setConstraints(label, colIdx, rowIdx, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
            }
        }
        return grid;
    }

    public static void main(String[] args) {
        launch(args);
    }
}


来源:https://stackoverflow.com/questions/36949054/javafx-cant-get-gridpane-to-fit-in-the-center-of-borderpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!