JavaFX 如何在使用 FXML 时使 ScrollPane 内容水平增长?

JavaFX how to make ScrollPane content grow horizontally when using FXML?

这是我的 GridPane 的正常水平增长:

但是当我将 GridPane 包裹在 ScrollPane 中时:

我想保留 ScrollPane 的垂直滚动功能,但允许 GridPaneScrollPane 中水平滚动。可能吗?我尝试将 HBar Policy 设置为 NEVER,但它只隐藏了 HBar。提前致谢。

代码中的最小示例(似乎有效):

Stage stage = new Stage();
ScrollPane sp = new ScrollPane();
sp.setFitToHeight(true);
sp.setFitToWidth(true);
GridPane gp = new GridPane();
        
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TextField tf3 = new TextField();
TextField tf4 = new TextField();

gp.getColumnConstraints().add(new ColumnConstraints(10, 100, 1000, Priority.ALWAYS, HPos.CENTER, true));
gp.getColumnConstraints().add(new ColumnConstraints(10, 100, 1000, Priority.ALWAYS, HPos.CENTER, true));
gp.getRowConstraints().add(new RowConstraints(10, 20, 1000, Priority.ALWAYS, VPos.CENTER, true));
gp.getRowConstraints().add(new RowConstraints(10, 20, 1000, Priority.ALWAYS, VPos.CENTER, true));
gp.getRowConstraints().add(new RowConstraints(10, 20, 1000, Priority.ALWAYS, VPos.CENTER, true));
        
gp.add(tf1, 0, 0);
gp.add(tf2, 1, 0);
gp.add(tf3, 0, 1);
gp.add(tf4, 0, 2);
        
sp.setContent(gp);
        
stage.setScene(new Scene(sp));
stage.show();   

我在 FXML 中的示例(不起作用):

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<ScrollPane fitToHeight="true" fitToWidth="true" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <content>
      <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0">
        <columnConstraints>
          <ColumnConstraints hgrow="ALWAYS" maxWidth="1000.0" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="ALWAYS" maxWidth="1000.0" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="1000.0" minHeight="10.0" prefHeight="20.0" vgrow="ALWAYS" />
          <RowConstraints maxHeight="1000.0" minHeight="10.0" prefHeight="20.0" vgrow="ALWAYS" />
          <RowConstraints maxHeight="1000.0" minHeight="10.0" prefHeight="20.0" vgrow="ALWAYS" />
        </rowConstraints>
         <children>
            <TextField />
            <TextField GridPane.columnIndex="1" />
            <TextField GridPane.rowIndex="1" />
            <TextField GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </content>
</ScrollPane>

如果您将 ScrollPanefitToWidth 属性 设置为 true

当然你的 GridPane 必须能够增长,所以你需要确保它的 prefWidth/maxWidth 属性不会阻止它,至少一个相同它的列数。

示例 FXML(在 SceneBuilder 中测试):

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<ScrollPane fitToWidth="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="410.0" prefWidth="518.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <content>
      <GridPane hgap="6.0" vgap="4.0">
        <columnConstraints>
          <ColumnConstraints fillWidth="false" hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity" prefWidth="300.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="150.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="150.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="150.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button mnemonicParsing="false" text="Button" />
            <Button mnemonicParsing="false" text="Button" GridPane.rowIndex="1" />
            <Button mnemonicParsing="false" text="Button" GridPane.rowIndex="2" />
            <TextField GridPane.columnIndex="1" />
            <TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </content>
</ScrollPane>