我在哪里放置图像以用于创建新的Swing ImageIcon对象? [英] Where have I to put an image to use it to create a new Swing ImageIcon object?

查看:84
本文介绍了我在哪里放置图像以用于创建新的Swing ImageIcon对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Java Swing库,但遇到问题。

I am studying Java Swing library and I have a problem.

在示例程序中,它通过以下行创建 ImageIcon 对象:

In an example program it create an ImageIcon object by this line:

ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));

在我执行程序时,由于在项目中 exit.png 丢失:

Whe I execute my program I obtain the following error caused by the fact that in the project the exit.png is missing:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at com.andrea.second.SimpleMenu.initUI(SimpleMenu.java:23)
    at com.andrea.second.SimpleMenu.<init>(SimpleMenu.java:17)
    at com.andrea.second.SimpleMenu$2.run(SimpleMenu.java:53)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

问题是,使用Eclipse,我必须把这个图像放在哪里?

The problem is, using Eclipse, where I have to put this immage?

我试图将 exit.png文件复制并粘贴(在我的文件系统中,而不是在Eclipse中)到包含创建该类的类的同一包文件夹中……但是它不起作用

I tryed to copy and paste it (in my file system, not in Eclipse) an "exit.png" file into the same package folder that contains the class that create it...but it don't work

我该怎么做才能在Eclipse中解决此问题?

What have I to do to solve this problem in Eclipse?

推荐答案

使用

ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));

您需要放入 exit.png getClass()表示的类相同。

You need to put the exit.png in the same package as the Class represented by getClass().

With

ImageIcon icon = new ImageIcon(getClass().getResource("/exit.png")); // note leading /

您需要将其放在类路径的根目录。在Eclipse中,可以通过将其直接放在 src 中来实现。

You need to put it at the root of your classpath. In Eclipse, that would happen by putting it directly in src.

Class#getResource(String) javadoc状态

The Class#getResource(String) javadoc states


在委派之前,绝对资源名称是使用
给定资源名称使用此算法:

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

如果名称以'/'开头('\u002f'),则
的绝对名称是资源的一部分

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

否则,绝对名称的格式如下:

Otherwise, the absolute name is of the following form:

modified_pa​​ckage_name / name其中,modified_pa​​ckage_name是此对象的
软件包名称,其中用'/'代替'。'('\u002e')。

modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

这篇关于我在哪里放置图像以用于创建新的Swing ImageIcon对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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