在JavaFX运行中无法启动
以下是报错
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.NullPointerException: Location is required.
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3369)
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3332)
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3300)
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3272)
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3248)
at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3241)
at JavaFxStudent.Main.changeView(Main.java:36)
at JavaFxStudent.Main.start(Main.java:30)
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
这个是文件图
这个是FXMl初始配置:fx:controller="JavaFxStudent.JavaFXImpl.Logins"
这个是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" />
</children>
</AnchorPane>
这个是控制类的源代码
package JavaFxStudent.JavaFXImpl;
import javafx.fxml.FXML;
/**
* @program: JavaFx
* @ClassName Logins
* @description(类信息): 登陆界面
* @author: honor
* @create: 2024-05-30 11:32
* @Version 1.0
**/
public class Logins {
}
这个是测试类源代码
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;
/**
* @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("src/JavaFxStudent/JavaFxml/JavaFX.fxml");
stage.show();
}
public static void changeView(String fxml){
Parent root = null;
try {
root = FXMLLoader.load(Main.class.getResource(fxml));
stage.setScene(new Scene(root));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
这个是虚拟机选项配置
--module-path "D:\JavaFX21\javafx-sdk-21.0.3\lib" --add-modules javafx.controls,javafx.fxml
最开始使用17JDK无效,重新下了21JDK运行,重写了代码依旧不行,但是不使用fxml直接运行FX没有问题,怀疑是Fxml问题
二编:
在使用前面赤兔回答进行修缮后依旧报错,但是说的是Cound not find Fxml 即FXNML文件为空
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
这是修改过后的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>
这是修改过后的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!");
}
}
这是测试类源代码
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.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);
}
}
}