gpt4 book ai didi

java - Java 图像即将出现在 NPE 中

转载 作者:行者123 更新时间:2023-12-01 16:40:05 26 4
gpt4 key购买 nike

我正在尝试将图像放入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);
}
}

最佳答案

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

关于java - Java 图像即将出现在 NPE 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61876064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com