如何修复javafx.fxml.LoadException

时间:2015-07-28 18:03:19

标签: java javafx fxml

我正在查看LoginDemo的javafx示例代码。我使用代码进行乘法场景应用;我得到一个javafx.fxml.LoadException。我不知道该怎么做。

我的代码是打开一个窗口并有一个按钮,然后单击它以在javafx TextArea中显示文本。

主:

package Test;

import java.io.InputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

private Stage stage;

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

@Override
public void start(Stage primaryStage) throws Exception {
    try{
        stage = primaryStage;
        stage.setTitle("Test");
        setScene();
        stage.show();
    }catch(Exception e){
        e.printStackTrace();
    }

}

private void setScene(){
    try{
        Cont controller = (Cont) changeScene("Window.fxml");
        controller.setApp(this);
    }catch(Exception e){
        e.printStackTrace();
    }
}

private Initializable changeScene(String fxml) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    InputStream in = Main.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(Main.class.getResource(fxml));
    AnchorPane page;
    try {
        page = (AnchorPane) loader.load(in);
    } finally {
        in.close();
    } 
    Scene scene = new Scene(page, 800, 600);
    stage.setScene(scene);
    stage.sizeToScene();
    return (Initializable) loader.getController();
   }

}

续:

package Test;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;


public class Cont implements Initializable {

private Main application;

@FXML
private Button btn;
@FXML
public TextArea Console;

public void setApp(Main application){
    this.application = application;
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    assert btn != null : "fx:id=\"btn\" was not injected: check your FXML file 'Window.fxml'.";
    assert Console != null : "fx:id=\"Console\" was not injected: check your FXML file 'Window.fxml'.";

    btn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent arg0) {
            setText("hello world");
        }
    });

}

public void setText(String text){
    Console.appendText(text+"\n");
 }

}

Window.fxml:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Cont">
   <children>
      <TextArea fx:id="Console" prefHeight="365.0" prefWidth="600.0" />
      <Button fx:id="btn" layoutX="274.0" layoutY="374.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

错误:

javafx.fxml.LoadException: 
/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at Test.Main.changeScene(Main.java:49)
at Test.Main.setScene(Main.java:35)
at Test.Main.start(Main.java:25)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/70604542.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/455370116.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(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$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Cont
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)
... 24 more

1 个答案:

答案 0 :(得分:3)

fx:controller属性需要控制器类的完全限定类名。由于您的控制器Cont位于名为Test的包中,因此您需要

fx:controller="Test.Cont"

堆栈跟踪确实为您提供了所需的所有信息:错误源自的文件和行号:

  

/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

及其根本原因:

  

引起:java.lang.ClassNotFoundException:Cont

即。它无法找到名为Cont的类(因为它的名称为Test.Cont)。

(顺便说一句,请注意包名称应全部为小写,因此您应该真正调用包test,而不是Test。)