CTimet 2021-12-11 08:38 采纳率: 84%
浏览 173
已结题

JavaFX程序在设置图标时发生异常

img


这是在start方法中的一段代码。其中,logo.jpg这个文件是真实存在的。

img


而我对这个文件分别使用了绝对路径和相对路径进行测试。每一种路径在测试时都抛出了InvocationTargetException异常
这个异常翻译过来就是调用目标异常。本来我是使用图片的相对路径的,发生异常后我就改成了绝对路径,可是任然抛出了这个异常
这是IDEA在debug模式下运行代码时控制台的输出:

"C:\Program Files\Java\jdk-16.0.1\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:4462,suspend=y,server=n -javaagent:C:\Users\ACE\AppData\Local\JetBrains\IdeaIC2021.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Users\ACE\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.3\lib\idea_rt.jar" -p C:\Users\ACE\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16-win.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16-win.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16-win.jar;F:\JavafxStudyDemo02\target\classes;C:\Users\ACE\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16-win.jar -m com.example.javafxstudydemo02/com.example.javafxstudydemo02.HelloApplication
已连接到目标 VM, 地址: ''127.0.0.1:4462',传输: '套接字''
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Exception 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(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1125)
    at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:618)
    at com.example.javafxstudydemo02/com.example.javafxstudydemo02.HelloApplication.start(HelloApplication.java:49)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1117)
    ... 12 more
Exception running application com.example.javafxstudydemo02.HelloApplication
与目标 VM 断开连接, 地址为: ''127.0.0.1:4462',传输: '套接字''

进程已结束,退出代码为 1

这是HelloApplication文件源码:

package com.example.javafxstudydemo02;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class HelloApplication extends Application
{
    public static void main(String[] args)
    {
        Application.launch(args);
    }
    @Override
    public void start(Stage stage) throws Exception
    {
        Button button = new Button("百度");
        BorderPane pane = new BorderPane(button);
        Scene scene = new Scene(pane,500,500);

        button.setOnAction(e -> {
            getHostServices().showDocument("https://www.baidu.com/");
        });
        stage.setTitle("FirstJavafxApp");
        stage.setScene(scene);//将Scene场景添加到stage里
        stage.getIcons().add(new Image("src/main/resources/image/logo.jpg"));
        //getIcons方法获取应用图标,通过add方法添加一个Image对象。该对象相对路径在src/main/resources/image/logo.jpg
        stage.show();
    }
}

错误应该就是在设置应用图标的那一行,但是我不明白,为什么我使用绝对路径和相对路径都发生了错误。求解答,谢谢

  • 写回答

1条回答 默认 最新

  • Yuiee 2021-12-11 09:05
    关注

    resources文件夹下的文件在编译后,都是在根目录。因此相对路径改成 image/logo.jpg 即可。

    stage.getIcons().add(new Image("image/logo.jpg"));
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月9日
  • 创建了问题 12月11日

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置