HBox 旋转 90 度时不会填充父级 GridPane

问题描述 投票:0回答:1

我已经创建了一个网格窗格,如图所示,并且在该网格窗格中,我为顶行添加了多个 HBox,我必须旋转 HBox,但当我这样做时,由于某种原因,HBox 不再填充网格窗格。

我尝试手动更改 HBox 的高度,但这也会更改 HBox 所在网格窗格的单元格的宽度。

我想知道是否有人知道如何使旋转后的 HBox 仍然填充其父级。

橙色方块是模拟大富翁程序中的 HBox。

所以在这里我将通过名称

HBox
定义我自己的
StreetTile
类,它是图像中的橙色图块:

public class Tile extends HBox {

    public Tile(int orientation){
        //orientation = 0,1,2 of 3
        setAlignment(Pos.CENTER_RIGHT);
       
        setRotate(90*orientation);
    }
}

public class RectangleTile extends Tile{
    public RectangleTile(int orientation) {
        super(orientation);
        setMaxHeight(66);
        setPrefHeight(66);
        setFillHeight(true);

        setMaxWidth(128);
        setPrefWidth(128);

        setStyle("-fx-background-color: ORANGE");
    }
}

public class StreetTile extends RectangleTile{
    public StreetTile(String Name, Color color, int orientation) {
        super(orientation);
        Label label = new Label(Name);
        label.setAlignment(Pos.CENTER);


        getChildren().add(label);
        getChildren().add(new Rectangle(22,66, color));
        //The rectangle represents the monopoly street color
    }
}

然后我只需将其添加到我的网格窗格中,该网格窗格仅将其对齐设置为居中。

javafx layout gridpane hbox
1个回答
0
投票

您想要使用节点变换后的边界进行布局计算。为此,请将节点包装在

Group
:

node.setRotate(90);
Group group = new Group(node);

组的布局边界现在与旋转节点的边界匹配。

来自

Group
文档(强调我的):

应用于组的任何变换、效果或状态都将应用于该组的所有子组。此类变换和效果不会包含在该组的布局边界中,但是,如果直接在此组的子级上设置变换和效果,则它们将包含在该组的布局边界中。

© www.soinside.com 2019 - 2024. All rights reserved.