对于内容较宽的HBox儿童,将忽略maxWidth。
例如,下面的中间窗格太大,在右列(窗格)下方延伸:
<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,例如定义百分比或以其他方式设置内容宽度。
有没有办法让JavaFX在每次子内容更改时重新计算子窗格大小,而永远不会超过父级的maxWidth?
答案 0 :(得分:6)
这个类可以直接用于需要儿童绝对定位的情况,因为除了将可调整大小的孩子调整到他们喜欢的大小之外,它不会执行布局。由于窗格在布局期间单独留下位置,因此应用程序负责定位子项。
因此,您无法通过将基本窗格用作子项来达到目标。最好尝试使用StackPanes代替:
stackpane的父级将在布局期间调整stackpane的可调整大小范围内的堆栈窗格。
Stackpane的无界最大宽度和高度表示父级可以调整大小超出其首选大小以填充分配给它的任何空间。
看看这个例子(我为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组件。
<强>更新强>
上一个示例使中心窗格根据其子项的宽度动态调整,如果需要,可以满足父窗格,但绝不会超过其最大宽度。因此,如果您希望窗格始终填充其父级的宽度,则必须在中间窗格中添加足够大的首选宽度。
<StackPane fx:id="centerPane" maxHeight="10000.0" minHeight="400.0" prefHeight="10000.0" prefWidth="1000.0"/>
答案 1 :(得分:2)
要根据子项大小自动调整容器大小,请使用javafx
常量,如:
centerPane.setMinWidth(Control.USE_PREF_SIZE);