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

查看:47
本文介绍了将图像添加到 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-folder 中,也许你的 IDE 或你的构建工具会自动将它打包到 JAR 中.否则,请检查您的 IDE/构建工具的文档,您必须将其放置在哪个位置.

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天全站免登陆