java - 加载 FXML 文件期间未找到文件异常

标签 java javafx fxml fxmlloader

FXMLe1.java:

package fxmle1; 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class FXMLe1 extends Application {

@Override
public void start(Stage primaryStage) throws Exception{


   Parent panel = FXMLLoader.load(getClass().getResource("FXMLe1.fxml"));        
   Scene scene = new Scene(panel, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

}

FXMLe1.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>

<GridPane fx:id="root" prefHeight="400.0" prefWidth="600.0"  xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmle1.FXMLe1Controller">
    <BorderPane>
        <top>
            <Label text = "Test Javafx"/>    
        </top>
        <center>
            <Button text = "test"/>
        </center> 
    </BorderPane>    
    <stylesheets>
       
    </stylesheets>
</GridPane>

FXMLe1Controller.java:

package fxmle1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;


public class FXMLe1Controller implements Initializable {

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

大家好! 所以我尝试使用 FXML 制作一个 JavaFX 项目,但不幸的是,我遇到了这个问题:

Exception in Application start method
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: C:\Users\Амин\Documents\NetBeansProjects\JavaFX\Hehe There Is A Method To My Madness (Не удается найти указанный файл)
	at java.util.zip.ZipFile.open(Native Method)
	at java.util.zip.ZipFile.<init>(ZipFile.java:225)
	at java.util.zip.ZipFile.<init>(ZipFile.java:155)
	at java.util.jar.JarFile.<init>(JarFile.java:166)
	at java.util.jar.JarFile.<init>(JarFile.java:103)
	at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
	at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
	at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:94)
	at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
	at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:150)
	at java.net.URL.openStream(URL.java:1045)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
	at fxmle1.FXMLe1.start(FXMLe1.java:29)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	... 1 more
Exception running application fxmle1.FXMLe1
Java Result: 1

我尝试在互联网上寻找解决方案,但这对我没有多大帮助。 我还寻找了使我的项目崩溃的代码,那就是:

 Parent panel = FXMLLoader.load(getClass().getResource("FXMLe1.fxml"));

那么,有人对此有答案吗?)如果有的话,我会很高兴你告诉我。

最佳答案

将您的FXMLe1.fxml放入resources文件夹中,然后尝试

Parent panel = FXMLLoader.load(getClass().getResource("/FXMLe1.fxml"));

这应该可以作为您的解决方案

关于java - 加载 FXML 文件期间未找到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334361/

相关文章:

java - layout_weight 在表格行中带有按钮和 TextView

java - 这个小程序游戏循环有什么问题?

java - 从另一个类混淆调用方法(机器人类)

java - AES javafx 程序在按下加密按钮时显示错误

java - JavaFX 中的 SetAlignment 方法

java - 图:只访问那些最大化效用函数的节点

java - 无法构建 JavaFX 2.1.0 示例应用程序 - DataApp - 无法创建任务或键入 javafx :com. sun.javafx.tools.ant:application

java - 如何从一个 Java FXML Controller 中从另一个 FXML Controller 获取数据(这些 Controller 不是嵌套 Controller )?

java - 在 JavaFx 中重新启动应用程序

Javafx设置背景