gpt4 book ai didi

java - 使用具有百分比宽度的列时,GridPane 没有给标签足够的空间

转载 作者:行者123 更新时间:2023-11-30 07:41:52 24 4
gpt4 key购买 nike

下面的代码生成这个屏幕截图:

Label not fitting

第 2 列下方的标签在需要换行时不想占用额外空间。这仅在 使用百分比宽度时发生——文档说在这种情况下它将忽略所有其他属性,包括 hgrow 等,但它没有提到它也会影响行的工作方式..但是看起来确实如此。

任何人都有解决方法或可以告诉我我做错了什么?我想要的只是显示 3 张图像,在它们下面有未知大小的标签,这些标签的间距和对齐很好,而且大小都相同......像这样:

Properly reflowed labels

以上是用自定义控件完成的,暂命名为“BetterGridPane”...

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class TestLabelWrap extends Application {

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();

root.add(new ListView(FXCollections.observableArrayList("1", "2", "3")), 0, 0);
root.add(new ListView(FXCollections.observableArrayList("1", "2", "3")), 1, 0);
root.add(new ListView(FXCollections.observableArrayList("1", "2", "3")), 2, 0);

root.add(new Label("Value A"), 0, 1);
root.add(new Label("The value for this porperty is so large it wraps, The value for this porperty is so large it wraps") {{
setWrapText(true);
}}, 1, 1);
root.add(new Label("Value C"), 2, 1);

root.getColumnConstraints().add(new ColumnConstraints() {{ setPercentWidth(33.33); }});
root.getColumnConstraints().add(new ColumnConstraints() {{ setPercentWidth(33.33); }});
root.getColumnConstraints().add(new ColumnConstraints() {{ setPercentWidth(33.33); }});

root.getRowConstraints().add(new RowConstraints() {{ setVgrow(Priority.NEVER); }});
root.getRowConstraints().add(new RowConstraints() {{ setVgrow(Priority.ALWAYS); }});

primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

最佳答案

wrapText 设置为true 实际上似乎就足够了。您可以通过简单地增加窗口的高度并观察文本开始换行(根据需要减小窗口的宽度)来看到这一点。问题似乎出在 GridPane 和它的初始大小计算上——它水平增长以容纳 Label,但不是垂直增长。一种解决方法是简单地为 SceneGridPaneLabel 指定一个显式(首选)宽度,同时保留要计算的高度。

  • 新场景(root, someWidth, -1)
  • root.setPrefWidth(someWidth)
  • label.setPrefWidth(someWidth) + label.setMaxWidth(Double.MAX_VALUE)

但是,即使 ColumnConstraints.percentWidth 的 Javadoc 说:

If set to a value greater than 0, the column will be resized to this percentage of the gridpane's available width and the other size constraints (minWidth, prefWidth, maxWidth, hgrow) will be ignored.

[min|pref|max]Width 似乎没有被忽略在首选尺寸计算中。由于您似乎拥有宽度已知且统一的图像,因此您应该能够将每个约束的首选宽度设置为略大于图像的宽度。这是一个示例(使用 OpenJDK 12 和 OpenJFX 12):

import java.util.stream.Stream;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class Main extends Application {

private static ColumnConstraints[] createColumnConstraints() {
return Stream.generate(() -> {
var constraint = new ColumnConstraints();
constraint.setHalignment(HPos.CENTER);
constraint.setPercentWidth(100.0 / 3.0);
constraint.setPrefWidth(225.0); // SET PREF WIDTH
return constraint;
}).limit(3).toArray(ColumnConstraints[]::new);
}

private static Rectangle[] createRectangles() {
return Stream.generate(() -> new Rectangle(100.0, 150.0)).limit(3).toArray(Rectangle[]::new);
}

private static Label[] createLabels(String... texts) {
return Stream.of(texts).map(text -> {
var label = new Label(text);
label.setWrapText(true);
label.setTextAlignment(TextAlignment.CENTER);
return label;
}).toArray(Label[]::new);
}

@Override
public void start(Stage primaryStage) {
var root = new GridPane();
root.setGridLinesVisible(true); // FOR VISUAL CLARITY
root.setHgap(10.0);
root.setVgap(10.0);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(20.0));
root.getColumnConstraints().addAll(createColumnConstraints());

root.addRow(0, createRectangles());
root.addRow(1, createLabels("Label A", "This is long text. ".repeat(10), "Label C"));

primaryStage.setScene(new Scene(root));
primaryStage.show();
}

}

结果是:

screenshot of UI created by example code


推测:

由于您使用 new Scene(root) 这意味着 Scene 将根据其内容的首选大小自行调整大小。这基本上让根可以自由支配,可以根据需要增长。如 GridPane 所述:

By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.

通过使用 percentWidth,每列的宽度实际上没有上限;他们会长到适合最宽的 child 。此外,因为每一列都使用 percentWidth,所以随着一个列的增长,它们都会增长。

这意味着 Label 具有几乎无限的水平增长空间,这意味着 Label 的首选高度被计算为只有一行文本;当你有无限宽度时为什么要换行?因此,Label 所在的行只能垂直增长以容纳一行文本。没有空间来包装文本超限。

关于java - 使用具有百分比宽度的列时,GridPane 没有给标签足够的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55581485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com