【问题标题】:javaFX LoadExceptionjavaFX 加载异常
【发布时间】:2014-11-25 15:44:44
【问题描述】:

对于一个项目,我需要使用 javaFX 创建一个 GUI,因此通过我非常密切关注的 scenebuiler/javaFX 教程,我创建了一个非常基本的 GUI。但是负载似乎不起作用并引发 LoadException。我在网上似乎有一些类似的问题,但他们的答案似乎都没有解决我的问题。我已经尽可能多地尝试了类似的指南和教程,但它就是无法加载。

我试过用 Parent 代替 AnchorPane,用 getClass 代替 Main.class 以及加载器的更多变体,我在网上找到了。

package application;

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 {
            System.out.println("pre load");
            AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("/application/testView.fxml"));
            System.out.println("post load");
            Scene scene = new Scene(root);
            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);
    }
}

还有例外

javafx.fxml.LoadException: 
/D:/Dropbox/Technische%20informatica/OOP/EclipseWorkspace/javaFXtest/bin/application/testView.fxml:8

    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.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 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/14158153.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/30452001.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/25437038.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/27229737.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$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/15096457.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: MyTestController
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    ... 28 more

【问题讨论】:

  • 原因很清楚:ClassNotFoundException: MyTestController。这个类存在吗?包装是否正确?

标签: java javafx scenebuilder


【解决方案1】:

所以它抱怨缺少控制器是 MyTestController 真的不在一个包中,例如 application.MyTestController?

【讨论】:

  • 教程告诉我我需要创建一个新的包“控制器”,MyTestController 在。但我很确定我也尝试在“应用程序”中创建控制器。我只是试着再做一次。编辑:与 main/fxml 相同的包中的控制器仍然给出 LoadException 并由以下原因引起:java.lang.ClassNotFoundException: MyTestController
  • 有效!没有一个教程用包指定控制器,所以给 fxml 控制器 application.MyTestController 使它工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2019-02-22
相关资源
最近更新 更多