gpt4 book ai didi

layout - JavaFX 8 maxWidth 被忽略了吗?

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

忽略 JavaFx 8 最大宽度

对于具有更宽内容的 HBox 子项,将忽略 maxWidth。

例如,下面的中心 Pane 太大,延伸到右列 (Pane) 下:

 <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="1000.0" prefWidth="1236.0">
<children>
<Pane fx:id="leftPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0"/>
<Pane fx:id="centerPane" maxHeight="10000.0" maxWidth="10000.0" minHeight="400.0" minWidth="120.0" prefHeight="10000.0" >
</Pane>
<Pane fx:id="rightPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" />
</children>
</HBox>

没有冲突的 CSS,例如定义百分比或设置内容宽度。

如何使 maxWidth 成为硬限制?

有没有办法让 JavaFX 在每次子内容更改时重新计算子 Pane 的大小,同时永远不会超过父级的最大宽度?

最佳答案

来自 JavaFX Pane documentation :

This class may be used directly in cases where absolute positioning of children is required since it does not perform layout beyond resizing resizable children to their preferred sizes. It is the application's responsibility to position the children since the pane leaves the positions alone during layout.

因此,您无法通过将基本 Pane 用作子 Pane 来实现您的目标。最好尝试使用 StackPanes相反:

A stackpane's parent will resize the stackpane within the stackpane's resizable range during layout.

A stackpane's unbounded maximum width and height are an indication to the parent that it may be resized beyond its preferred size to fill whatever space is assigned to it.

看看这个例子(我向 StackPanes 添加了背景颜色,因为我使用 SceneBuilder 来测试行为)并告诉我它是否满足您的需求:

<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="1000.0" prefWidth="1236.0" style="-fx-background-color: lightblue;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane fx:id="leftPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" style="-fx-background-color: red;" />
<StackPane fx:id="centerPane" maxHeight="10000.0" maxWidth="10000.0" minHeight="400.0" minWidth="120.0" prefHeight="10000.0" style="-fx-background-color: white;">
<children>
<Label prefWidth="999.0" text="Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. Label of example. " />
</children></StackPane>
<StackPane fx:id="rightPane" maxHeight="10000.0" maxWidth="120.0" minHeight="400.0" minWidth="100.0" prefHeight="10000.0" prefWidth="120.0" style="-fx-background-color: green;" />
</children></HBox>

您也可以改用其他 Pane 或 Box 组件。

更新:

前面的示例使中心 Pane 根据其子 Pane 的宽度动态调整,在需要时填充父 Pane,但永远不会超过其最大宽度。因此,如果您希望 Pane 始终填充其父级的宽度,则必须向中心 Pane 添加足够大的首选宽度。

<StackPane fx:id="centerPane" maxHeight="10000.0" minHeight="400.0" prefHeight="10000.0" prefWidth="1000.0"/>

关于layout - JavaFX 8 maxWidth 被忽略了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32212560/

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