javafx Hbox 旋转 90 度时不会填充父级 Gridpane

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

我已经创建了一个网格窗格,如图所示,并且在该网格窗格中添加了多个 HBox,但对于顶行,我必须旋转 HBox,但当我这样做时,由于某种原因,HBox 不再填充网格窗格。 我尝试手动更改 HBox 的高度,但这也会更改 HBox 所在网格窗格的单元格的宽度, 我想知道是否有人知道如何使旋转的 HBox 仍然填充其父级, 我已经为此工作了两天,我的截止日期是一周内,所以我将永远感激任何能够解决这个问题的人 The orange tiles are the HBoxes in a program ment to emulate Monopoly

所以在这里我将通过 StreetTile 的名称定义我自己的 HBox 类,即图像中的橙色图块

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 rotation gridpane hbox
1个回答
0
投票

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

Group
:

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

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

来自

Group
文档(强调我的):

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

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