英文:
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("LoginMain.fxml"));
primaryStage.setTitle("Ford Car Review");
primaryStage.setScene(new Scene(root, 550, 450));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here's my loginMain.fxml file
<?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">
<TextField fx:id="username" layoutX="195.0" layoutY="155.0" prefHeight="25.0" prefWidth="197.0"
promptText="Enter username" style="-fx-background-color: white;"><font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField fx:id="password" layoutX="195.0" layoutY="200.0"
prefHeight="25.0" prefWidth="197.0"
promptText="Enter password" style="-fx-background-color: white;">
<font>
<Font size="14.0" />
</font>
</PasswordField>
<Button fx:id="signUpButton" layoutX="446.0" layoutY="368.0"
onAction="#signUp" prefHeight="30.0"
prefWidth="70.0" text="Sign Up">
<font>
<Font size="14.0" />
</font>
</Button>
<Button fx:id="login" layoutX="297.0" layoutY="267.0" onAction="#loginAction"
prefHeight="30.0" prefWidth="61.0" text="Login" />
<CheckBox fx:id="remember" layoutX="152.0" layoutY="267.0" prefHeight="30.0"
prefWidth="107.0" text="Remember Me" textFill="WHITE" />
<Button fx:id="forgotPassword" layoutX="167.0" layoutY="316.0" mnemonicParsing="false"
prefHeight="25.0" prefWidth="197.0" style="-fx-background-color: #2D3447;"
text="Forgot Your Password?"
textFill="WHITE" underline="true" />
<ImageView fitHeight="83.0" fitWidth="129.0" layoutX="152.0"
layoutY="23.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/images.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="35.0" layoutX="152.0" layoutY="155.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/username.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="152.0" layoutY="200.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/password.png" />
</image>
</ImageView>
<Label layoutX="285.0" layoutY="23.0" prefHeight="30.0" prefWidth="84.0"
text="FORD" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="285.0" layoutY="53.0" prefHeight="25.0" prefWidth="70.0"
text="CAR" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="284.0" layoutY="81.0" text="REVIEW" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<ImageView fx:id="progress" fitHeight="83.0" fitWidth="125.0" layoutX="210.0" layoutY="341.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/source.gif" />
</image>
</ImageView>
</AnchorPane>
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 -> System.out.println("Login Successfully"));
pt.play();
}
@FXML
public void signUp() {
try {
login.getScene().getWindow().hide();
Stage signUpStage = new Stage();
Parent root = FXMLLoader.load(Main.class.getResource("/sample/SignUP.fxml"));
Scene scene = new Scene(root);
signUpStage.setScene(scene);
signUpStage.show();
signUpStage.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here's my SignUP.fxml file:
<?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">
<TextField layoutX="160.0" layoutY="145.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Username" style="-fx-background-color: white;">
<font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField layoutX="160.0" layoutY="190.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Password" style="-fx-background-color: #2D34white47;">
<font>
<Font size="14.0" />
</font>
</PasswordField>
<RadioButton layoutX="168.0" layoutY="248.0" mnemonicParsing="false" selected="true"
text="Male" textFill="#a0a2ab">
<font>
<Font size="14.0" />
</font>
<toggleGroup>
<ToggleGroup fx:id="gender" />
</toggleGroup>
</RadioButton>
<RadioButton layoutX="239.0" layoutY="248.0" mnemonicParsing="false" text="Female"
textFill="#a0a2ab" toggleGroup="$gender">
<font>
<Font size="14.0" />
</font>
</RadioButton>
<TextField layoutX="160.0" layoutY="289.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Location">
<font>
<Font size="14.0" />
</font>
</TextField>
<Button layoutX="136.0" layoutY="350.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="239.0"
onAction="#signUP" style="-fx-background-color: #2196f3;" text="Sign UP" textFill="#ddd1d1">
<font>
<Font size="14.0" />
</font>
</Button>
<ImageView fitHeight="30.0" fitWidth="33.0" layoutX="121.0" layoutY="243.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/gender.png" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="145.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/username.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="190.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/password.png" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="289.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/location.png" />
</image>
</ImageView>
<Button layoutX="448.0" layoutY="36.0" mnemonicParsing="false" prefHeight="30.0"
prefWidth="56.0" style="-fx-background-color: #2196f3;" text="Login" textFill="WHITE" />
<ImageView fitHeight="89.0" fitWidth="131.0" layoutX="121.0" layoutY="14.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/images.jpg" />
</image>
</ImageView>
<Label layoutX="260.0" layoutY="14.0" prefHeight="30.0" prefWidth="84.0"
text="FORD" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="260.0" layoutY="43.0" prefHeight="30.0" prefWidth="70.0"
text="CAR" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="260.0" layoutY="73.0" prefHeight="30.0" prefWidth="92.0"
text="REVIEW" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<ImageView fx:id="progress" fitHeight="80.0" fitWidth="105.0" layoutX="215.0"
layoutY="382.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/source.gif" />
</image>
</ImageView>
<RadioButton layoutX="320.0" layoutY="250.0" mnemonicParsing="false" text="Other"
textFill="#c3b5b5">
<font>
<Font size="14.0" />
</font>
</RadioButton>
</AnchorPane>
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 -> System.out.println("Sign Up Successful!"));
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
的文档中所说的:
> 控制器初始化接口。
>
> 注意 此接口已被控制器自动注入 location
和 resources
属性所取代 [已加重]。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() {} // 如果需要的话
}
这实际上使得 location
和 resources
在使用 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论