JavaFX ScrollPane 不符合其约束

标签 java javafx

我想要实现像下图这样的结果,其中滚动条始终靠近“执行”按钮。

This is what I am trying to achieve

但是当我调整它的大小时,滚动 Pane 不遵守其约束 例如

This is what happens

这是我的 FXML 代码:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ResultsUI">
   <children>
      <AnchorPane prefHeight="434.0" prefWidth="600.0" style="-fx-background-color: white;">
         <children>
            <TextField id="queryOldTF" fx:id="queryOldTF" layoutX="14.0" layoutY="14.0" prefHeight="40.0" prefWidth="518.0" style="-fx-min-height: 40; -fx-border-radius: 50%; -fx-border-color: #007BFF; -fx-background-color: white;" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="68.0" AnchorPane.topAnchor="14.0" />
            <Button id="serachAgainBtn" fx:id="serachAgainBtn" layoutX="532.0" layoutY="14.0" mnemonicParsing="false" onAction="#searchAgain" style="-fx-background-color: #007BFF; -fx-min-height: 40; -fx-min-width: 40; -fx-text-fill: white; -fx-border-radius: 50%; -fx-background-radius: 50%;" text="GO" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0" />
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="700.0" prefWidth="600.0" style="-fx-background-color: white;">
         <children>
            <ScrollPane id="scrollPane" fx:id="scrollPane" fitToWidth="true" layoutX="1.0" prefHeight="339.0" prefWidth="574.0" style="-fx-background-color: white;" vbarPolicy="ALWAYS" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="0.0">
              <content>
                  <Pane style="-fx-background-color: white;">
                     <children>
                        <AnchorPane layoutX="10.0" layoutY="14.0" style="-fx-background-color: white;">
                           <children>
                              <Label id="descTl" layoutY="44.0" prefHeight="83.0" prefWidth="556.0" text="Description" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="40.0" />
                              <Label id="nameTl" prefHeight="27.0" prefWidth="557.0" style="-fx-font-size: 18;" text="Name" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="1.0" />
                              <Label id="authorTl" layoutX="4.0" layoutY="27.0" prefHeight="17.0" prefWidth="413.0" text="Authors" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" />
                              <Button id="openBtn" layoutX="6.0" layoutY="135.0" mnemonicParsing="false" style="-fx-background-color: white; -fx-border-color: #007BFF; -fx-border-radius: 25%;" text="Open" AnchorPane.bottomAnchor="1.0" AnchorPane.rightAnchor="505.40000000000003" />
                           </children>
                        </AnchorPane>
                     </children>
                  </Pane>
              </content>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </ScrollPane>
         </children>
      </AnchorPane>
   </children>
</VBox>

我花了几个小时尝试修复它,但不知道我错过了什么,任何帮助将不胜感激。

最佳答案

尝试以下操作:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ResultsUI">
   <children>
      <AnchorPane prefWidth="600.0" style="-fx-background-color: white;" VBox.vgrow="NEVER">
         <children>
            <TextField id="queryOldTF" fx:id="queryOldTF" layoutX="14.0" layoutY="14.0" prefHeight="40.0" prefWidth="518.0" style="-fx-min-height: 40; -fx-border-radius: 50%; -fx-border-color: #007BFF; -fx-background-color: white;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="68.0" AnchorPane.topAnchor="14.0" />
            <Button id="serachAgainBtn" fx:id="serachAgainBtn" layoutX="532.0" layoutY="14.0" mnemonicParsing="false" onAction="#searchAgain" style="-fx-background-color: #007BFF; -fx-min-height: 40; -fx-min-width: 40; -fx-text-fill: white; -fx-border-radius: 50%; -fx-background-radius: 50%;" text="GO" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0" />
         </children>
         <VBox.margin>
            <Insets />
         </VBox.margin>
      </AnchorPane>
      <AnchorPane prefHeight="700.0" prefWidth="600.0" style="-fx-background-color: white;" VBox.vgrow="ALWAYS">
         <children>
            <ScrollPane id="scrollPane" fx:id="scrollPane" fitToWidth="true" layoutX="1.0" prefHeight="339.0" prefWidth="574.0" style="-fx-background-color: white;" vbarPolicy="ALWAYS" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="0.0">
              <content>
                  <Pane style="-fx-background-color: white;">
                     <children>
                        <AnchorPane layoutX="10.0" layoutY="14.0" style="-fx-background-color: white;">
                           <children>
                              <Label id="descTl" layoutY="44.0" prefHeight="83.0" prefWidth="556.0" text="Description" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="40.0" />
                              <Label id="nameTl" prefHeight="27.0" prefWidth="557.0" style="-fx-font-size: 18;" text="Name" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="1.0" />
                              <Label id="authorTl" layoutX="4.0" layoutY="27.0" prefHeight="17.0" prefWidth="413.0" text="Authors" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="1.0" />
                              <Button id="openBtn" layoutX="6.0" layoutY="135.0" mnemonicParsing="false" style="-fx-background-color: white; -fx-border-color: #007BFF; -fx-border-radius: 25%;" text="Open" AnchorPane.bottomAnchor="1.0" AnchorPane.rightAnchor="505.40000000000003" />
                           </children>
                        </AnchorPane>
                     </children>
                  </Pane>
              </content>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </ScrollPane>
         </children>
      </AnchorPane>
   </children>
</VBox>

我将 VBox.vgrow="NEVER" 添加到顶部锚定 Pane ,将 VBox.vgrow="ALWAYS" 添加到底部锚定 Pane 。我还删除了顶部锚定 Pane 的首选高度,并将文本字段固定在距底部 14 像素的位置,这样您就有了一个漂亮的小边距。

关于JavaFX ScrollPane 不符合其约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189438/

相关文章:

java - 如何等待订阅完成?

java - Lucene:相似度类...如何定义几个相似度度量?

java - 如何让install4j知道卸载时不删除某些文件

java - JavaFX 中的键盘箭头

JavaFX 整个 GUI 在某些机器上损坏

java - 在 Android 上使用 GSON 解析 JSON

java - Selenium 和 Java 尝试在表格单元格 <td> 内查找 <select> 标记

animation - 禁用 JavaFX 中的所有默认动画

JavaFX 1 网格 Pane 中的第一列比其他列间隔得更远

gradle - JavaFX 模块不导出到未命名的模块