简体   繁体   English

Javafx窗格“忘记”布局

[英]Javafx Pane “forgets” layout

I have a function that creates and returns Pane, which is stored elsewhere. 我有一个创建并返回Pane的函数,该函数存储在其他位置。 This is shown when a user presses a button. 当用户按下按钮时显示。

public Pane createBoard(Board d){
        TilePane tilepane = new TilePane();
        pane.setPrefColumns(8);
        pane.setPrefRows(8);
        pane.setMaxSize(400,400);
        pane.setMinSize(400,400);

        for(Cell c : d.getCells()){
            pane.getChildren().add(c);
        }    

        return tilepane;
    }

Passing this object to the scene returns this: 将此对象传递到场景将返回以下内容:

在此处输入图片说明

Yet, if I add it to a StackPane first, I get this: 但是,如果我首先将其添加到StackPane中,则会得到以下信息:

在此处输入图片说明

Why does the stackpane 'fix' my issue, and what should be done to ensure that all panes will be represented properly? 为什么stackpane可以“解决”我的问题,并且应该采取什么措施来确保正确显示所有窗格? Is this expected behavior? 这是预期的行为吗?

If I use 如果我用

Pane tilepane = createBoard(new Board());
StackPane stackpane = new StackPane();
stackpane.getchildren().add(tilepane);
Scene scene = new Scene(stackpane,600,600);

I get the expected results. 我得到了预期的结果。

If I use: 如果我使用:

Pane tilepane = createBoard(new Board());
Scene scene = new Scene(tilepane,600,600);

I get the first image. 我得到第一张图片。


Edits: 编辑:

A StackPane basically places all of the child nodes directly on top of each other. StackPane基本上将所有子节点直接放置在彼此之上。 In the context of this question, the StackPane I use only has one child, the TilePane. 在这个问题的上下文中,我使用的StackPane只有一个孩子,即TilePane。

Example: 例:

Non-working: 非工作:

 public void start(Stage primaryStage) {   

        TilePane tp = new TilePane();
        tp.setPrefColumns(5);
        tp.setPrefRows(5);
        tp.setMaxSize(100, 100);

        ArrayList<Rectangle> rectangles = new ArrayList();

        for(int i = 0;i<25;i++){
        rectangles.add(new Rectangle(20,20));
        }

        for(int i = 0;i<25;i++){
        rectangles.get(i).setFill(Color.rgb(i*2, i*9, i*6));
        }
        tp.getChildren().addAll(rectangles);

        Scene scene = new Scene(tp, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Working: 工作:

 public void start(Stage primaryStage) {   


        TilePane tp = new TilePane();
        tp.setPrefColumns(5);
        tp.setPrefRows(5);
        tp.setMaxSize(100, 100);

        ArrayList<Rectangle> rectangles = new ArrayList();

        for(int i = 0;i<25;i++){
        rectangles.add(new Rectangle(20,20));
        }

        for(int i = 0;i<25;i++){
        rectangles.get(i).setFill(Color.rgb(i*2, i*9, i*6));
        }
        tp.getChildren().addAll(rectangles);

        StackPane stack = new StackPane();                            <-- Added
        stack.getChildren().add(tp);                                 <--Added
        Scene scene = new Scene(stack, 600, 600);                    <-- Changed to stack from tp
        primaryStage.setScene(scene);
        primaryStage.show();
    }

This is because when you resize the Stage the TilePane is also adjusted to be the size of the Stage since there is nothing holding it. 这是因为在调整舞台大小时,由于没有任何支撑,因此TilePane也被调整为舞台的大小。

When you wrap it in a StackPane, then it can respect the max since the StackPane will resize to the new stage size (in this example 600x600) leaving the TilePane alone at 100X100 centered on the StackPane. 当您将其包装在StackPane中时,它可以遵守最大值,因为StackPane会调整为新的舞台大小(在本示例中为600x600),而TilePane单独放置在StackPane的中心为100X100。

Since you have a stage at 600 - the tile pane can't be limited to 100.... 由于您的舞台为600,因此不能将瓷砖窗格限制为100 ....

StackPane will layout it's children centered, that's why it looks correct. StackPane将布局以孩子为中心,这就是为什么它看起来正确的原因。 If you placed it just on a Pane, then your TilePane would be in the top left corner.... 如果仅将其放在窗格上,则TilePane将位于左上角。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM