在使用JavaFX启动SceneBuilder时报错,显示fxml文件路径为空,以下是报错
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.RuntimeException: Could not find FXML file:D:\文档\IDEA\JavaFx\src\JavaFxStudent\JavaFxml\JavaFX.fxml
at JavaFxStudent.Main.changeView(Main.java:39)
at JavaFxStudent.Main.start(Main.java:31)
at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@21.0.3/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
... 1 more
Exception running application JavaFxStudent.Main
这个是项目格式图:
电脑环境变量图
这个是Login登陆界面类的源代码:
package JavaFxStudent.JavaFXImpl;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import java.awt.event.ActionEvent;
/**
* @program: JavaFx
* @ClassName Logins
* @description(类信息): 登陆界面
* @author: honor
* @create: 2024-05-30 11:32
* @Version 1.0
**/
public class Logins {
@FXML
private Button button;
@FXML
public void handButtonClick(javafx.event.ActionEvent actionEvent) {
System.out.println("Button clicked!");
}
}
这个是fxml文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="JavaFxStudent.JavaFXImpl.Logins">
<children>
<Label layoutX="144.0" layoutY="116.0" prefHeight="101.0" prefWidth="271.0" text="Fuck"/>
<Button layoutX="187.0" layoutY="209.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="196.0"
text="Buttons" onAction="#handButtonClick"/>
</children>
</AnchorPane>
这个是测试类源代码:
package JavaFxStudent;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
/**
* @program: JavaFx
* @ClassName Main
* @description(类信息): 测试类
* @author: honor
* @create: 2024-05-30 11:30
* @Version 1.0
**/
public class Main extends Application {
private static Stage stage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Main.stage = stage;
stage.setTitle("Fuck");
changeView("D:\\文档\\IDEA\\JavaFx\\src\\JavaFxStudent\\JavaFxml\\JavaFX.fxml");
stage.show();
}
public static void changeView(String fxml) {
Parent root = null;
try {
URL reso = Main.class.getClassLoader().getResource(fxml);
if (reso == null) throw new RuntimeException("Could not find FXML file:" + fxml);
root = FXMLLoader.load(reso);
stage.setScene(new Scene(root));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
找了一圈修不好,同样的代码文件在他人电脑上能运行在我这就不行了