如何在VBox JavaFX中调整子锚点的大小

如何在VBox JavaFX中调整子锚点的大小,javafx,scenebuilder,Javafx,Scenebuilder,这是我的主要用户界面 我在AnchorPane中有一个VBox Fxid:datePane。Vbox是蓝色的 单击带有“添加”标签的按钮时,第二个.fxml文件将加载到VBox Fxid:datePane中 加载的fxml文件采用Vbox的高度和宽度 第二个.fxml有一个红色的VBox 但是,如果我调整屏幕大小,加载的文件不会相应地改变其高度和宽度。 如何确保加载的fxml文件采用其父VBox Fxid:datePane的宽度和高度 这是我的FxmlController.java文件 @FX

这是我的主要用户界面

我在AnchorPane中有一个VBox Fxid:datePane。Vbox是蓝色的

单击带有“添加”标签的按钮时,第二个.fxml文件将加载到VBox Fxid:datePane中

加载的fxml文件采用Vbox的高度和宽度

第二个.fxml有一个红色的VBox

但是,如果我调整屏幕大小,加载的文件不会相应地改变其高度和宽度。

如何确保加载的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();
   }

}
@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);
}
这是我的Main.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();
   }

}
@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>
<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>
<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>
这是我的第二个.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>
<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>
<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>
这是我的第三个.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>
<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>
<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>

我解决了这个问题。我只需要将main.fxml中second.fxml中的VBox和VBox Fxid:datePane的值设置为

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

它成功了。

将所有约束设置为零。