2017-02-11 900 views
0

我在fxml文件中的所有代碼都是在一個錨窗格中設置的,我想知道是否有一種簡單的方法來允許整個場景在窗口大小調整時自動調整大小?(JavaFX)(SceneBuilder)如果一切都設置爲AnchorPane,窗口大小調整時如何自動調整場景大小?

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

當調整窗口大小時,您的'AnchorPane'正在調整大小。在給定的代碼中,您用圖像填充整個「場景」,所以我假設您要調整圖像大小以填充窗口。 – MBec

+0

是的,我只是想要一切填滿窗口的大小 – Tresk33

回答

0

綁定ImaveView適合寬度/高度屬性Stage寬度/高度屬性。該解決方案不會保持圖像比例,因此它會變形。

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());