运行 中的 JavaFx 错误
JavaFx erron in running
我对 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$launchApplication2(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/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.access0(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$launchApplication19(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/1393559157.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/200091476.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda/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
我在网上搜索了解决方案,大多数都说问题可以出在url。但是正如你在第一个代码中看到的那样,我把它写成 (../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) {
}
}
考虑这一行 /H:/Java%20projects/JavaFx/bin/sas.fxml:7
我认为文件夹名称是 "Java projects"。尝试将其重命名为 "JavaProjects" 或 "Java_projects"。 space 字符转换为 %20,这导致 java.lang.ClassNotFoundException
被抛出
fx:controller
属性需要 class 的 完全限定 名称。由于您将 MyController
放在一个名为 javafx
的包中(顺便说一句,您不应该:这是一个受保护的包名称,因此您应该选择其他特定于您的 company/organization等),你需要
fx:controller="javafx.MyController"
由于您只指定了 fx:controller="MyController"
,FXMLLoader
正在默认包(即 class 路径的根目录中)查找名为 class 的 class =12=]。由于它在那里找不到它(它在不同的包中),它给你一个 ClassNotFoundException
.
我对 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$launchApplication2(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/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.access0(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$launchApplication19(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/1393559157.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/200091476.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda/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
我在网上搜索了解决方案,大多数都说问题可以出在url。但是正如你在第一个代码中看到的那样,我把它写成 (../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) {
}
}
考虑这一行 /H:/Java%20projects/JavaFx/bin/sas.fxml:7
我认为文件夹名称是 "Java projects"。尝试将其重命名为 "JavaProjects" 或 "Java_projects"。 space 字符转换为 %20,这导致 java.lang.ClassNotFoundException
被抛出
fx:controller
属性需要 class 的 完全限定 名称。由于您将 MyController
放在一个名为 javafx
的包中(顺便说一句,您不应该:这是一个受保护的包名称,因此您应该选择其他特定于您的 company/organization等),你需要
fx:controller="javafx.MyController"
由于您只指定了 fx:controller="MyController"
,FXMLLoader
正在默认包(即 class 路径的根目录中)查找名为 class 的 class =12=]。由于它在那里找不到它(它在不同的包中),它给你一个 ClassNotFoundException
.