我尝试在Eclipse中使用SceneBuilder来创建第一个简单的图形界面示例,但是我遇到了无法解决的错误。
我通过Eclipse函数创建了控制器类。
*xception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
at ProvaGrafica2.start(ProvaGrafica2.java:29)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Exception running application ProvaGrafica2*
import java.io.File;
import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ProvaGrafica2 extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start (Stage stage) throws Exception
{
System.out.println("prova");
File file = new File("C:\\Users\\u5ex\\Desktop\\eclipse\\Grafica2\\src\\a.fxml");
if (file.exists())
System.out.println("il file esiste");
Parent root = FXMLLoader.load(getClass().getResource("C:\\Users\\u5ex\\Desktop\\eclipse\\Grafica2\\src\\a.fxml"));
Scene scene=new Scene (root);
stage.setTitle("Login");
stage.setScene(scene);
stage.show();
}
答案 0 :(得分:1)
对于您而言,将getResource调用缩短为getClass().getResource("a.fxml")
就足够了。至少我希望如此,因为如果没有实际尝试,我不会打赌:-)
答案 1 :(得分:0)
@mipa的答案是正确的,您应该使用getClass().getResource("a.fxml")
,但是对此有一些更深的了解:getClass().getResource(...)
应该用于src
文件夹中的文件,哪些文件将最终保存在您的最终JAR中。但是,传递到getResource()
的路径不是相对于src
文件夹而是相对于您的类文件。因此,如果您的.java文件为:
src/com/package/YourClass.java
然后getClass().getResouce("a.fxml")
指的是:
src/com/package/a.fxml
如果要相对于src
目录,请以斜杠(即getClass().getResouce("/a.fxml")
)开头。
此外,您还应注意,要加载磁盘上任何位置的文件(例如,用户选择的文件),请改用此文件:
File file = new File("your/path/here/file.fxml");
FXMLLoader.load(file.toURI().toURL());