加载fxml文件时出现LoadException



嗨,我正在使用javaFx,而且我是新手。我需要帮助。在运行主应用程序时,我收到javafx.fxml.LoadException的错误:

请帮忙。

代码:主代码

package mainApp;

import controller.LoginController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            //Create a loader for the UI components
            FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(Main.class.getResource("/view/loginFx.fxml"));
            //Inflate the view using the loader
            AnchorPane root = (AnchorPane) loader.load();
            //Set window title
            primaryStage.setTitle("Welcome");
            //Create a scene with the inflated view
            Scene scene = new Scene(root);
            //Set the scene to the stage
            primaryStage.setScene(scene);
            //Get the controller instance from the loader
            LoginController controller = loader.getController();
            //Set the parent stage in the controller
            controller.setDialogStage(primaryStage);
            //Show the view
            primaryStage.show();
        } catch(Exception e) {
            //System.out.println("Error occured while inflating view: " + e);
            e.printStackTrace();
        }
    }

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

fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.controller.LoginController">
    <children>
        <Label layoutX="113.0" layoutY="102.0" text="UserName" />
        <Label layoutX="116.0" layoutY="173.0" text="Password" />
        <TextField fx:id="UserName" layoutX="200.0" layoutY="98.0" />
        <Button layoutX="242.0" layoutY="251.0" mnemonicParsing="false" onAction="#loginbutton" text="Login" />
        <Text fx:id="username" layoutX="200.0" layoutY="400.0" />
      <PasswordField layoutX="200.0" layoutY="169.0" />
      <Label layoutX="54.0" layoutY="45.0" prefHeight="17.0" prefWidth="130.0" text="Welcome" />
   </children>
</AnchorPane>

控制器文件:

package controller;

//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
//import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import model.Login;
import dao.UserDao;
public class LoginController {
    //This is the parent stage
        private Stage dialogStage;
        //This is the Text box element in the view for name of bank
        @FXML
        private TextField username;
        //Method to set the parent stage of the current view
        public void setDialogStage(Stage dialogStage) {
            this.dialogStage = dialogStage;
        }
        public void loginbutton() {
            //Extract the data from the view elements
            String username = this.username.getText();
        //  String address = this.address.getText();
            //Validate the data
            if(username == null || username.trim().equals("")) {
                return;
            }
        Login login = new Login();
        login.setName(username);
        UserDao u = new UserDao();
        u.create(login);
        close();
        }
        private void close() {
            dialogStage.fireEvent(new WindowEvent(dialogStage,WindowEvent.WINDOW_CLOSE_REQUEST));
        }
    }

请告诉我出了什么问题我得到以下错误:javafx.fxml.LoadException:/C:/Users/Neel Megha/workspace/User/bin/view/loginFx.fxml:11

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
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 mainApp.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at 
com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: login.controller.LoginController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 17 more

堆栈跟踪告诉您问题:

引起原因:java.lang.ClassNotFoundException:login.controller.LoginController

您在FXML文件中将控制器声明为fx:controller="login.controller.LoginController",因此FXMLLoader正在login.controller包中查找LoginController类。但是,您的LoginController类声明它在controller包中:

package controller ;
// ...
public class LoginController { ... }

因此,您应该将FXML更改为fx:controller="controller.LoginController"

(您的代码中可能还有其他错误:这就是导致您发布错误的原因。)


顺便说一句,您不需要将Stage实例连接到控制器的所有代码。只做

private void close() {
    username.getScene().getWindow().hide();
}

现在,您可以从LoginController中去掉dialogStagesetDialogStage()方法,并且可以删除行

LoginController controller = loader.getController();
controller.setDialogStage(primaryStage);

来自CCD_ 12。

(当然,有时你需要访问控制器,但从UI中的任何节点都可以访问的阶段的所有不必要的布线都让我抓狂。)

相关内容

  • 没有找到相关文章

最新更新