fxml加载程序异常“已经指定了根值”我从未指定过根值

时间:2019-04-18 00:54:33

标签: javafx fxml fxmlloader

我正在尝试使超链接打开新窗口。我已经从代码的其他部分复制了fxml loader方法,这些方法可以正常工作。由于某种原因,我遇到了一个fxml loader异常问题,该问题表明已经指定了根值。我没有在代码中的任何地方指定根值。我也没有在fxml中指定控制器,因为关于stackoverflow的其他答案都说这可能是导致问题的原因。

这是正在加载新Window的类。尝试加载"/SellerProfileRegisterLayout.fxml"

时会发生此问题
public class SwitchToSellerProfile {

    @FXML private BorderPane borderPane = new BorderPane();
    @FXML private TextField usernameLogin = new TextField();
    @FXML private PasswordField passwordFieldLogin = new PasswordField();
    @FXML private MainController mainController;
    @FXML private Hyperlink sellerProfileRegisterHyperLink = new Hyperlink();

    public SwitchToSellerProfile(MainController mainController){
        this.mainController = mainController;
    }
    @FXML
    private void initialize(){
        sellerProfileRegisterHyperLink.setOnAction(e -> {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/SellerProfileRegisterLayout.fxml"));
                loader.setController(new SellerProfileRegisterController(this.mainController));
            this.mainController.getContentPane().getChildren().clear();
                this.mainController.getContentPane().getChildren().add(loader.load());
    // clicking the error always takes me here to the loader.load()
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }
}

SwitchToSellerProfile的fxml

<BorderPane prefHeight="400.0" prefWidth="550.0" 
            xmlns="http://javafx.com/javafx/8.0.172-ea" 
            xmlns:fx="http://javafx.com/fxml/1">
    <center>
      <Pane prefHeight="200.0" prefWidth="441.0" 
            BorderPane.alignment="CENTER">
        <children>
            <Label alignment="CENTER" contentDisplay="CENTER" 
                   layoutX="162.0" layoutY="71.0" prefHeight="46.0" prefWidth="228.0" 
                   text="Business Profile Sign In">
                <font>
                    <Font size="20.0" />
                </font>
            </Label>
            <TextField fx:id="usernameLogin" layoutX="192.0" layoutY="132.0" promptText="Business Username" />
            <PasswordField fx:id="passwordFieldLogin" layoutX="192.0" layoutY="167.0" promptText="Business Password" />
            <Hyperlink fx:id="forgotPasswordHyperlink" layoutX="218.0" layoutY="200.0" prefHeight="25.0" prefWidth="131.0" text="Forgot Password? " />
            <Label fx:id="incorrectLabel" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="190.0" text="Wrong username or password" textFill="RED" visible="false" />
            <Button fx:id="btnLogin" layoutX="234.0" layoutY="235.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="83.0" text="Sign In" />
            <Label layoutX="192.0" layoutY="274.0" text="Don't have one?">
               <font>
                  <Font size="12.0" />
               </font>
            </Label>
            <Hyperlink fx:id="sellerProfileRegisterHyperLink" layoutX="283.0" layoutY="271.0" text="Register here">
               <font>
                  <Font size="12.0" />
               </font>
            </Hyperlink>
        </children>
      </Pane>
   </center>
</BorderPane>

注册控制器及其fxml

public class SellerProfileRegisterController {

    private MainController mainController;
    @FXML
    private TextField textField = new TextField();
    @FXML private TextField businessEmail = new TextField();
    @FXML private Button registerBtn = new Button();
    @FXML private Hyperlink goBackLink = new Hyperlink();

    public SellerProfileRegisterController(MainController mainController){
        this.mainController = mainController;
    }
    @FXML private void initialize(){
        registerBtn.setText("Hello"); 
        // just to see if it works hint: it doesn't 
    }
}
<BorderPane prefHeight="400.0" prefWidth="600.0" 
            xmlns="http://javafx.com/javafx/8.0.172-ea" 
            xmlns:fx="http://javafx.com/fxml/1">
     <center>
      <Pane prefHeight="200.0" prefWidth="200.0" 
            BorderPane.alignment="CENTER">
          <children>
              <Label alignment="CENTER" contentDisplay="CENTER" 
                     layoutX="181.0" layoutY="62.0" prefHeight="46.0" prefWidth="225.0" 
                     text="Seller Profile Register">
                  <font>
                      <Font size="20.0" />
                  </font>
              </Label>
            <TextField fx:id="textField" layoutX="210.0" layoutY="121.0" promptText="Business Name" />
            <TextField fx:id="businessEmail" layoutX="210.0" layoutY="162.0" promptText="Business Email Address" />
            <Button layoutX="229.0" layoutY="278.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="130.0" text="Register Account" fx:id="registerBtn" />
            <Hyperlink layoutX="210.0" layoutY="311.0" text="Go back? " fx:id="goBackLink">
               <font>
                  <Font size="10.0" />
               </font></Hyperlink>
            <PasswordField layoutX="210.0" layoutY="200.0" promptText="Password" />
            <PasswordField layoutX="211.0" layoutY="238.0" promptText="Confirm Password" />
          </children>
      </Pane>
     </center>
</BorderPane>

错误消息:

 javafx.fxml.LoadException: Root value already specified.
 user/IdeaProjects/FinalStore/target/classes/SellerProfileRegisterLayout.fxml
 user/IdeaProjects/FinalStore/target/classes/SellerProfileRegisterLayout.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at Store.SwitchToSellerProfile.lambda$initialize$0(SwitchToSellerProfile.java:38)

第38行是我在上面评论的loader.load()

因此,理想情况下,SellerProfileRegisterController会加载而不是什么也不会发生并且会向控制台显示错误消息。我不确定是什么原因造成的。谢谢!

0 个答案:

没有答案