" javafx.fxml.LoadException"加载fxml时

时间:2017-12-06 12:52:27

标签: java javafx fxml scenebuilder fxmlloader

showAdminLoginScene()方法产生错误, 但是,如果我修改了这个方法并且我将同一个包中的另一个fxml文件加载,它加载就好了:这里是我的主要:

package application;

import java.io.IOException;

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

public class Main extends Application {
  private Stage primaryStage;
  private static BorderPane mainLayout;
  @Override
public void start(Stage primaryStage) throws IOException {
    this.primaryStage= primaryStage;
    this.primaryStage.setTitle("LAU LABS");
    showMainView();
}
private void showMainView() throws IOException{
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("MainView.fxml"));

    mainLayout=loader.load();


    Scene scene=new Scene(mainLayout);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void showAdminLoginScene() {
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("AdminLogin.fxml"));
    BorderPane adminLogin;
    try {
        adminLogin = loader.load();
        mainLayout.setCenter(adminLogin);
        } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void showAdminLoggedIn() {
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(Main.class.getResource("AdminLoggedIn.fxml"));
    AnchorPane adminLoggedIn;
    try {
        adminLoggedIn = loader.load();
        mainLayout.setCenter(adminLoggedIn);
    } catch (IOException e) {
        System.out.println("the error is in Main, ShowAdminLoggedIn");
    }

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

这是我的控制器类:

package application;

import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javafx.fxml.FXML;


public class AdminLoginController {
   @FXML private TextField txtUsername;
   @FXML private TextField txtPassword;
   @FXML private Label lblStatus;
   private Main main;


public void Login(ActionEvent event) {
   if(txtUsername.getText().equals("user")&& 
         txtPassword.getText().equals("pass")){
    main.showAdminLoggedIn();
}
else{
    lblStatus.setText("Login Failed!");
}
}
}

这是我的fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Text?>


<BorderPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AdminLoginController">
   <center>
      <AnchorPane prefHeight="300.0" prefWidth="300.0">
         <children>
            <TextField fx:id="txtUsername" layoutX="56.0" layoutY="79.0" prefHeight="26.0" prefWidth="189.0" />
            <PasswordField fx:id="txtPassword" layoutX="56.0" layoutY="150.0" prefHeight="26.0" prefWidth="189.0" />
            <Button layoutX="56.0" layoutY="227.0" mnemonicParsing="false" onAction="#Login" prefHeight="32.0" prefWidth="189.0" style="-fx-background-color: #004c3c;" text="Login" />
            <Text fill="#004c3c" layoutX="56.0" layoutY="75.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Username" wrappingWidth="60.98307228088379" />
            <Text fill="#004c3c" layoutX="56.0" layoutY="146.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password" />
            <Button layoutX="1.0" minWidth="52.0" mnemonicParsing="false" prefHeight="39.0" prefWidth="52.0" style="-fx-background-color: Transparent;" />
            <ImageView fitHeight="35.0" fitWidth="200.0" layoutX="10.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@BackButton.png" />
               </image>
            </ImageView>
            <Label fx:id="lblStatus" layoutX="56.0" layoutY="202.0" prefHeight="16.0" prefWidth="189.0" style="-fx-text-fill: Red;" text="Status: " />
         </children>
      </AnchorPane>
   </center>
</BorderPane>

他们都在同一个名为&#34; application&#34; (fxml,controller和main)

那是错误:

javafx.fxml.LoadException: 
/C:/Users/Owner/Documents/UNI/programming/workspace/Lau/bin/application/AdminLogin.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
    at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
    at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.Main.showAdminLoginScene(Main.java:39)
    at application.MainViewController.goAdminLogin(MainViewController.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.TextField
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    ... 65 more

2 个答案:

答案 0 :(得分:2)

你在FXML中使用了不正确的TextField(我想这不是故意的):

<?import com.gluonhq.charm.glisten.control.TextField?>

导致:

Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.TextField

您可以将导入更改为...

<?import javafx.scene.control.TextField?>

使用内置TextField

答案 1 :(得分:1)

您缺少Gluon's Charm library,您应该将其添加到构建自动化工具中:

对于maven项目:

<dependencies>
...
<dependency>
    <groupId>com.gluonhq</groupId>
    <artifactId>charm-glisten</artifactId>
    <version>4.4.1</version> // Change version as needed 
</dependency>
...
</dependencies>
<repositories>
       <repository>
         <id>nexus-com-gluonhq</id>
         <name>Nexus repo</name>
         <url>http://nexus.gluonhq.com/nexus/content/repositories/releases</url>
       </repository>
</repositories>

for Gradle:

..
repositories {
    ....
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
   ...
}

dependencies {
    compile 'com.gluonhq:charm-glisten:4.4.1'
    ...
}

顺便说一句,在您使用JavaFX项目时,在您的控制器类中使用javafx.scene.control而不是java.awt