标题窗格的自动高度

时间:2018-01-18 20:34:13

标签: java user-interface javafx

我正在修补场景构建器以尽可能好地设计UI,但我在Javafx中定位标题窗格时遇到问题。 正如您所看到的,我在Grid窗格中有一个标题窗格,其中包含顶部,左侧和右侧锚点以及设置了底部,左侧和右侧锚点的textarea。

我想要的: 带有标题窗格的网格窗格应位于顶部,当所有标题窗格都折叠时,网格应该只具有该窗格标签的高度。其余的空间应该被文本区占用。

我怎样才能做到这一点?

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的代码,对网格删除包装器的改动很小(真的有必要吗?)。更改该网格的pref高度可防止其折叠: main (网格上的红色背景为红色以提高可见度)

我尝试设置行,网格和每个标题窗格,其中pref h = 200且min h = 0.还将vgrow更改为never,也不起作用。

<?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>

1 个答案:

答案 0 :(得分:1)

在这种情况下,最好使用VBox作为根。然后是TextAreaVGrow = "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