java - JavaFX - 应用程序中的异常
问题描述
我正在尝试创建一个 JavaFX 程序,每次尝试运行我的代码时,我都会遇到异常 - 我不完全确定这意味着什么......
我的代码:
package com.PickingList.ui;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) throws IOException {
Scene scene;
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("./TelaLogin.fxml"));
Parent root = loader.load();
stage.setScene(new Scene(root));
stage.setTitle("Login");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我正在使用 Eclipse 创建项目,但出现以下错误:
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(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(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(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.PickingList.ui.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
Exception running application com.PickingList.ui.Main
解决方案
尝试:
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/TelaLogin.fxml"));
假设您的文件夹结构如下所示:
/src/TelaLogin.fxml
推荐阅读
- r - R中的最后一次观察结果和最后一次观察结果向后进行
- android - Android 模拟器 - 升级后如何运行 Chrome 应用
- sql - Excel 中的 SQL:无法调用声明的变量
- sql-server - 有没有办法为 SQL Server 中的排序操作设置默认排序规则?或 SQL Server 2019 中的 oracle NLS_SORT 等效项
- visual-studio-code - Visual Studio Code 文件过滤器行为
- python - 使用 FiPy 在 1D 柱坐标上求解 PDE
- javascript - jQuery Validator Form Plugin 在验证之后但在提交之前显示消息
- r - 如何使用ggplot2将子组点图(3)集中在分组箱线图(2)上?
- jsf - 如何使用命令按钮jsf primefaces在ui重复中获取h:outputtext的值?
- xml - 通过 Snowflake UI 查询具有嵌套标签的 XML