将图像添加到JAR Java [英] Add image to JAR Java

查看:108
本文介绍了将图像添加到JAR Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JFrame 中使用了一些图像,我已经给出了将图像加载到框架面板的路径,但是当我制作应用程序的jar时它没有显示图片。

我应该在哪里放置图片?

我应该如何指定路径?

I'm using some images in JFrame, I have given a path to load image into panel of the frame, but when i made jar of the application its not displaying images.
Where should I place the image?
How should I specify the path?

setIconImage(Toolkit.getDefaultToolkit().getImage(
                "D:\\RFT\\src\\dailogBox\\dump.jpg"));

就像我这样做。

推荐答案

第一步:将图像放在JAR文件中的已定义位置。如果你把它放到src文件夹中,也许你的IDE或你的构建工具会自动将它打包到JAR。否则,请查看IDE / build-tool的文档,在哪个位置放置它。

First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.

第二步:从程序中访问该文件。我假设您将它放在包(JAR中的路径)package1 / package2中,该文件名为dump.jpg。在这种情况下,您调用:

Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/package1/package2/dump.jpg")));

getClass()。getResource(...)返回类路径上资源的URL。使用'/'在类路径中启动路径,并使用资源的完整路径/包,用'/'分隔。

getClass().getResource(...) returns an URL for a resource on your classpath. You start the path in the classpath with a '/' and use the complete path/packages to your resource, separated by '/'.

这篇关于将图像添加到JAR Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆