具有首选大小和最小大小的 JavaFx 布局错误行为

JavaFx layout missbeheavior with prefered size and min Size

免责声明:我是 JavaFx 初学者,来自 wpf 世界

我对首选高度和最大高度的行为有疑问。忽略最小高度并使用首选高度。我做错了什么?

代码示例:(这只是为了说明问题)

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<VBox fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
      <Pane fx:id="buttonPane" style="-fx-background-color: lightgreen;">
         <children>
            <Button minHeight="0.0" minWidth="0.0"  onAction="#buttonPaneClick" prefHeight="200.0" text="Button" />
         </children>
      </Pane>
   </children>
</VBox>

问题:按钮只显示了一半,没有缩小。但是最小高度是 0,所以我希望当没有更多可用位置但他被裁剪掉时按钮会变为 0。

来自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.

(我的重点)。

因此,无论如何,包含按钮的 Pane 都会将其调整为首选大小。如果您将包含按钮的窗格替换为比这更多布局的布局容器(例如 HBox),那么您将看到您期望的行为:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
      <HBox fx:id="buttonPane" style="-fx-background-color: lightgreen;">
         <children>
            <Button minHeight="0.0" minWidth="0.0"  prefHeight="200.0" text="Button" />
         </children>
      </HBox>
   </children>
</VBox>