/**
     * Finds a resource with a given name.  This method returns null if no
     * resource with this name is found.  The rules for searching resources
     * associated with a given class are implemented by the * defining class
     * loader of the class.
     *
     * <p> This method delegates the call to its class loader, after making
     * these changes to the resource name: if the resource name starts with
     * "/", it is unchanged; otherwise, the package name is prepended to the
     * resource name after converting "." to "/".  If this object was loaded by
     * the bootstrap loader, the call is delegated to
     * <code>ClassLoader.getSystemResource</code>.
     *
     * @param name  name of the desired resource
     * @return      a <code>java.net.URL</code> object.
     * @see         java.lang.ClassLoader
     * @since JDK1.1
     */
    public java.net.URL getResource(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResource(name);
        }
        return cl.getResource(name);
    }

解决方案 »

  1.   

    这有什么好解释的。this指的是你的当前类,getClass转成Class类型,为了运用Class类的getResource方法。
      

  2.   

    getResource方法返回URL类型,一些类的构造方法需要URL类型的参数。例如ImageIcon
      

  3.   

    那么参数imagePath应该如何确定?是写绝对路径,还是相对的?
    如果写相对的,那么相对哪个路径?资源文件应该放到什么路径?
    经过我的试验,发现,资源要放到与类相同的路径中,相对路径
    是相对于类所在的目录。所以我认为这个定位方法不是很好。
    还是使用System.getProperty("user.dir")定为要好些。