简体   繁体   English

访问.JAR文件中的图像

[英]Accessing images in a .JAR file

I have a set of 5 images in my program. 我的程序中有5张图像。 The program will be compiled as a single .JAR file, so they need to be read from it as such. 该程序将被编译为单个.JAR文件,因此需要从中读取它们。 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")));

which works for the icons in JMenuItem s (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. 适用于JMenuItem的图标(例如jMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bha/resources/icon.png")));将始终有效)以及在测试时在IDE中,但在编译为JAR文件时返回null

Given that getResource returns a URL, i'm guessing that it's not liking the spaces. 给定getResource返回一个URL,我猜想它不喜欢空格。 As a test, i'd try doing 作为测试,我会尝试做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM