我正在尝试在JPanel上显示图像。我使用ImageIcon来渲染图像,并且图像与类文件位于同一目录中。但是,图像没有显示,并且没有发生错误。有人能帮我找出我的代码出了什么问题吗……
package ev;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Image extends JPanel {
ImageIcon image = new ImageIcon("peanut.jpg");
int x = 10;
int y = 10;
public void paintComponent(Graphics g) {
super.paintComponent(g);
image.paintIcon(this, g, x, y);
}
}发布于 2012-04-10 16:23:50
你应该使用
ImageIcon image = new ImageIcon(this.getClass()
.getResource("org/myproject/mypackage/peanut.jpg"));https://stackoverflow.com/questions/10084787
复制相似问题