Java中的图像即将出现NPE

问题描述 投票:0回答:1

我正在尝试在java中放图片。我的最终目标是做一个我可以用方向键控制的图像,但我似乎不能把图像放入。我导入了图像,而且名称也是正确的。我一直得到一个空指针异常。

代码。

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

public class Test extends JPanel {
public static ImageIcon image;


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        image = new ImageIcon(getClass().getResource("rocketJava.jpg"));
        image.paintIcon(this,g, 20, 20);

    }
    public static void main(String[] args) {
        JFrame f= new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Test s = new Test();
        f.add(s);

        f.setSize(600,600);
        f.setVisible(true);
    }
}
java image swing nullpointerexception embedded-resource
1个回答
0
投票

问题只是我需要把图片文件移到bin文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.