2017-04-03 43 views
0

內調整孩子AnchorPanes這是我的主UI Main UI如何將一個垂直框的JavaFX

我有一個垂直框:一個AnchorPane內(Fxid datePane)。 Vbox是藍色的。

當點擊帶有「添加」標籤的按鈕時,會將「second.fxml」文件加載到VBox(Fxid:datePane)中。

加載的fxml文件取得了Vbox的高度和寬度。

second.fxml有一個VBox,它是紅色的。

但是,如果我調整屏幕大小,加載的文件不會相應地改變其高度和寬度。 After resizing the screen

我如何確保加載的fxml文件採用其父VBox(Fxid:datePane)的寬度和高度?

這是我FxmlController.java文件

@FXML 
private Button btnAdd; 

@FXML 
private VBox datePane; 

@FXML 
    private VBox vbxsecond; 

@FXML 
private VBox vbxThird; 

@FXML 
private AnchorPane apLoader; 

VBox v; 

@Override 
public void initialize(URL arg0, ResourceBundle arg1) { 
    // TODO Auto-generated method stub 
    double w = apLoader.getWidth(); 
    double h = apLoader.getHeight(); 
    System.out.println(h); 
} 

public void setDataPane(Node node) { 
    // update VBox with new form(FXML) depends on which button is clicked 
    datePane.getChildren().setAll(node); 

} 

public VBox fadeAnimate(String url) throws IOException { 
    v = (VBox) FXMLLoader.load(getClass().getResource(url)); 
    FadeTransition ft = new FadeTransition(Duration.millis(1500)); 
    ft.setNode(v); 
    ft.setFromValue(0.1); 
    ft.setToValue(1); 
    ft.setCycleCount(1); 
    ft.setAutoReverse(false); 
    double w = datePane.getWidth(); 
    double h = datePane.getHeight(); 
    v.setPrefHeight(h); 
    v.setPrefWidth(w); 

    ft.play(); 
    return v; 


} 

public void loadPane() throws IOException { 

    try{ 

     setDataPane(fadeAnimate("/resources/second.fxml")); 


    }catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

} 

public void loadPane2() throws IOException { 

    try{ 

     setDataPane(fadeAnimate("/resources/third.fxml")); 


    }catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

} 

這是我的Main.java文件

@FXML 
private VBox vbxsecond; 

@FXML 
    private VBox datePane; 

@Override 
public void start(Stage primaryStage) { 
    try { 
     //BorderPane root = new BorderPane(); 
     Parent root = FXMLLoader.load(getClass().getResource("/resources/dashboard.fxml")); 
     Scene scene = new Scene(root); 
     scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     //primaryStage.setMaximized(true); 
     primaryStage.show();    
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    launch(args); 
} 

這是我dashboard.fxml文件

AnchorPane minHeight="800.0" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FxmlController"> 
    <children> 
     <SplitPane dividerPositions="0.29797979797979796" layoutX="14.0" layoutY="14.0" prefHeight="800.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <items> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="798.0" prefWidth="192.0"> 
       <children> 
        <Button fx:id="btnAdd" layoutX="22.0" layoutY="122.0" mnemonicParsing="false" onAction="#loadPane" prefHeight="25.0" prefWidth="51.0" text="Add" AnchorPane.leftAnchor="22.0" AnchorPane.topAnchor="122.0" /> 
        <Button fx:id="btnadd2" layoutX="57.0" layoutY="281.0" mnemonicParsing="false" onAction="#loadPane2" text="ADD second" /> 
       </children> 
      </AnchorPane> 
      <AnchorPane fx:id="apLoader" minHeight="0.0" minWidth="0.0" prefHeight="158.0" prefWidth="814.0"> 
       <children> 
        <VBox fx:id="datePane" layoutX="10.0" layoutY="23.0" prefHeight="798.0" prefWidth="628.0" style="-fx-background-color: Blue;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
       </children> 
      </AnchorPane> 
     </items> 
     </SplitPane> 
    </children> 
</AnchorPane> 

這是我第二.fxml文件

<VBox fx:id="vbxsecond" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="814.0" style="-fx-background-color: Red;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Label fx:id="vbxSecond" text="HELLOOO" VBox.vgrow="ALWAYS"> 
     <font> 
      <Font size="96.0" /> 
     </font> 
     </Label> 
    </children> 
</VBox> 

這是我third.fxml文件

<VBox fx:id="vbxThird" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="814.0" style="-fx-background-color: Pink;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Label text="Third HELOO" VBox.vgrow="ALWAYS"> 
     <font> 
      <Font size="85.0" /> 
     </font> 
     </Label> 
    </children> 
</VBox> 
+0

將所有約束條件設置爲零。 – Sedrick

回答

0

我解決了這個問題。我不得不設置垂直框的值在second.fxml和垂直框(Fxid:datePane)在main.fxml到

AnchorPane.topAnchor="0.0" 
AnchorPane.bottomAnchor="0.0" 
AnchorPane.leftAnchor="0.0" 
AnchorPane.rightAnchor="0.0" 

和它的工作。