JAR 文件外的图像?
Images outside JAR file?
我真的很困惑...所以我的文件结构是这样的
/PCBuilder
-PCBuilder.jar <= The main program
/PCBuilder/Images
/accessories/*.png files here
/gui/*.png files here
/parts/*.png files here
/prebuild/*.png files here
我的代码
private void okMouseEntered(java.awt.event.MouseEvent evt) {
ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PCBuilder/images/gui/Okay_glow.png")));
}
private void okMouseExited(java.awt.event.MouseEvent evt) {
ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PCBuilder/images/gui/Okay_red.png")));
}
当我在 Netbeans 上 运行 它时,它工作正常...但是当我尝试编译它和 运行 JAR 文件时,它显示此错误...
D:\Documents\NetBeansProjects\PCBuilder_GUIHACKED\dist>java -jar PCBuilder_GUIHACKED.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at PCBuilder.welcomescreen.okMouseEntered(welcomescreen.java:87)
at PCBuilder.welcomescreen.access0(welcomescreen.java:12)
at PCBuilder.welcomescreen.mouseEntered(welcomescreen.java:45)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
有什么解决办法吗?谢谢
你的镜像在可执行jar文件的父目录下,所以最好获取父路径,然后从目录中读取镜像:
作为解决方案:
File f = new File(this.getClass().getProtectionDomain().
getCodeSource().getLocation().toURI().getPath());
String path = f.getParent() + "/images/print.png";
lbl1.setIcon(new ImageIcon(path));
1.Getjar文件路径:
File f = new File(this.getClass().getProtectionDomain().
getCodeSource().getLocation().toURI().getPath());
2.Get图片的绝对路径(如{jar_file_parent_directory}/images/print.png):
String path = f.getParent() + "/images/print.png";
3.Set 组件图标(例如 JLabel lbl1)
lbl1.setIcon(new ImageIcon(path));
我真的很困惑...所以我的文件结构是这样的
/PCBuilder
-PCBuilder.jar <= The main program
/PCBuilder/Images
/accessories/*.png files here
/gui/*.png files here
/parts/*.png files here
/prebuild/*.png files here
我的代码
private void okMouseEntered(java.awt.event.MouseEvent evt) {
ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PCBuilder/images/gui/Okay_glow.png")));
}
private void okMouseExited(java.awt.event.MouseEvent evt) {
ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PCBuilder/images/gui/Okay_red.png")));
}
当我在 Netbeans 上 运行 它时,它工作正常...但是当我尝试编译它和 运行 JAR 文件时,它显示此错误...
D:\Documents\NetBeansProjects\PCBuilder_GUIHACKED\dist>java -jar PCBuilder_GUIHACKED.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at PCBuilder.welcomescreen.okMouseEntered(welcomescreen.java:87)
at PCBuilder.welcomescreen.access0(welcomescreen.java:12)
at PCBuilder.welcomescreen.mouseEntered(welcomescreen.java:45)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
有什么解决办法吗?谢谢
你的镜像在可执行jar文件的父目录下,所以最好获取父路径,然后从目录中读取镜像:
作为解决方案:
File f = new File(this.getClass().getProtectionDomain().
getCodeSource().getLocation().toURI().getPath());
String path = f.getParent() + "/images/print.png";
lbl1.setIcon(new ImageIcon(path));
1.Getjar文件路径:
File f = new File(this.getClass().getProtectionDomain().
getCodeSource().getLocation().toURI().getPath());
2.Get图片的绝对路径(如{jar_file_parent_directory}/images/print.png):
String path = f.getParent() + "/images/print.png";
3.Set 组件图标(例如 JLabel lbl1)
lbl1.setIcon(new ImageIcon(path));