gpt4 book ai didi

JavaFX:如何使 VBox 及其内容随着窗口高度无限扩展,就像宽度一样?

转载 作者:行者123 更新时间:2023-12-02 08:53:16 24 4
gpt4 key购买 nike

可能是一个非常简单的问题,但我一直无法弄清楚。

我在 VBox 和 SplitPane 内有一个 ScrollPane(feat.Label):

(完整的 fxml 文件位于底部)

enter image description here

当您水平展开窗口或分割 Pane 分隔符时,垂直框会自动拉伸(stretch)以适合垂直框,标签会适本地重新居中,并且滚动 Pane 会展开以适合垂直框。垂直扩展时不会发生这种情况,我希望如此。我怎样才能做到这一点?如果我应该使用不同的容器,请告诉我。

我的麻烦的 Gif(如果有帮助的话): enter image description here完整的 fxml 文件:

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.ui.DefaultLayoutController">
<children>
<BorderPane prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<VBox prefHeight="573.0" prefWidth="306.0" style="-fx-background-color: #ccbfb1;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label alignment="CENTER" maxWidth="Infinity" prefHeight="61.0" prefWidth="260.0" style="-fx-alignment: center; -fx-background-color: #e6d7c8;" text="Text" textAlignment="CENTER">
<font>
<Font name="AdobeDevanagari-Regular" size="51.0" />
</font>
</Label>
<ScrollPane fx:id="mainScrollPane" prefHeight="559.0" prefWidth="453.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
</content>
</ScrollPane>
</children>
</VBox>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="573.0" prefWidth="364.0">
<children>
<VBox prefHeight="573.0" prefWidth="396.0" style="-fx-background-color: #e6c896;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</center></BorderPane>
</children>
</AnchorPane>

最佳答案

原始答案

解决方案:程序化

实现此目的的一种方法是设置 VBox 的最大高度及其元素 Double.MAX_VALUE使用setMaxHeight(double)方法。或者,您可以使用静态 VBox.setVgrow(Priority)方法(据我所知推荐方式)对所有 VBox的 children 。您可以仅使用常规 for 迭代子级。循环或forEach流操作:

// For loop
for(Node child : yourBox.getChildren()) {
VBox.setVgrow(child, Priority.ALWAYS);
}

// forEach stream operation (Java 8+)
yourBox.getChildren().forEach(child -> VBox.setVGrow(child, Priorty.ALWAYS));

编辑

替代解决方案:场景生成器

正如 @Panais 所建议的那样,在 JavaFX“场景生成器”类型应用程序(例如 Gluon)中,vgrow您的 VBox 的属性可以设置为Always ,通过简单的下拉菜单即可实现与上述相同的功能。

替代解决方案:XML

该用户还提到在 XML 中编辑此属性 - 设置 <VBox prefHeight=..., VBox.vgrow="always" />VBox 的 XML 标记内,也取得了与上述两个答案相同的结果。

关于JavaFX:如何使 VBox 及其内容随着窗口高度无限扩展,就像宽度一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60647102/

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