在 javafx 的 HBox 中有三个同样大小的 VBox
Have three equally sized VBoxes in HBox in javafx
我在一个 HBox 中有三个 VBox。我希望它们都占 HBox 的三分之一和整个高度。我试过 HBox.setHgrow(<every VBox>, Priority.ALWAYS)
和 <every VBox>.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
并且效果很好,但是当我将一个组件添加到其中一个 VBox 时,它会自行调整大小并变得比其他的更大。
知道如何正确解决这个问题吗?
您可以将 VBox
添加到 StackPane
,将 StackPane
添加到 HBox
。在 StackPane
中,您还可以放置一个占位符(我通常使用透明 Rectangular
)并将其绑定到绑定 maxVBoxWidth
。这是一个你必须自己定义的Binding
:
DoubleBinding maxVBoxBinding = new DoubleBinding() {
{
super.bind(vbox1.widthProperty(),vbox2.widthProperty(), vbox3.widthProperty());
}
@Override
protected double computeValue() {
return Math.max(vbox1.getWidth(), Math.max(vbox2.getWidth(), vbox2.getWidth()));
}
}
使用 GridPane
而不是 HBox
。您可以使用一组列约束,每个约束都设置 percentWidth
以赋予每列相等的宽度。
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class VBoxInGridPane extends Application {
@Override
public void start(Stage primaryStage) {
VBox box1 = new VBox();
box1.setStyle("-fx-background-color: -fx-background; -fx-background: red ;");
box1.getChildren().add(new Label("Content"));
VBox box2 = new VBox();
box2.setStyle("-fx-background-color: green ;");
VBox box3 = new VBox();
box3.setStyle("-fx-background-color: blue ;");
GridPane root = new GridPane();
root.add(box1, 0, 0);
root.add(box2, 1, 0);
root.add(box3, 2, 0);
for (int i = 0 ; i < 3 ; i++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(100.0/3.0);
cc.setHgrow(Priority.ALWAYS);
root.getColumnConstraints().add(cc);
}
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS);
root.getRowConstraints().add(rc);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我在一个 HBox 中有三个 VBox。我希望它们都占 HBox 的三分之一和整个高度。我试过 HBox.setHgrow(<every VBox>, Priority.ALWAYS)
和 <every VBox>.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
并且效果很好,但是当我将一个组件添加到其中一个 VBox 时,它会自行调整大小并变得比其他的更大。
知道如何正确解决这个问题吗?
您可以将 VBox
添加到 StackPane
,将 StackPane
添加到 HBox
。在 StackPane
中,您还可以放置一个占位符(我通常使用透明 Rectangular
)并将其绑定到绑定 maxVBoxWidth
。这是一个你必须自己定义的Binding
:
DoubleBinding maxVBoxBinding = new DoubleBinding() {
{
super.bind(vbox1.widthProperty(),vbox2.widthProperty(), vbox3.widthProperty());
}
@Override
protected double computeValue() {
return Math.max(vbox1.getWidth(), Math.max(vbox2.getWidth(), vbox2.getWidth()));
}
}
使用 GridPane
而不是 HBox
。您可以使用一组列约束,每个约束都设置 percentWidth
以赋予每列相等的宽度。
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class VBoxInGridPane extends Application {
@Override
public void start(Stage primaryStage) {
VBox box1 = new VBox();
box1.setStyle("-fx-background-color: -fx-background; -fx-background: red ;");
box1.getChildren().add(new Label("Content"));
VBox box2 = new VBox();
box2.setStyle("-fx-background-color: green ;");
VBox box3 = new VBox();
box3.setStyle("-fx-background-color: blue ;");
GridPane root = new GridPane();
root.add(box1, 0, 0);
root.add(box2, 1, 0);
root.add(box3, 2, 0);
for (int i = 0 ; i < 3 ; i++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(100.0/3.0);
cc.setHgrow(Priority.ALWAYS);
root.getColumnConstraints().add(cc);
}
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS);
root.getRowConstraints().add(rc);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}