在 JavaFX 中包装内容

wrap content in JavaFX

package example;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Text text = new Text("This is a Text");

        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.setStyle("-fx-background-color: yellow;");
        box.getChildren().add(text);

        StackPane container = new StackPane();
        container.getChildren().add(box);

        BorderPane bp = new BorderPane();
        bp.setCenter(container);

        Scene scene = new Scene(bp, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是输出:

问:有人能解释一下为什么Vbox会填满整个屏幕吗?有没有类似Android的wrap_content的方法?我想要下面的图像作为输出:

VBox 会自动将自身调整为 Parent 的大小,因此最好不要为其设置背景色。相反,您可以使用 Label 代替 Text,然后将背景色添加到标签 而不是 VBox.

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label text = new Label("This is a Text");

        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        text.setStyle("-fx-background-color: yellow;");
        box.getChildren().add(text);

        StackPane container = new StackPane();
        container.getChildren().add(box);

        BorderPane bp = new BorderPane();
        bp.setCenter(container);

        Scene scene = new Scene(bp, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

这将为您提供如下图所示的输出:

解决方案

将 VBox 包裹在 Group 中;例如使用:

container.getChildren().add(new Group(box));

而不是:

container.getChildren().add(box);

为什么有效

来自 javadoc 组:

By default, a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass.

这意味着 VBox 不会增长到超过其内容的首选大小(刚好足以在其中显示标签的区域)。

替代实现

将 VBox 的最大大小设置为首选大小。然后 VBox 只会变得足够大以适应其中内容的首选大小,并且永远不会变得更大。

box.setMaxSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);

为什么 VBox 默认增长

它是一个 resizable container,它将伸展以填充可用区域。

我不知道效果是否与 Android wrap_content 方法完全相同,因为我从未为 Android 开发过,但效果似乎完全匹配您在问题中提供的第二张图片,这似乎是您想要的。