我正在尝试创建一个简单的JavaFX应用程序进行登录,我收到以下错误:
javafx.fxml.LoadException: /D:/iulia/universitate/an%20I,%20semestrul%20II/POO/Proiect/POO/bin/application/Loggin.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:15)
at om.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField field application.Login.usersg to javafx.scene.layout.AnchorPane
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at javafx.fxml.FXMLLoader.injectFields(Unknown Source)
at javafx.fxml.FXMLLoader.access$1600(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processValue(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
... 18 more
这是我的代码:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
public class Login {
@FXML
private Label lblog;
@FXML
private Label lbmem;
@FXML
private TextField user;
@FXML
private TextField name;
@FXML
private TextField lname;
@FXML
private TextField usersg;
@FXML
private PasswordField paswd;
@FXML
private PasswordField paswdsg;
@FXML
private PasswordField paswdsga;
private Client client;
public void log(ActionEvent event)
{
if(user.getText().equals("Iulia") && paswd.getText().equals("1234"))
lblog.setText("Login successful");
else
lblog.setText("Login failed. Username and password don't match");
}
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root= FXMLLoader.load(getClass().getResource("Loggin.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
FMXL资源:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="WELCOME TO PAY-APP" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Login">
<content>
<AnchorPane fx:id="usersg" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label fx:id="lblog" layoutX="43.0" layoutY="43.0" text="LOG IN " textAlignment="CENTER" />
<TextField fx:id="user" layoutX="103.0" layoutY="74.0" />
<PasswordField fx:id="paswd" layoutX="103.0" layoutY="131.0" />
<Button fx:id="logbt" layoutX="152.0" layoutY="197.0" mnemonicParsing="false" onAction="#log" text="LOG IN" />
<Label layoutX="26.0" layoutY="78.0" text="USERNAME:" />
<Label layoutX="25.0" layoutY="135.0" text="PASSWORD:" />
<Button fx:id="sgnbt" layoutX="377.0" layoutY="280.0" mnemonicParsing="false" text="SIGN UP!" />
<Label fx:id="lbmem" layoutX="285.0" layoutY="43.0" text="NOT A MEMBER? SIGN UP!" />
<Label layoutX="288.0" layoutY="78.0" text="NAME:" />
<Label layoutX="288.0" layoutY="111.0" text="LAST NAME:" />
<Label layoutX="288.0" layoutY="148.0" text="USERNAME:" />
<Label layoutX="287.0" layoutY="189.0" text="PASSWORD:" />
<Label layoutX="287.0" layoutY="231.0" text="PASSWORD AGAIN:" />
<TextField fx:id="name" layoutX="399.0" layoutY="66.0" />
<TextField fx:id="usersg" layoutX="399.0" layoutY="144.0" />
<TextField fx:id="lname" layoutX="399.0" layoutY="107.0" />
<PasswordField fx:id="paswdsg" layoutX="399.0" layoutY="185.0" />
<PasswordField fx:id="pswdsga" layoutX="399.0" layoutY="227.0" />
</children></AnchorPane>
</content>
</TitledPane>
和MainFX.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1">
<!-- TODO Add Nodes -->
</AnchorPane>
我做错了什么?谢谢你的帮助
答案 0 :(得分:2)
在您的FXML中,usersg
是一个AnchorPane。
在您的控制器中,usersg
被TextField
定义为:
@FXML
private TextField usersg;
只需将定义更改为AnchorPane即可:
@FXML
private AnchorPane usersg;