【问题标题】:Auto height with Titled panes带有标题窗格的自动高度
【发布时间】: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


【解决方案1】:

在这种情况下,最好使用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>

【讨论】:

  • 感谢您的回答!但是如何更改网格的默认高度以保持折叠工作?如果我更改网格/标题窗格的任何首选高度,它会停止折叠,只留下空白。
  • 使用我的代码或单击网格的左侧或右侧选项卡并将最小高度设置为零。
  • 如果set min-height to zero 有帮助,请告诉我。
  • 我使用了你的代码,但是设置 pref 高度可以防止它在 min h = 0 时崩溃。imgur.com/a/eNUCb
  • 我认为某些东西与 pref 值和 hbox 有关,我会做一些研究。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2010-10-22
  • 2019-11-12
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多