簡體   English   中英

使用FXML(JavaFX)中的窗口調整內容大小

[英]Resize content with window in FXML (JavaFX)

在FXML(JavaFX)中按用戶調整大小后,如何獲得新的窗口大小? 我已經搜索了“ onResizeWindow”方法,但是什么也沒找到。

我有一個簡單的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>

我發現了如何做到這一點:我必須使用一個包含PaneStackPane ,如下所示:

<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>

您可以聽舞台的寬度和高度:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM