Javafx 无法将按钮放置在右下角

Javafx 无法将按钮放置在右下角,javafx,scenebuilder,Javafx,Scenebuilder,在场景生成器中,预览按钮位于准确的位置,即右下角,没有任何间隙,但当我运行应用程序时,有间隙。 按钮位于窗格上,窗格放置在锚定窗格的顶部。 如何解决此问题? 当我将其他组件放置在最右侧或最底部时,也会发生同样的情况 请检查一下样品。请注意SceneBuiler中的最小宽度、最小高度、预宽度、预高度、最大宽度、最大高度、vgrow、hgrow属性。 主要的概念是,您使用的窗格类型可以为您提供所需的布局。它不应该硬编码 <?xml version="1.0" encoding="UTF-8"

在场景生成器中,预览按钮位于准确的位置,即右下角,没有任何间隙,但当我运行应用程序时,有间隙。

按钮位于窗格上,窗格放置在锚定窗格的顶部。 如何解决此问题?
当我将其他组件放置在最右侧或最底部时,也会发生同样的情况

请检查一下样品。请注意SceneBuiler中的最小宽度、最小高度、预宽度、预高度、最大宽度、最大高度、vgrow、hgrow属性。 主要的概念是,您使用的窗格类型可以为您提供所需的布局。它不应该硬编码

<?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.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox style="-fx-background-color: yellow;">
         <children>
            <StackPane VBox.vgrow="ALWAYS">
               <children>
                  <Button mnemonicParsing="false" text="Menu Button" />
               </children>
            </StackPane>
            <StackPane VBox.vgrow="ALWAYS">
               <children>
                  <Button mnemonicParsing="false" text="Menu Button" />
               </children>
            </StackPane>
            <StackPane layoutX="10.0" layoutY="210.0" VBox.vgrow="ALWAYS">
               <children>
                  <Button mnemonicParsing="false" text="Menu Button" />
               </children>
            </StackPane>
            <StackPane layoutX="10.0" layoutY="276.0" VBox.vgrow="ALWAYS">
               <children>
                  <Button mnemonicParsing="false" text="Menu Button" />
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </StackPane>
            <StackPane layoutX="10.0" layoutY="310.0" VBox.vgrow="ALWAYS">
               <children>
                  <Button mnemonicParsing="false" text="Menu Button" />
               </children>
            </StackPane>
         </children>
      </VBox>
      <VBox style="-fx-background-color: red;" HBox.hgrow="ALWAYS">
         <children>
            <StackPane prefHeight="150.0" prefWidth="200.0" style="-fx-background-color: green;" VBox.vgrow="ALWAYS">
               <children>
                  <GridPane maxHeight="-Infinity" maxWidth="-Infinity" style="-fx-background-color: purple;">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Label" />
                        <TextField GridPane.columnIndex="1" />
                        <TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
                        <Label text="Label" GridPane.rowIndex="1" />
                        <Label text="Label" GridPane.rowIndex="2" />
                     </children>
                  </GridPane>
               </children>
            </StackPane>
            <HBox alignment="TOP_RIGHT">
               <children>
                  <Button mnemonicParsing="false" text="Button" />
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</HBox>


是否在按钮的布局部分设置了约束?这可能与此有关。检查布局x和按钮的布局以及锚泊烷的pref size x和y,如果相同,则按钮必须位于窗格的左下角。

请提供fxml。它几乎有700行。我可以用你的电子邮件地址发送fxml文件吗?在这里发布。我试过了,但是我不能。当我添加我的fxml代码时,它的最大限制为30000个字符。它有43000个字符。发布的问题实际上不可回答。但是,使用
窗格
表明您没有使用响应性布局。在某些版本的JavaFX+OS组合中,场景实际上比首选大小更大,这就是为什么使用响应性布局非常重要的原因。例如,您可以使用
AnchorPane
将按钮定位到右下角,方法是直接将其添加为子按钮,并指定底部和右侧的锚定。不过,其他布局更可取,因为,
AnchorPane
不能防止子对象重叠……我理解了您试图用FXML代码解释的一些概念。我还做了另一个测试项目。我放置了锚窗格,并在其顶部再次使用了简单窗格,并在右下角添加了一个按钮。现在,在预览模式和应用程序执行期间,该按钮都显示在相同的位置。这意味着窗格和容器在这里不是问题,而且我不需要担心窗口大小。在我的情况下,它不应该像您的一样灵活。我仍然不确定在执行过程中导致窗格上的控件从最右侧向左侧移动一点的实际问题。使用“场景视图”在运行时调试节点的位置。我已经检查过没有填充或任何内容。检查锚定窗格及其顶部窗格的x和y的pref大小。我还检查了按钮和其他控件pref size。尝试将其最大和最小大小设置为pref size,但仍然没有成功