我想获得像打击图像那样的效果,其中的滚动条始终保持在转到按钮附近。
但是当我调整它的大小时,滚动窗格不遵守其约束
例如
这是我的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"
,在底部的窗格中添加了VBox.vgrow="ALWAYS"
。我还删除了顶部锚定窗格的首选高度,并将文本字段固定在距底部14px的位置,这样您的边距就不错了。关于java - JavaFX ScrollPane不符合其约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54189438/