2013-08-16 86 views
2

由於某些原因,我的窗格:gas-pumps-header和coffee-house-header不會在窗口重新調整大小時拉伸。我使用的是JavaFX Scene Builder,而我是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> 
+0

你期望什麼樣的拉伸?例如,在多大程度上,拉伸最大尺寸時應該有最小尺寸?您可以編輯您的問題以添加更多信息。 – jewelsea

+0

@jewelsea你是對的。我編輯了這個問題。 –

回答

1

玩過不同種類的窗格後,我設法讓它工作。

我使用了一個BorderPane,並將子節點添加到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> 
0

氣體泵頭已經作爲應用程序窗口大小的變化自動調整。在SceneBuilder中打開你的fxml文件(我在win7上使用1.1-b28),點擊SceneBuilder Hierarchy窗格中的gas-pumps-header窗格,將樣式設置爲-fx-border-color:red;選擇SceneBuilder菜單項Preview |顯示預覽和窗口並調整預覽窗口的大小,當您調整預覽窗口大小時,將看到氣泵頭變化大小的紅色邊框。

我很確定上述內容對您沒有任何幫助,因爲您可能對佈局有些問題,我無法從您的問題中理解。

一般來說,我會建議使用適當的窗格子類而不是窗格作爲大多數佈局,因爲窗格子類將爲您的佈局工作做更多工作。也許在你的情況下,HBox會更合適。