在JavaFX中,我想要一個HBox根據其包含的Button子元素(父元素調整爲子元素)自動調整其高度和寬度。如何根據子元素自動調整父節點的大小
默認情況下,發生了以下情況: HBox-width設置爲其最大寬度,使其完全適合其父級StackPane(子級調整爲父級)。就像HERE。
以下圖片說明我的問題:
會發生什麼: ex.1
我需要什麼: ex.2
會有人碰巧知道如何完成的描述,使用JavaFX和CSS?
這些都是我的一些代碼片段:
// JavaFX Code
BorderPane mainPane = new BorderPane();
Button button1 = new Button("Button1");
Button button2 = new Button("Button2");
button1.getStyleClass().add("menuButton");
button2.getStyleClass().add("menuButton");
HBox buttonParent = new HBox();
buttonParent.setId("buttonParent");
// buttonParent.autosize();
buttonParent.setAlignment(Pos.TOP_LEFT);
buttonParent.getChildren().addAll(button1, button2);
mainPane.setCenter(parent);
/* CSS Code */
.menuButton {
-fx-background-size: cover;
-fx-background-position: center;
-fx-border-color: #162338;
-fx-border-width: 1px;
-fx-pref-width: 100px;
}
#buttonParent {
-fx-background-color: #000;
-fx-border-color: #162338;
-fx-border-radius: 3px;
-fx-border-insets: -1px;
-fx-background-radius: 3px;
-fx-padding: 5px;
}
非常感謝您!
這真的是實現我的目標的正確方法。非常感謝! –