Javafx 调整窗口大小时拉伸窗格

Javafx 调整窗口大小时拉伸窗格,javafx,fxml,pane,Javafx,Fxml,Pane,由于某些原因,我的两个窗格:汽油泵顶部和咖啡馆顶部在重新调整窗口尺寸时不会拉伸。我使用的是JavaFX场景生成器,我是JavaFX新手(如果答案很简单的话…) 我希望窗格向两侧(左侧和右侧)延伸,并填充整个窗口宽度 这是我的FXML <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> &

由于某些原因,我的两个窗格:汽油泵顶部和咖啡馆顶部在重新调整窗口尺寸时不会拉伸。我使用的是JavaFX场景生成器,我是JavaFX新手(如果答案很简单的话…)

我希望窗格向两侧(左侧和右侧)延伸,并填充整个窗口宽度

这是我的FXML

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="768.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <HBox id="action-buttons-section" alignment="CENTER_LEFT" layoutY="14.0" spacing="5.0" 

AnchorPane.leftAnchor="14.0">
      <children>
        <Button mnemonicParsing="false" text="Add Customer" />
        <Button mnemonicParsing="false" text="Move Customer" />
      </children>
    </HBox>
    <HBox id="fuel-pool-section" alignment="CENTER_LEFT" layoutY="14.0" spacing="5.0" AnchorPane.rightAnchor="16.0">
      <children>
        <Label text="Main Fuel Pool" />
        <ProgressBar prefHeight="20.0" prefWidth="200.0" progress="0.0" />
        <Button mnemonicParsing="false" text="Fill" />
      </children>
    </HBox>
    <VBox alignment="CENTER" spacing="10.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="44.0">
      <children>
        <VBox alignment="CENTER" spacing="5.0">
          <children>
            <Pane id="gas-pumps-header" prefHeight="100.0" prefWidth="996.0"> <!-- first one -->
              <children>
                <Label layoutX="14.0" layoutY="15.0" text="Gas Pumps">
                  <font>
                    <Font name="System Bold" size="16.0" fx:id="x2" />
                  </font>
                </Label>
                <HBox id="gas-pumps-income-section" alignment="CENTER_RIGHT" layoutX="893.0" layoutY="15.0" spacing="5.0">
                  <children>
                    <Label text="Income">
                      <font>
                        <Font size="14.0" fx:id="x1" />
                      </font>
                    </Label>
                    <Label fx:id="gasPumpsIncomeValue" font="$x1" text="Value" />
                  </children>
                </HBox>
              </children>
            </Pane>
            <HBox id="gas-pumps-section" prefHeight="378.0" prefWidth="996.0" styleClass="layout" VBox.vgrow="ALWAYS">
              <padding>
                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x3" />
              </padding>
            </HBox>
          </children>
        </VBox>
        <VBox alignment="CENTER" spacing="5.0">
          <children>
            <Pane id="coffee-house-header" prefHeight="100.0" prefWidth="996.0"> <!-- second one -->
              <children>
                <Label font="$x2" layoutX="14.0" layoutY="15.0" text="Coffee House" />
                <HBox id="coffee-house-income-section" alignment="CENTER_RIGHT" layoutX="893.0" layoutY="15.0" spacing="5.0">
                  <children>
                    <Label font="$x1" text="Income" />
                    <Label fx:id="coffeeHouseIncomeValue" font="$x1" text="Value" />
                  </children>
                </HBox>
              </children>
            </Pane>
            <HBox id="coffee-house-section" padding="$x3" prefHeight="378.0" prefWidth="996.0" styleClass="layout" VBox.vgrow="ALWAYS" />
          </children>
        </VBox>
      </children>
    </VBox>
  </children>
  <stylesheets>
    <URL value="@style.css" />
  </stylesheets>
</AnchorPane>

随着应用程序窗口大小的变化,气体泵标题已自动调整大小

在SceneBuilder中打开fxml文件(我在win7上使用了1.1-b28),单击SceneBuilder层次结构窗格中的气体泵标题窗格,将样式设置为-fx边框颜色:红色;选择SceneBuilder菜单项Preview | Show Preview and Window(显示预览和窗口)并调整预览窗口的大小,您将看到气体泵标题的红色边框在调整预览窗口的大小时改变大小

我很确定上面的内容对你没有多大帮助,因为你可能对布局有一些问题,我无法从你的问题中理解


一般来说,我建议对大多数布局使用合适的Pane子类,而不是Pane子类,因为Pane子类将为您的布局工作做更多的工作。也许在你的情况下,HBox更合适。

在玩了不同类型的窗格后,我设法让它工作起来

我使用了一个BorderPane,并在BorderPane的左侧和右侧添加了子节点。现在,当我重新调整窗口的大小时,每个元素都会重新定位到它在起始布局中的相对位置

这是边框部分

<BorderPane id="gas-pumps-header" VBox.vgrow="SOMETIMES">
    <left>
        <Label text="Gas Pumps" BorderPane.alignment="CENTER_LEFT">
            <font>
                <Font name="System Bold" size="16.0" fx:id="x2" />
            </font>
        </Label>
    </left>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x3" />
    </padding>
    <right>
        <HBox id="gas-pumps-income-section" alignment="CENTER_RIGHT" spacing="5.0" BorderPane.alignment="CENTER_RIGHT">
            <children>
                <Label text="Income">
                    <font>
                        <Font size="14.0" fx:id="x1" />
                    </font>
                </Label>
                <Label fx:id="gasPumpsIncomeValue" font="$x1" text="Value" />
            </children>
        </HBox>
    </right>              
</BorderPane>


你希望做什么样的伸展运动?例如,在什么方向,通过多少,应该有一个最小尺寸的拉伸最大尺寸?您可以编辑您的问题以添加更多信息。@sea您是对的。我已经编辑了这个问题。