我遇到了javafx的问题。我在SceneBuilder中创建了我的fxml文件,并将其与包文件夹放在同一目录中。这是代码:
public class Main extends Application {
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage window) throws Exception {
Pane mainPane = (Pane)FXMLLoader.load(Main.class.getResource("../sas.fxml"));
Scene scene = new Scene(mainPane);
window.setScene(scene);
window.show();
}
}
当我运行它时,它给了我这个错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1323468230.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/H:/Java%20projects/JavaFx/bin/sas.fxml:7
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 javafx.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1393559157.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/200091476.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)
... 1 more
Caused by: java.lang.ClassNotFoundException: MyController
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
Exception running application javafx.Main
我在线搜索解决方案,他们中的大多数人说问题可能与网址有关。但正如你在第一个代码中看到的那样,我把它写成(../sas.fxml),因为它与java文件不在同一个文件夹中,它与包文件夹位于同一个文件夹中。所以任何想法如何解决?感谢任何解决方案
FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="185.0" prefWidth="349.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
<children>
<TextField fx:id="username" layoutX="44.0" layoutY="35.0" promptText="username" />
<Button fx:id="login" layoutX="224.0" layoutY="35.0" mnemonicParsing="false" onAction="#loginFucntion" text="Login" />
</children>
</Pane>
MyController.java
package javafx;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class MyController {
@FXML
private Button login;
@FXML
private TextField username;
@FXML
void loginFucntion(ActionEvent event) {
}
}
答案 0 :(得分:2)
fx:controller
属性需要该类的完全限定名称。由于您将MyController
放在名为javafx
的程序包中(顺便说一下,您不应该:这是受保护的程序包名称,因此您应该选择特定于您公司/组织的其他名称等),你需要
fx:controller="javafx.MyController"
由于您只指定了fx:controller="MyController"
,FXMLLoader
正在查找名为MyController
的类的默认包(即类路径的根目录)。由于它找不到它(它在不同的包中),它给你一个ClassNotFoundException
。
答案 1 :(得分:0)
考虑这一行/H:/Java%20projects/JavaFx/bin/sas.fxml:7
我认为文件夹名称是“Java projects”。尝试将其重命名为“JavaProjects”或“Java_projects”。空格字符将转换为%20,这会导致java.lang.ClassNotFoundException
被抛出