简体   繁体   中英

Javafx Pane “forgets” layout

I have a function that creates and returns Pane, which is stored elsewhere. 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:

在此处输入图片说明

Why does the stackpane 'fix' my issue, and what should be done to ensure that all panes will be represented properly? 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. In the context of this question, the StackPane I use only has one child, the 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.

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.

Since you have a stage at 600 - the tile pane can't be limited to 100....

StackPane will layout it's children centered, that's why it looks correct. If you placed it just on a Pane, then your TilePane would be in the top left corner....

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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