JavaFX无法使GridPane适合BorderPane的中心 [英] JavaFX, can't get GridPane to fit in the center of BorderPane

查看:220
本文介绍了JavaFX无法使GridPane适合BorderPane的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个数独应用程序,但我遇到的问题阻碍了我的进步。我正在尝试做的是创建这个我用循环创建的数独板,它将一个Label添加到StackPane中,然后StackPane进入GridPane的指定位置。所以我试图强制这个板有一个固定的尺寸,并没有超过这一点。

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.

我花了整整一个晚上研究和尝试不同的事情,例如将GridPane放入AnchorPane并尝试将AnchorPane放在BorderPane的中心,但它当你为数独板使用太多行和列时,似乎总是超出窗口外。

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.

我要做的是获得一个设定的大小对于所有电路板,所以如果需要,它会被拉伸到那一点,或者如果需要它会收缩。附图中使用的是BorderPane,中间放置了一个GridPane。您可以看到16x16非常适合,而25x25只是在窗外。

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.

帮助将非常受欢迎。

尝试创建25x25时窗口的图示。

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

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

尝试创建时窗口的样子图片一个16x16。
这也是使用insets将它推向顶部,所以它看起来不太合适。

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

推荐答案

您需要做的就是设置网格约束以满足您的需求。为此,将标签放在 GridRow 中(不需要 StackPane )并将其vAlignment和hAlignment设置为Center。然后将标签的vGrow和hGrow设置为 Priority.ALWAYS

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);
    }
}

这篇关于JavaFX无法使GridPane适合BorderPane的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆