gpt4 book ai didi

java - 如何限制javafx垂直flowpane中的列数

转载 作者:行者123 更新时间:2023-12-02 05:00:35 25 4
gpt4 key购买 nike

我主要使用场景生成器为我的 javafx 应用程序创建 ui。我目前正在使用垂直的 FlowPane 来按照我想要的方式排列它,但是它不是按照我想要的方式继续向下,而是继续离开窗口的右侧。

Application image

我画了红色轮廓来演示我想要将其他框放置在哪里。 FlowPane 是用于此目的的合适容器吗?如果是这样,我怎样才能让它发挥作用?

最佳答案

查看此示例应用程序:

private static final Random rand = new Random();
private static final int max = 200;

@Override
public void start(Stage primaryStage) {
try {
FlowPane root = new FlowPane(Orientation.VERTICAL);
root.setPrefWrapLength(5000);
root.setPadding(new Insets(10));
root.setHgap(10);
root.setVgap(10);

for (int i = 0; i < 200; i++) {
Rectangle rectangle = new Rectangle(50, 50);
rectangle.setFill(Color.rgb((int) (rand.nextDouble() * max),
(int) (rand.nextDouble() * max),
(int) (rand.nextDouble() * max)));
root.getChildren().add(rectangle);
}

Scene scene = new Scene(new ScrollPane(root), 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}

root.setPrefWrapLength(5000); 行是重要的一行,如 FlowPane.setPrefWrapLength()确定 FlowPane 将增长的大小(以像素为单位)。

由于您的 FlowPaneVERTICALroot.setPrefWrapLength(5000); 确定每列最多向下增长 5000。之后创建一个新列。

如果您想限制列数,或者换句话说:您的 FlowPane 的最大宽度,您应该从 VERTICAL 切换到 HORIZONTAL as FlowPane.setPrefWrapLength() 将限制 Pane 的宽度。

关于java - 如何限制javafx垂直flowpane中的列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28316259/

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