访问.JAR文件中的图像 [英] Accessing images in a .JAR file

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

问题描述

我的程序中有一组5张图像。该程序将编译为单个 .JAR 文件,因此需要从中读取它们。但是,当我拨打电话时

I have a set of 5 images in my program. The program will be compiled as a single .JAR file, so they need to be read from it as such. However, when I make a call like

images = new ArrayPP<ImageIcon>(
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 1.png")),
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 2.png")),
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 3.png")),
          new ImageIcon(getClass().getResource("/bha/resources/revit logo - glass.png")),
          new ImageIcon(getClass().getResource("/bha/resources/The All-Knowing Octopus.png")));

适用于 JMenuItem s中的图标(例如, jMenuItem.setIcon(new javax.swing.ImageIcon(getClass()。getResource(/ bha / resources / icon.png))); 将始终在IDE中进行测试时,在编译为 JAR 文件时返回 null

which works for the icons in JMenuItems (for example, jMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bha/resources/icon.png"))); will always work) and when testing in an IDE, but returns as null when compiled as a JAR file.

推荐答案

鉴于getResource返回一个URL,我猜它不喜欢这些空格。作为测试,我尝试做

Given that getResource returns a URL, i'm guessing that it's not liking the spaces. As a test, i'd try doing

new ImageIcon(getClass().getResource(URLEncoder.encode("/bha/resources/Portal Test Chamber 17 - Part 1.png", "UTF-8"))),

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

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