为什么 JavaFX FlowPane 在设置为非托管时不自行调整大小?

问题描述 投票:0回答:1

我想要一个 Flowpane 项目布局,无论 Flowpane 的父级大小如何

据我了解,在任何区域上调用 setManaged(false) 都会导致父级不调整该区域的大小并导致该区域成为其自己的布局根。

但是如果我尝试这样做

Pane
 -FlowPane
  -label
  -label
  -label

其中 FlowPane 已成功设置为 false,最小/最大/首选项宽度和高度均设置为绝对值,在我的示例中为 200 像素,那么我希望看到 Pane 具有其设置的大小,并且 FlowPane 位于我的任何位置将其高度和宽度设置为 200。事实并非如此。

这是一个简单的例子。当标签在其父 flowpane 外部渲染时,flowpane 计算的宽度和高度为 0,因此更宽的 flowpane 的包装逻辑将永远不允许任何比像 stackPane 那样包装每个项目的内容。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.Pane?>

<Pane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <FlowPane alignment="CENTER_LEFT" managed="false" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" translateX="120.0" translateY="120.0">
         <children>
            <Label text="fdsfdfdsfdsfds">
               <graphic>
                  <Pane maxHeight="10.0" maxWidth="10.0" minWidth="10.0" prefHeight="10.0" prefWidth="10.0" style="-fx-background-color: red;" />
               </graphic>
            </Label>
            <Label text="dfdfdsfsdf">
               <graphic>
                  <Pane maxHeight="10.0" maxWidth="10.0" minWidth="10.0" prefHeight="10.0" prefWidth="10.0" style="-fx-background-color: red;" />
               </graphic>
            </Label>
            <Label text="dfdfsd">
               <graphic>
                  <Pane maxHeight="10.0" maxWidth="10.0" minWidth="10.0" prefHeight="10.0" prefWidth="10.0" style="-fx-background-color: red;" />
               </graphic>
            </Label>
            <Label text="fdsfds">
               <graphic>
                  <Pane maxHeight="10.0" maxWidth="10.0" minWidth="10.0" prefHeight="10.0" prefWidth="10.0" style="-fx-background-color: red;" />
               </graphic>
            </Label>
         </children>
      </FlowPane>
   </children>
</Pane>

如何解释本地尺寸的缺乏?

javafx scenebuilder gluonfx
1个回答
0
投票

正如评论中所指出的,并由我自己确认,因为我陷入了同样的情况,FlowPane 需要包装在一个处理布局的 Group 中 - 然后当然需要将 Group 而不是 FlowPane 本身设置为不受管理,正如我现在痛苦地意识到的那样。

按照

https://stackoverflow.com/a/29538731/6723250
中所述设置prefWrapLength在这种情况下没有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.