layout - JavaFX 8 maxWidth 被忽略了吗?

标签 layout javafx-8

忽略 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/

相关文章:

css - 从哪里获得所有 JavaFX CSS 属性名称的列表?

css - 必须适合其内容的剩余宽度 div?

android - 如何将 View (例如标签)的高度/宽度设置为其内容的大小?

java - 停止 JavaFX LinearGradient 中的不透明度

javafx-2 - 如何更改选项卡 Pane 样式

java - 使用 (getClass().getResourceAsStream ("Path/to/pic")) 与 ("Path/to/pic") 的新图像实例

JavaFX:从 FXML 加载场景但从代码添加标签

ruby-on-rails-3 - 在液体布局中渲染部分 (Rails3)

ios - 当应用程序在另一个设备上运行时,如何使对象适应另一个大小。 ( swift )

java - Netbeans 属性表 导航器窗口中容器中的 Swing 控件