Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getClass().getResource()异常?_Java_Swing_Components_Imageicon_Getresource - Fatal编程技术网

Java getClass().getResource()异常?

Java getClass().getResource()异常?,java,swing,components,imageicon,getresource,Java,Swing,Components,Imageicon,Getresource,我正在尝试学习如何将图像添加到我的JFrame中。我在GUI方面做得很好,但我无法理解为什么这不起作用 我已经设置了阵列,以便我可以制作多个图像,以防您感到疑惑 1我的问题是getClass.getResource0.png;出于某种原因,这种做法一直在失败。当主电源。。。去创建对象GUIv1,在图像[0]..0.png中失败 不知道为什么,我使用的是eclipse,图像就在我的类所在的默认包中。有需要吗 2这里似乎也有问题,但这不是第一个例外的原因,我也希望得到这个问题的答案 我为代码字体的错

我正在尝试学习如何将图像添加到我的JFrame中。我在GUI方面做得很好,但我无法理解为什么这不起作用

我已经设置了阵列,以便我可以制作多个图像,以防您感到疑惑

1我的问题是getClass.getResource0.png;出于某种原因,这种做法一直在失败。当主电源。。。去创建对象GUIv1,在图像[0]..0.png中失败

不知道为什么,我使用的是eclipse,图像就在我的类所在的默认包中。有需要吗

2这里似乎也有问题,但这不是第一个例外的原因,我也希望得到这个问题的答案

我为代码字体的错误道歉,这是我第一次来这里

import java.awt.*;
import javax.swing.*;

public class GUIv1 extends JFrame{

private static int tilesnum = 2;
private static ImageIcon[] image = new ImageIcon[tilesnum + 2];
private static JLabel[] imagepanel = new JLabel[tilesnum + 2];

public GUIv1() {
    setLayout(new FlowLayout());

    image[0] = new ImageIcon(getClass().getResource("0.png"));     //HERE (1)
    image[1] = new ImageIcon(getClass().getResource("1.png"));
    image[2] = new ImageIcon(getClass().getResource("2.png"));
    image[3] = new ImageIcon(getClass().getResource("3.png"));

    for(int i = 0; i < tilesnum + 2; i++) {
        imagepanel[i] = new JLabel(image[i]);
        add(image[i]);                                         //HERE (2)
    }

}

public static void main(String[] args) {

    GUIv1 selectorframe = new GUIv1();  
    selectorframe.setTitle("MapEditor v2");
    //JFrame mainframe = new JFrame("MapEditor v2");    
    selectorframe.pack();
    selectorframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    selectorframe.setVisible(true);
}   
}
使用getClass.getResource时,图像必须与类文件GUIv1.class位于同一位置,否则,当null值传递到ImageIcon的构造函数时,将产生NPE

如果您不确定在这种情况下类根在何处,图像应位于何处,则可以显示以下结果:

System.out.println(getClass().getProtectionDomain().getCodeSource().getLocation());
在构造函数中

其次,不能将ImageIcon直接添加到JFrame容器中,因为它不是组件。您可以添加Jlabel,这是一个组件:

add(imagepanel[i]);
使用getClass.getResource时,图像必须与类文件GUIv1.class位于同一位置,否则,当null值传递到ImageIcon的构造函数时,将产生NPE

如果您不确定在这种情况下类根在何处,图像应位于何处,则可以显示以下结果:

System.out.println(getClass().getProtectionDomain().getCodeSource().getLocation());
在构造函数中

其次,不能将ImageIcon直接添加到JFrame容器中,因为它不是组件。您可以添加Jlabel,这是一个组件:

add(imagepanel[i]);

此外,您还需要确保资源实际上已经包含在内,即如果它是一个Jar资源,那么这些资源需要包含在Jar中。如果它是一个类目录,同样,需要包含资源+1,您还需要确保资源实际上已经被包含,即如果它是一个Jar资源,则需要将资源包含在Jar中。如果它是一个类目录,那么同样需要包含资源+1什么异常?GetResource不抛出异常,它返回null。如果您遇到异常,它会出现在您的代码中。这可能会引导您走向所需的方向:-什么异常?GetResource不抛出异常,它返回null。如果您遇到异常,它会出现在您的代码中。这可能会引导您朝着所需的方向前进:-