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

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

问题描述

我正在开发数独应用程序,但是遇到了阻碍我进步的问题.我想做的是用循环创建此数独板,从而在LabelPane中添加一个Label,然后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时窗口外观的图片.这也使用嵌入将其更多地推向顶部,因此看起来并不太错.

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

推荐答案

您要做的就是设置网格约束以适应您的需求.为此,将 Labels 放入 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天全站免登陆