我认为这两个属性的定义是相同的。在“correct.fxml”网格中,如果我添加 1 行或 1 列,则会出现相应的 ScrollPane 滚动条。在“wrong.fxml”网格中不会发生这种情况,我不明白为什么。也许我没有看到它们之间的区别。感谢您的帮助。
正确.fxml
错误.fxml
正确.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="429.0"
prefWidth="761.0" stylesheets="@stylesheet.css" xmlns="http://javafx.com/javafx/22"
xmlns:fx="http://javafx.com/fxml/1">
<center>
<ScrollPane BorderPane.alignment="CENTER">
<content>
<StackPane>
<children>
<Pane StackPane.alignment="CENTER">
<children>
<GridPane alignment="CENTER" hgap="1.0" vgap="1.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
</rowConstraints>
</GridPane>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>
错误.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<BorderPane prefHeight="277.0" prefWidth="495.0" xmlns="http://javafx.com/javafx/22"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="br.com.ablogic.crossword.MainViewController">
<center>
<ScrollPane fx:id="scpGrid" fitToHeight="true" fitToWidth="true"
focusTraversable="false" BorderPane.alignment="CENTER">
<content>
<StackPane fx:id="stkPane">
<children>
<Pane fx:id="pane" StackPane.alignment="CENTER">
<children>
<GridPane alignment="CENTER" hgap="1.0" vgap="1.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
<ColumnConstraints halignment="CENTER" hgrow="NEVER"
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints maxHeight="40.0" minHeight="40.0"
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
</rowConstraints>
</GridPane>
</children>
</Pane>
</children>
</StackPane>
</content>
</ScrollPane>
</center>
</BorderPane>
在“不正确”的版本中,您在 的声明中有
fitToWidth="true"
和。fitToHeight="true"
ScrollPane
这将强制滚动窗格的内容适应视口的宽度和高度,这样它就永远不会大于视口,因此滚动条永远不会出现。请参阅这些属性的文档。
FXML 的“正确”版本没有这些设置,因此内容(通常)将调整为其首选大小,从而允许滚动条在必要时出现。
代码中还有许多其他异常。例如,您的某些列约束有
minWidth="40" maxWidth="40" prefWidth="100"
,这显然没有意义。无论如何,对这些大小(或者,的大小BorderPane
,这两个版本之间也不同)进行硬编码通常不是一个好主意:percentWidth
如果您想强制所有列具有相同的宽度,请使用。(还有其他方法可以实现这一点。)在 FXML 中的Pane
和StackPane
周围的元素也完全是多余的GridPane
。