我进行了以下设置:
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();
我的问题是,warningLabel
和 errorLabel
的大小不同。它们应该水平生长到相同的大小。我不想给出具体的尺寸。两者的大小必须以哪个标签需要更大的大小来显示整个文本为准。
问题是,两个标签都有背景,您可以看到 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);
输出工具提示
类似于:
背景: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/