关于loadExceptions和IllegalArgumentException的问题。

huangapple go评论117阅读模式
英文:

A Problem regarding loadExceptions and IllegalArguementException

问题

好的,下面是您提供的内容的翻译:


Ok,所以我设计了两个GUI界面,并且为每个界面都有两个控制器。基本上,第一个窗口/GUI(LoginMain.fxml)有一个注册按钮,当你点击它时,会打开另一个GUI(SignUP.fxml)。但出现了一个问题,当我将SignUP.fxml文件与其控制器关联起来时,点击注册按钮时它不会打开。但当我不关联它时,它可以正常工作。

这是我的主要方法:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("LoginMain.fxml"));
        primaryStage.setTitle("Ford Car Review");
        primaryStage.setScene(new Scene(root, 550, 450));
        primaryStage.show();
    }

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

这是我的loginMain.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="426.0" prefWidth="547.0" style="-fx-background-color: #2D3447; 
    -fx-border-color: yellow;"
            xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="sample.LoginController">
    <!-- 其他部分已省略 -->
</AnchorPane>

这是我的Login Controller类:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class LoginController {

    @FXML
    private Button signUpButton;
    @FXML
    private TextField username;
    @FXML
    private CheckBox remember;
    @FXML
    private Button login;
    @FXML
    private Button forgotPassword;
    @FXML
    private ImageView progress;
    @FXML
    private PasswordField password;

    public void initialize() {
        progress.setVisible(false);
    }

    @FXML
    public void loginAction() {
        progress.setVisible(true);
        PauseTransition pt = new PauseTransition();
        pt.setDuration(Duration.seconds(3));
        pt.setOnFinished(e -> System.out.println("Login Successfully"));
        pt.play();
    }

    @FXML
    public void signUp() {
        try {
            login.getScene().getWindow().hide();
            Stage signUpStage = new Stage();
            // 其他部分已省略
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的SignUP.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="462.0" prefWidth="547.0" style="-fx-background-color: #2D3447; 
    -fx-border-color: yellow;"
            xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="sample.SignUpController">
    <!-- 其他部分已省略 -->
</AnchorPane>

这是我的SignUpController类:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;

public class SignUpController {

    @FXML
    private AnchorPane parentPane;
    @FXML
    private Button login;
    @FXML
    private TextField name;
    @FXML
    private Button signUp;
    @FXML
    private RadioButton male;
    @FXML
    private RadioButton female;
    @FXML
    private RadioButton other;
    @FXML
    private TextField location;
    @FXML
    private ImageView progress;
    @FXML
    private PasswordField password;

    public void initialize() {
        progress.setVisible(false);
    }

    @FXML
    public void signUP() {
        progress.setVisible(true);
        PauseTransition pt = new PauseTransition();
        pt.setDuration(Duration.seconds(3));
        pt.setOnFinished(e -> System.out.println("Sign Up Successful!"));
        pt.play();
    }
}

最后,这是我收到的错误:

javafx.fxml.LoadException: 
/C:/Users/Owner/Desktop/JavaPrograms/GaveUp/out/production/GaveUp/sample/SignUP.fxml
    // 其他错误信息已省略

英文:

Ok, so I designed two guis and also have 2 controllers for each of them. So basically the first window/gui (LoginMain.fxml) has a sign up button and when you click it, it opens the other gui (SignUP.fxml). But for some reason when I associate the SignUP.fxml file with its controller it doesn't open up when I click the sign up button. But when I don't associate it, it works fine.

Here's my main method:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource(&quot;LoginMain.fxml&quot;));
    primaryStage.setTitle(&quot;Ford Car Review&quot;);
    primaryStage.setScene(new Scene(root, 550, 450));
    primaryStage.show();
}


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

Here's my loginMain.fxml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.image.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;?import javafx.scene.text.*?&gt;
&lt;AnchorPane prefHeight=&quot;426.0&quot; prefWidth=&quot;547.0&quot; style=&quot;-fx-background-color: #2D3447; 
-fx-border- color: yellow;&quot;
        xmlns=&quot;http://javafx.com/javafx/10.0.2-internal&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
        fx:controller=&quot;sample.LoginController&quot;&gt;
&lt;TextField fx:id=&quot;username&quot; layoutX=&quot;195.0&quot; layoutY=&quot;155.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;197.0&quot;
       promptText=&quot;Enter username&quot; style=&quot;-fx-background-color: white;&quot;&gt;&lt;font&gt;
       &lt;Font size=&quot;14.0&quot; /&gt;
       &lt;/font&gt;
       &lt;/TextField&gt;
       &lt;PasswordField fx:id=&quot;password&quot; layoutX=&quot;195.0&quot; layoutY=&quot;200.0&quot; 
       prefHeight=&quot;25.0&quot; prefWidth=&quot;197.0&quot;
       promptText=&quot;Enter password&quot; style=&quot;-fx-background-color: white;&quot;&gt;
       &lt;font&gt;
       &lt;Font size=&quot;14.0&quot; /&gt;
       &lt;/font&gt;
       &lt;/PasswordField&gt;
       &lt;Button fx:id=&quot;signUpButton&quot; layoutX=&quot;446.0&quot; layoutY=&quot;368.0&quot; 
       onAction=&quot;#signUp&quot;  prefHeight=&quot;30.0&quot;
              prefWidth=&quot;70.0&quot; text=&quot;Sign Up&quot;&gt;
        &lt;font&gt;
           &lt;Font size=&quot;14.0&quot; /&gt;
        &lt;/font&gt;
       &lt;/Button&gt;
       &lt;Button fx:id=&quot;login&quot; layoutX=&quot;297.0&quot; layoutY=&quot;267.0&quot; onAction=&quot;#loginAction&quot;
              prefHeight=&quot;30.0&quot; prefWidth=&quot;61.0&quot; text=&quot;Login&quot; /&gt;
       &lt;CheckBox fx:id=&quot;remember&quot; layoutX=&quot;152.0&quot; layoutY=&quot;267.0&quot; prefHeight=&quot;30.0&quot;
                prefWidth=&quot;107.0&quot; text=&quot;Remember Me&quot; textFill=&quot;WHITE&quot; /&gt;
       &lt;Button fx:id=&quot;forgotPassword&quot; layoutX=&quot;167.0&quot; layoutY=&quot;316.0&quot; mnemonicParsing=&quot;false&quot;
       prefHeight=&quot;25.0&quot; prefWidth=&quot;197.0&quot; style=&quot;-fx-background-color: #2D3447;&quot; 
       text=&quot;Forgot Your Password?&quot;
       textFill=&quot;WHITE&quot; underline=&quot;true&quot; /&gt;
       &lt;ImageView fitHeight=&quot;83.0&quot; fitWidth=&quot;129.0&quot; layoutX=&quot;152.0&quot; 
        layoutY=&quot;23.0&quot; pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
       &lt;image&gt;
       &lt;Image url=&quot;@../img/images.jpg&quot; /&gt;
       &lt;/image&gt;
       &lt;/ImageView&gt;
       &lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;35.0&quot; layoutX=&quot;152.0&quot; layoutY=&quot;155.0&quot; 
       pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
       &lt;image&gt;
       &lt;Image url=&quot;@../img/username.jpg&quot; /&gt;
       &lt;/image&gt;
       &lt;/ImageView&gt;
       &lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;30.0&quot; layoutX=&quot;152.0&quot; layoutY=&quot;200.0&quot; 
       pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
       &lt;image&gt;
       &lt;Image url=&quot;@../img/password.png&quot; /&gt;
       &lt;/image&gt;
       &lt;/ImageView&gt;
       &lt;Label layoutX=&quot;285.0&quot; layoutY=&quot;23.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;84.0&quot;
       text=&quot;FORD&quot; textFill=&quot;#6958d7&quot;&gt;
       &lt;font&gt;
       &lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
       &lt;/font&gt;
       &lt;/Label&gt;
       &lt;Label layoutX=&quot;285.0&quot; layoutY=&quot;53.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;70.0&quot; 
       text=&quot;CAR&quot;     textFill=&quot;#6958d7&quot;&gt;
       &lt;font&gt;
       &lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
       &lt;/font&gt;
       &lt;/Label&gt;
       &lt;Label layoutX=&quot;284.0&quot; layoutY=&quot;81.0&quot; text=&quot;REVIEW&quot; textFill=&quot;#6958d7&quot;&gt;
       &lt;font&gt;
       &lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
       &lt;/font&gt;
       &lt;/Label&gt;
       &lt;ImageView fx:id=&quot;progress&quot; fitHeight=&quot;83.0&quot; fitWidth=&quot;125.0&quot; layoutX=&quot;210.0&quot; layoutY=&quot;341.0&quot;
          pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
       &lt;image&gt;
       &lt;Image url=&quot;@../img/source.gif&quot; /&gt;
       &lt;/image&gt;
       &lt;/ImageView&gt;
       &lt;/AnchorPane&gt;

Here's my Login Controller class:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;

public class LoginController {

@FXML
private Button signUpButton;
@FXML
private TextField username;
@FXML
private CheckBox remember;
@FXML
private Button login;
@FXML
private Button forgotPassword;
@FXML
private ImageView progress;
@FXML
private PasswordField password;

public void initialize() {
    progress.setVisible(false);
}

@FXML
public void loginAction() {
    progress.setVisible(true);
    PauseTransition pt = new PauseTransition();
    pt.setDuration(Duration.seconds(3));
    pt.setOnFinished(e -&gt; System.out.println(&quot;Login Successfully&quot;));
    pt.play();
}

@FXML
public void signUp() {
    try {
        login.getScene().getWindow().hide();
        Stage signUpStage = new Stage();
        Parent root = FXMLLoader.load(Main.class.getResource(&quot;/sample/SignUP.fxml&quot;));
        Scene scene = new Scene(root);
        signUpStage.setScene(scene);
        signUpStage.show();
        signUpStage.setResizable(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

Here's my SignUP.fxml file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.image.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;?import javafx.scene.text.*?&gt;
&lt;AnchorPane prefHeight=&quot;462.0&quot; prefWidth=&quot;547.0&quot; style=&quot;-fx-background-color: #2D3447; 
-fx-border-color: yellow;&quot;
        xmlns=&quot;http://javafx.com/javafx/10.0.2-internal&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
        fx:controller=&quot;sample.SignUpController&quot;&gt;
&lt;TextField layoutX=&quot;160.0&quot; layoutY=&quot;145.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;228.0&quot; 
 promptText=&quot;Enter Username&quot; style=&quot;-fx-background-color: white;&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/TextField&gt;
&lt;PasswordField layoutX=&quot;160.0&quot; layoutY=&quot;190.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;228.0&quot; 
promptText=&quot;Enter Password&quot; style=&quot;-fx-background-color: #2D34white47;&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/PasswordField&gt;
&lt;RadioButton layoutX=&quot;168.0&quot; layoutY=&quot;248.0&quot; mnemonicParsing=&quot;false&quot; selected=&quot;true&quot; 
text=&quot;Male&quot; textFill=&quot;#a0a2ab&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;toggleGroup&gt;
&lt;ToggleGroup fx:id=&quot;gender&quot; /&gt;
&lt;/toggleGroup&gt;
&lt;/RadioButton&gt;
&lt;RadioButton layoutX=&quot;239.0&quot; layoutY=&quot;248.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Female&quot; 
textFill=&quot;#a0a2ab&quot; toggleGroup=&quot;$gender&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/RadioButton&gt;
&lt;TextField layoutX=&quot;160.0&quot; layoutY=&quot;289.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;228.0&quot; 
promptText=&quot;Enter Location&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/TextField&gt;
&lt;Button layoutX=&quot;136.0&quot; layoutY=&quot;350.0&quot; mnemonicParsing=&quot;false&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;239.0&quot;
onAction=&quot;#signUP&quot; style=&quot;-fx-background-color: #2196f3;&quot; text=&quot;Sign UP&quot; textFill=&quot;#ddd1d1&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/Button&gt;
&lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;33.0&quot; layoutX=&quot;121.0&quot; layoutY=&quot;243.0&quot;
pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/gender.png&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;30.0&quot; layoutX=&quot;121.0&quot; layoutY=&quot;145.0&quot; 
pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/username.jpg&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;30.0&quot; layoutX=&quot;121.0&quot; layoutY=&quot;190.0&quot; 
pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/password.png&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;ImageView fitHeight=&quot;30.0&quot; fitWidth=&quot;30.0&quot; layoutX=&quot;121.0&quot; layoutY=&quot;289.0&quot;
pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/location.png&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;Button layoutX=&quot;448.0&quot; layoutY=&quot;36.0&quot; mnemonicParsing=&quot;false&quot; prefHeight=&quot;30.0&quot; 
 prefWidth=&quot;56.0&quot; style=&quot;-fx-background-color: #2196f3;&quot; text=&quot;Login&quot; textFill=&quot;WHITE&quot; /&gt;
&lt;ImageView fitHeight=&quot;89.0&quot; fitWidth=&quot;131.0&quot; layoutX=&quot;121.0&quot; layoutY=&quot;14.0&quot;
pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/images.jpg&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;Label layoutX=&quot;260.0&quot; layoutY=&quot;14.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;84.0&quot; 
text=&quot;FORD&quot; textFill=&quot;#7a6ae4&quot;&gt;
&lt;font&gt;
&lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
&lt;/font&gt;
&lt;/Label&gt;
&lt;Label layoutX=&quot;260.0&quot; layoutY=&quot;43.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;70.0&quot; 
text=&quot;CAR&quot; textFill=&quot;#7a6ae4&quot;&gt;
&lt;font&gt;
&lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
&lt;/font&gt;
&lt;/Label&gt;
&lt;Label layoutX=&quot;260.0&quot; layoutY=&quot;73.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;92.0&quot; 
text=&quot;REVIEW&quot; textFill=&quot;#7a6ae4&quot;&gt;
&lt;font&gt;
&lt;Font name=&quot;Century Schoolbook&quot; size=&quot;20.0&quot; /&gt;
&lt;/font&gt;
&lt;/Label&gt;
&lt;ImageView fx:id=&quot;progress&quot; fitHeight=&quot;80.0&quot; fitWidth=&quot;105.0&quot; layoutX=&quot;215.0&quot; 
layoutY=&quot;382.0&quot; pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@../img/source.gif&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;RadioButton layoutX=&quot;320.0&quot; layoutY=&quot;250.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Other&quot;
textFill=&quot;#c3b5b5&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;14.0&quot; /&gt;
&lt;/font&gt;
&lt;/RadioButton&gt;
&lt;/AnchorPane&gt;

Here's my SignUpController class:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;

public class SignUpController {

@FXML
private AnchorPane parentPane;
@FXML
private Button login;
@FXML
private TextField name;
@FXML
private Button signUp;
@FXML
private RadioButton male;
@FXML
private ToggleGroup gender;
@FXML
private RadioButton female;
@FXML
private RadioButton other;
@FXML
private TextField location;
@FXML
private ImageView progress;
@FXML
private PasswordField password;

public void initialize() {
    progress.setVisible(false);
}

@FXML
public void signUP() {
    progress.setVisible(true);
    PauseTransition pt = new PauseTransition();
    pt.setDuration(Duration.seconds(3));
    pt.setOnFinished(e -&gt; System.out.println(&quot;Sign Up Successful!&quot;));
    pt.play();
}
}

Finally, here's the error I Got:

javafx.fxml.LoadException: 
/C:/Users/Owner/Desktop/JavaPrograms/GaveUp/out/production/GaveUp/sample/SignUP.fxml

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at GaveUp/sample.LoginController.signUp(LoginController.java:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at         
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at   
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
         (DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at  
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
    (DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at 
    javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
    (CompositeEventHandler.java:86)
at 
    javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
    (CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased
    (ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord
    .handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent       
    (CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
    (CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
    (GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
    (GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2
    (GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock
    (QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent
    (GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:835)
    Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField 
    field sample.SignUpController.location to java.net.URL
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
    (UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
    (UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set
    (UnsafeObjectFieldAccessorImpl.java:81)
at java.base/java.lang.reflect.Field.set(Field.java:780)
at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
... 66 more

答案1

得分: 1

TL;DR: 在你的 SignUpController 类中有一个名为 "location" 的 TextField 字段。请将字段名称以及在 FXML 文件中对应的 fx:id 属性更改为除 "location" 或 "resources" 以外的其他名称,例如 "locationField"。


JavaFX 控制器可以配置在所有注入的 FXML 字段被注入后执行初始化操作。以前只能通过实现 Initializable 接口来实现这一点。该接口有一个方法,接受一个表示 FXML 文件位置的 URL 参数和一个 ResourceBundle 参数。但是这是 Initializable文档中所说的:

> 控制器初始化接口。
>
> 注意 此接口已被控制器自动注入 locationresources 属性所取代 [已加重]FXMLLoader 现在会自动调用控制器中由适当注解的无参 initialize() 方法。建议尽可能使用注入方法。

换句话说,与其实现接口,不如使用以下方法:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;

public class Controller {

    @FXML private URL location; // 如果需要的话
    @FXML private ResourceBundle resources; // 如果需要的话

    @FXML
    private void initialize() {} // 如果需要的话
}

这实际上使得 locationresources 在使用 FXML 时成为保留标识符,尽管并没有明确记录下来。这就是你遇到问题的地方。你的控制器有:

@FXML private TextField location;

你遇到的错误是:

Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField field sample.SignUpController.location to java.net.URL

因为 FXMLLoader 看到一个名为 "location" 的带有 @FXML 注解的字段,并尝试将 FXML 文件的位置分配给它。修复方法是在控制器和 FXML 文件中为你的 TextField 使用一个不同的标识符,而不是 "location"(或 "resources")。

英文:

TL;DR: You have a TextField field named "location" in your SignUpController class. Change the field name, along with the corresponding fx:id attribute in the FXML file, to something else other than "location" or "resources". For example, "locationField".


JavaFX controllers can be configured to perform initialization after all FXML-injected fields have been injected. This used to only be possible by implementing the Initializable interface. That interface has a single method which accepts a URL argument, representing the location of the FXML file, and a ResourceBundle argument. But here's what the documentation of Initializable has to say:

>Controller initialization interface.
>
>NOTE This interface has been superseded by automatic injection of location and resources properties into the controller [emphasis added]. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.

In other words, instead of implementing the interface it is preferred to use:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;

public class Controller {

    @FXML private URL location; // if needed
    @FXML private ResourceBundle resources; // if needed

    @FXML
    private void initialize() {} // if needed
}

This effectively makes location and resources reserved identifiers when using FXML, despite not being clearly documented as such. And that's where your problem is coming from. Your controller has:

@FXML private TextField location;

And the error you're getting is:

Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField field sample.SignUpController.location to java.net.URL

Because the FXMLLoader sees a field named "location" and annotated with @FXML and attempts to assign the location of the FXML file to it. The fix is to use a different identifier than "location" (or "resources") for your TextField, both in the controller and the FXML file.

huangapple
  • 本文由 发表于 2020年4月6日 00:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61045564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定