java - JavaFX 中 Gridpane 如何拉伸(stretch) AnchorPane 的整个高度? AnchorPane 是主窗口的根

标签 java javafx gridpane

我想创建一个基于GridPane的应用程序。 为了实现自适应性,我使用 AnchorPane。布局在宽度上延伸得很好,但在高度上却很糟糕。如何修复?

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.control.Button?>

<AnchorPane maxHeight="1000" maxWidth="-Infinity" minHeight="1000" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="code.WhiteBoardApplication">
   <children>
      <GridPane  maxWidth="-Infinity" minHeight="1000" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1200.0" gridLinesVisible="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="251.0" minHeight="0.0" prefHeight="66.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="311.0" minHeight="10.0" prefHeight="154.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="202.0" minHeight="10.0" prefHeight="178.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2">
               <children>
                  <ToolBar prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                      <Button mnemonicParsing="false" text="Button" />
                    </items>
                  </ToolBar>
               </children></AnchorPane>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
            <AnchorPane minHeight="1000.0" prefHeight="1000.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.rowSpan="2" />
         </children>
      </GridPane>
   </children>
</AnchorPane>

Application

最佳答案

您已在列行上设置最大高度

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

<?import javafx.scene.control.Button?>
<?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?>

<AnchorPane maxHeight="1000" maxWidth="-Infinity" minHeight="1000" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="code.WhiteBoardApplication">
   <children>
      <GridPane gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="1000" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="0.0" prefHeight="66.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="154.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="178.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2">
               <children>
                  <ToolBar prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                      <Button mnemonicParsing="false" text="Button" />
                    </items>
                  </ToolBar>
               </children></AnchorPane>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
            <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
            <AnchorPane minHeight="1000.0" prefHeight="1000.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.rowSpan="2" />
         </children>
      </GridPane>
   </children>
</AnchorPane>

关于java - JavaFX 中 Gridpane 如何拉伸(stretch) AnchorPane 的整个高度? AnchorPane 是主窗口的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027205/

相关文章:

java - <?> 与 <T>

java - 在 android 中进行语音识别后,文本不会打印在 editText 上

java - Appium - 如何获取 Android ScrollView 中的所有 View

java - 在国际象棋游戏的 Gridpane 中交换节点

Javafx从网格 Pane 中的文本字段获取文本

java - Spring框架XML配置与Annotation配置执行流程的区别

JavaFX周期性后台任务

javafx - 悬停时如何在javafx TableView中突出显示一列而不是一行?

java - SceneBuilder 看起来与 JavaFX 程序不同?

java - 将 GridPane 包装在 ScrollPane 中或以编程方式修改它。最有效的方法?