2016-04-15 82 views
0

FXML(JavaFX)中用戶在調整大小後如何獲得新的窗口大小?我搜索了一個「onResizeWindow」方法,但我什麼也沒找到。在FXML(JavaFX)中用窗口調整內容大小

我有一個簡單的Pane,我希望它調整自己的大小,當用戶調整窗口的大小。你如何調整窗格的大小ToolBar?

這裏是我的FXML代碼:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testJavaFXML.Controller"> 
    <padding> 
     <Insets bottom="200.0" left="200.0" right="200.0" top="200.0" /> 
    </padding> 
    <children> 
     <ToolBar prefWidth="600.0"> 
     <items> 
      <Button fx:id="buttonPlay" mnemonicParsing="false" onAction="#handlePlayButtonAction" text="Play" /> 
      <Button fx:id="buttonPause" mnemonicParsing="false" onAction="#handlePauseButtonAction" text="Pause" /> 
      <Button fx:id="buttonStop" mnemonicParsing="false" onAction="#handleStopButtonAction" text="Stop" /> 
     </items> 
     </ToolBar> 
     <Label fx:id="labelHelloWorld" layoutX="250.0" layoutY="187.0" text="Hello World!" /> 
    </children> 
</Pane> 

我發現如何做到這一點:我必須使用StackPane其中包括Pane,這樣的:

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Pane prefHeight="200.0" prefWidth="200.0"> 
     <children> 
      <ToolBar prefWidth="600.0"> 
       <items> 
        <Button fx:id="buttonPlay" mnemonicParsing="false" onAction="#handlePlayButtonAction" text="Play" /> 
        <Button fx:id="buttonPause" mnemonicParsing="false" onAction="#handlePauseButtonAction" text="Pause" /> 
        <Button fx:id="buttonStop" mnemonicParsing="false" onAction="#handleStopButtonAction" text="Stop" /> 
       </items> 
      </ToolBar> 
     </children> 
     </Pane> 
     <Label fx:id="labelHelloWorld" text="Hello World!" /> 
    </children> 
</StackPane> 

回答

0

你可以聽的寬度和舞臺的高度:

stage.widthProperty().addListener((ov, oldWidth, newWidth) -> someAction); 

但是更好的方法是使用適當的LayoutPane例如BorderPane處理調整大小