java - 如何在 VBox 中增长标签

标签 java javafx javafx-8

我进行了以下设置:

Label errorLabel = new Label("Hello Hans");
Label warningLabel = new Label("HEEELLLOOOOOOOOOOOOOOOOOOOOOOOOO");
VBox box = new VBox();
box.getChildren().addAll(errorLabel, warningLabel);
Tooltip t = new Tooltip();
t.setGraphic(box);
t.show();

我的问题是,warningLabelerrorLabel 的大小不同。它们应该水平生长到相同的大小。我不想给出具体的尺寸。两者的大小必须以哪个标签需要更大的大小来显示整个文本为准。

问题是,两个标签都有背景,您可以看到 warningLabel 占用了更多空间。我需要这些标签的两个背景同等增长。

最佳答案

您可以设置 maxWidthProperty如果有两个 Label,则为 Double.MAX_VALUE

Label errorLabel = new Label("Hello Hans");
errorLabel.setStyle("-fx-background-color: red");
Label warningLabel = new Label("HEEELLLOOOOOOOOOOOOOOOOOOOOOOOOO");
warningLabel.setStyle("-fx-background-color: orange");
warningLabel.setMaxWidth(Double.MAX_VALUE);
errorLabel.setMaxWidth(Double.MAX_VALUE);

输出工具提示类似于:

enter image description here

背景:Making Buttons the Same Size - Using a VBox - Example 2-1

To enable all of the buttons to be resized to the width of the VBox pane, the maximum width of each button is set to the Double.MAX_VALUE constant, which enables a control to grow without limit. An alternative to using the maximum value constant is to set the maximum width to a specific value, such as 80.0.

注意:重要的是 fillWidthProperty VBox 的 必须设置为 true(此属性默认为 true):

Whether or not resizable children will be resized to fill the full width of the vbox or be kept to their preferred width and aligned according to the alignment hpos value.

这很重要,因为:

VBox will resize children (if resizable) to their preferred heights and uses its fillWidth property to determine whether to resize their widths to fill its own width or keep their widths to their preferred (fillWidth defaults to true).

因此,如果 fillWidthProperty 设置为 true,VBox 将尝试将其子项的大小调整为自己的宽度,如果 maxWidthProperty 设置为每个子项的首选宽度,这就是为什么此属性必须设置为“足够大”的数字。

关于java - 如何在 VBox 中增长标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204144/

相关文章:

java - 动态 Javafx View 的 Controller 类

java - 开发多线程Javafx应用需要注意什么?

java - 如何在不为其分配样式类的情况下使用 CSS 样式表对 JavaFX 中的文本形状进行样式化?

JavaFX 2 : How to load stylesheet into a Scene subclass distributed as part of a library?

JavaFX 8 TabPane - 选项卡按钮不断滚动到 TabPane header 的视口(viewport)之外

java - 线程限制/swingworkers

java - 如何在没有迭代列表的情况下将 List<xxxx> 转换为 Map<primarykey of xxxx, xxxx>?

java - 如何在 JavaFx 3d 中获得将场景放置在场景中等功能

java - 如何使用可变参数重载方法?

java - 删除重复字符,保留顺序