【发布时间】:2018-01-18 20:34:13
【问题描述】:
我正在修改 Scene builder 以使 UI 设计得尽可能好,但我在 Javafx 中定位标题窗格时遇到了问题。 如您所见,我在锚窗格内设置了带有标题窗格的网格窗格,其中设置了顶部、左侧和右侧锚点,而 textarea 设置了底部、左侧和右侧锚点。
我想要什么: 带有标题窗格的网格窗格应该在顶部,当所有标题窗格都折叠时,网格应该只有该窗格标签的高度。其余的空间应该被文本区域占据。
我怎样才能做到这一点?
<?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 的代码,对删除网格的包装进行了微小的更改(真的有必要吗?)。更改该网格的首选项高度可防止其崩溃: (网格上的红色背景为红色以提高可见度)
我尝试使用 pref h = 200 和 min h = 0 设置行、网格和每个标题窗格。还将 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>
【问题讨论】:
-
看来,如果你在点击
GridPane的左侧或右侧标签后设置GridPane's`` preferredHeight,这会导致你的问题。如果将其设置为USE_COMPUTER_SIZE,则可以解决问题。
标签: java user-interface javafx