2016-04-15 151 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处理调整大小