0

嗨,我正在使用 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);
    }
}

.xml 文件

<?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
4

1 回答 1

0

堆栈跟踪告诉您问题:

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

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

package controller ;

// ...

public class LoginController { ... }

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

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


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

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

现在你可以摆脱dialogStagesetDialogStage()方法LoginController,你可以删除这些行

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

Main.

(当然有时您需要访问控制器,但所有这些不必要的舞台接线,可从 UI 中的任何节点访问,让我抓狂。)

于 2015-12-01T02:10:40.490 回答