gpt4 book ai didi

java - 带标题 Pane 的自动高度

转载 作者:行者123 更新时间:2023-11-30 02:16:13 24 4
gpt4 key购买 nike

我正在修改场景构建器以使 UI 设计得尽可能好,但我在 Javafx 中定位标题 Pane 时遇到问题。正如您所看到的,我有一个网格 Pane ,其中 anchor Pane 内有标题 Pane ,并设置了顶部、左侧和右侧 anchor ,文本区域设置了底部、左侧和右侧 anchor 。

我想要什么:带有标题 Pane 的网格 Pane 应位于顶部,并且当所有标题 Pane 折叠时,网格应仅具有该 Pane 标签的高度。其余空间应由文本区域占用。

我怎样才能实现这个目标?

main window(上面有文字区域)

structure

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1">
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints valignment="TOP" vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TitledPane animated="false" text="Memory" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Registers" GridPane.columnIndex="2" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Stack" GridPane.columnIndex="3" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</GridPane>
<TextArea editable="false" GridPane.rowIndex="1" />
</children>
</GridPane>
</center>
<top>
<ToolBar>
<items>
<Button mnemonicParsing="false" text="Start" />
<Button mnemonicParsing="false" text="End" />
<Button mnemonicParsing="false" text="Step forward" />
</items>
</ToolBar>
</top>
</BorderPane>

编辑:我使用了 @Sedric 的代码,做了一些小的改变,删除了网格的包装器(真的有必要吗?)。更改该网格的首选高度可防止其折叠: main(网格上的红色背景变为红色以提高可见度)

我尝试使用 pref h = 200 和 min h = 0 设置行、网格和每个标题 Pane 。还将 vgrow 更改为从不,也不起作用。

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ToolBar>
<items>
<Button mnemonicParsing="false" text="Start" />
<Button mnemonicParsing="false" text="End" />
<Button mnemonicParsing="false" text="Step forward" />
</items>
</ToolBar>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" minHeight="0.0" prefHeight="200.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TitledPane animated="false" text="Memory" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Registers" GridPane.columnIndex="2" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Stack" GridPane.columnIndex="3" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</GridPane>
<TextArea editable="false" maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
</children>
</VBox>

最佳答案

在这种情况下,最好使用 VBox 作为根。然后将 TextArea 改为 VGrow = "Always"

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ToolBar>
<items>
<Button mnemonicParsing="false" text="Start" />
<Button mnemonicParsing="false" text="End" />
<Button mnemonicParsing="false" text="Step forward" />
</items>
</ToolBar>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="0.0" valignment="TOP" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TitledPane animated="false" text="Memory" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Registers" GridPane.columnIndex="2" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Stack" GridPane.columnIndex="3" GridPane.valignment="TOP" GridPane.vgrow="ALWAYS">
<content>
<AnchorPane>
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</GridPane>
</children>
</GridPane>
<TextArea editable="false" maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
</children>
</VBox>

enter image description here

enter image description here

关于java - 带标题 Pane 的自动高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48329855/

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