首页 > 解决方案 > 尽管已链接,但 Anchor Pane 仍为空

问题描述

我正在开发一个 JavaFX 应用程序,我按下一个按钮,一个新的 FXML 将加载到我的 AnchorPane 上。

我已经尝试遵循这两个教程([Link1][1])([Link2][2]),但我无法让它工作。

一般来说,我对 GenuineCoder 的教程没有任何问题,但这次完全不同。我知道如果你运行 initialize() 方法,所有标有@FXML 注解的组件都会被加载。就是这样。所有组件都已加载,并且除了 AnchorPane 之外它们都可以工作。

我已经确定了几个 fx:id 在 Controller 和 .fxml 文件中是相同的。那么,为什么我得到一个 NullPointer?

无论如何,这是我的代码:

控制器类

public class AdministracionController implements Initializable{
    

@FXML
private ImageView butLogout;

@FXML
private JFXDrawer sidebarBox;

@FXML
private JFXHamburger hamburger;

@FXML
public AnchorPane rootPane;

private boolean isOpened;
private int transitionRate=1;
private HamburgerBasicCloseTransition transition;

    @FXML
    public void barSwitch() throws IOException{
        VBox box = FXMLLoader.load(getClass().getResource("/grd/pfc/menu/admon/admonToolbar.fxml"));
        sidebarBox.setSidePane(box); 
        sidebarBox.setMinWidth(0);
        if(transition==null){
           transition = new HamburgerBasicCloseTransition(hamburger);
        }
        transition.setRate(transitionRate);
        transition.play();
        if(isOpened){
            sidebarBox.setMinWidth(0);
            sidebarBox.close();
        }else{
            sidebarBox.setMinWidth(150);
            sidebarBox.open();
        }
        isOpened=!isOpened;
        transitionRate*=-1;
    }

    @FXML
    public void logout(){
        System.out.println("Sesión cerrada");
    }
    
    //This is the method where I try to load my FXML into my AnchorPane
    public void addEmpleado() throws IOException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("/grd/pfc/menu/admon/addEmpleado.fxml"));
        rootPane.getChildren().setAll(pane);
    }

    
    public void addPerfil() {

    }

    
    public void editEmpleado() {

    }

    
    public void editPerfil() {

    }

    public void genRep() {

    }

    
    public void viewGraph() {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
       
    }

}

主要FXML

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

<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="grd.pfc.admon.AdministracionController">
   <children>
      <ImageView fx:id="butLogout" fitHeight="55.0" fitWidth="65.0" layoutX="828.0" layoutY="485.0" onMouseClicked="#logout" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../multimedia/Logout.png" />
         </image>
      </ImageView>
      <Text fill="#4059a9" layoutX="263.0" layoutY="57.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Administración">
         <font>
            <Font name="Arial Bold" size="48.0" />
         </font>
      </Text>
      <Separator layoutY="66.0" prefHeight="23.0" prefWidth="900.0" />
      <JFXDrawer fx:id="sidebarBox" defaultDrawerSize="150.0" layoutY="77.0" prefHeight="473.0" prefWidth="150.0" />
      <JFXHamburger fx:id="hamburger" layoutX="14.0" layoutY="40.0" onMouseClicked="#barSwitch" styleClass="jfx-hamburger" stylesheets="@admon.css" />
      <AnchorPane fx:id="rootPane" layoutX="150.0" layoutY="84.0" prefHeight="399.0" prefWidth="744.0" />
   </children>
</AnchorPane>

要加载的 FXML

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

<?import com.jfoenix.controls.JFXComboBox?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="406.0" prefWidth="751.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Text fill="#4059a9" layoutX="227.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Añadir Empleado">
         <font>
            <Font name="Arial Bold" size="36.0" />
         </font>
      </Text>
      <VBox layoutX="125.0" layoutY="60.0" prefHeight="310.0" prefWidth="231.0">
         <children>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" VBox.vgrow="ALWAYS">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Fecha contratación" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Salario" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" />
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Usuario" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" />
         </children>
      </VBox>
      <VBox layoutX="395.0" layoutY="60.0" prefHeight="310.0" prefWidth="231.0">
         <children>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Apellidos" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Fecha Fin Contrato" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Tipo Contrato" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXComboBox prefHeight="25.0" prefWidth="234.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXComboBox>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Contraseña" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXPasswordField>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXPasswordField>
         </children>
      </VBox>
      <Separator layoutY="54.0" prefHeight="5.0" prefWidth="751.0" />
   </children>
</AnchorPane>

给您带来不便深表歉意,非常感谢![1]:https ://www.youtube.com/watch?v=RJOza3XQk34 [2]:https ://www.youtube.com/watch?v=ttD35jEo-f0

标签: javafxborderpane

解决方案


推荐阅读