我知道有几个类似的问题,但大部分的答案看起来非常具体。无论如何,我的问题(如标题所示)是我的.jar文件不会打开.png文件或.wav文件(我使用的只有两种文件类型)。但是,Netbeans会运行得很好。我使用
将png导入为ImageIconsImageIcon sun = new ImageIcon("Files\\Digital_Sun.png");
“Files”在我的项目文件夹中。我试图将这些文件加载到我的.jar中,当我用7zip打开它时,我可以看到它们在那里,但是我似乎无法找到.jar中文件的正确URL。我尝试过使用.getResourceAsStream(),正如其他人所建议的那样,这对我也不起作用。也许我使用它错了,但其他答案和文档在那里没有多大帮助。任何帮助将不胜感激,谢谢!
编辑:这是我尝试使用InputStreams,它根本没用。有人可以告诉我从zip / jar文件中读取的正确格式吗?
InputStream sunIS = new BufferedInputStream( new FileInputStream("Files\\Digital_Sun.png"));
InputStream overIS = new BufferedInputStream( new FileInputStream("Files\\overcast_digital.png"));
InputStream rainIS = new BufferedInputStream( new FileInputStream("Files\\rain_digital.png"));
InputStream snowIS = new BufferedInputStream( new FileInputStream("Files\\snow_digital.png"));
InputStream fairIS = new BufferedInputStream( new FileInputStream("Files\\digital_fair.png"));
InputStream clearIS = new BufferedInputStream( new FileInputStream("Files\\clear_digital.png"));
InputStream windyIS = new BufferedInputStream( new FileInputStream("Files\\windy_digital.png"));
InputStream nullIS = new BufferedInputStream( new FileInputStream("Files\\weather_unknown.png"));
ImageIcon sun;
ImageIcon overcast;
ImageIcon rain;
ImageIcon snow;
ImageIcon fair;
ImageIcon clear;
ImageIcon windy;
ImageIcon nullWeather;
public DigitalClock() throws IOException, LineUnavailableException, URISyntaxException { //clock constructor
super();
sun = new ImageIcon(ImageIO.read(sunIS));
overcast = new ImageIcon(ImageIO.read(overIS));
rain = new ImageIcon(ImageIO.read(rainIS));
snow = new ImageIcon(ImageIO.read(snowIS));
fair = new ImageIcon(ImageIO.read(fairIS));
clear = new ImageIcon(ImageIO.read(clearIS));
windy = new ImageIcon(ImageIO.read(windyIS));
nullWeather = new ImageIcon(ImageIO.read(nullIS));
答案 0 :(得分:0)
我解决了它,以下网站给出了使用.getResourceAsStream()
的一个很好的解释。http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html