VBox中的按钮重叠

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

我试图添加按钮到VBox中包含的BoarderPane,但它们似乎重叠。 我的fxml文件中的VBox片段是

<VBox fx:id="leftPlayerPlayArea" alignment="CENTER" minHeight="0.0" minWidth="0.0" prefWidth="120.0" BorderPane.alignment="CENTER_LEFT">
    <BorderPane.margin>
        <Insets left="30.0" />
    </BorderPane.margin>
    <opaqueInsets>
        <Insets />
    </opaqueInsets>
    <padding>
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
    </padding>
</VBox>

当我在控制器类中添加按钮时,按钮很好地添加到我的Hbox中,但是它们没有很好地添加到我的Vbox中。这是图像:

垂直框

This is an image of the Vbox

HBox中

This is an image of the Hbox

最后,这是我如何创建这些按钮:

 private Button playerPromptButtonCreator(String buttonText, EventHandler<ActionEvent> event, int id) {
    Button button = createBtn(buttonText);
    button.setMinWidth(BTN_MAX_WIDTH);
    button.setMinHeight(BTN_MAX_HEIGHT);
    button.setOnAction(event);
    if (playerPositions.get(id) == PlayerPosition.LEFT){
        button.setRotate(90);
    }
    if (playerPositions.get(id) == PlayerPosition.RIGHT)
    {
        button.setRotate(270);
    }

    Platform.runLater(new Runnable() { //and create the button if it doesnt exist if it doesnt
        @Override
        public void run() {
            //THIS IS MY LINE
            playerPlayAreas.get(id).add(button);
        }
    });
    return button;
}

我也为我的setSpacing()尝试了VBox属性但是没有效果。

javafx-8 fxml scenebuilder
1个回答
2
投票

要确定父布局中节点的大小,JavaFX使用未转换节点的边界,即它不考虑旋转并使用呈现为宽度的大小作为高度。

您可以通过将Button包装在Group中来解决此问题,但这需要您编辑方法的签名或修改调用方法:

Button button1 = new Button("Yes");
Button button2 = new Button("No");
button1.setRotate(90);
button2.setRotate(90);

VBox layout = new VBox(new Group(button1), new Group(button2));
© www.soinside.com 2019 - 2024. All rights reserved.