簡體   English   中英

(JavaFX)(SceneBuilder)如果將所有內容都設置為AnchorPane,則在調整窗口大小時如何自動調整場景大小?

[英](JavaFX)(SceneBuilder) How can I automatically resize the scene when the window is resized if everything is set into a AnchorPane?

我在fxml文件中的所有代碼都設置在錨定窗格中,我想知道是否有一種簡單的方法可以在調整窗口大小時自動調整整個場景的大小?

FXML文件:

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

<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="csgocaseopener.InventoryController">
   <children>
      <ImageView fitHeight="400.0" fitWidth="600.0" pickOnBounds="true">
         <image>
            <Image url="@back.png" />
         </image>
      </ImageView>
      <AnchorPane layoutX="38.0" layoutY="65.0" prefHeight="270.0" prefWidth="525.0" />
      <ImageView fx:id="backbtn" fitHeight="45.0" fitWidth="45.0" layoutY="-1.0" onMouseClicked="#handleInventoryBack" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="-1.0">
         <image>
            <Image url="@backbtn.png" />
         </image>
      </ImageView>
   </children>
</fx:root>

ImaveView適合於width / height屬性的ImaveView綁定到Stage width / height屬性。 該解決方案將無法保持圖像比例,因此會變形。

ImageView imageView = new ImageView(new Image(getClass().getResourceAsStream("test.png")));
//imageView.setPreserveRatio(true);  //uncomment to keep image ratio.
imageView.fitHeightProperty().bind(stage.heightProperty());
imageView.fitWidthProperty().bind(stage.widthProperty());

暫無
暫無

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

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