显示ImageIcon [英] Displaying an ImageIcon

查看:134
本文介绍了显示ImageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 JPanel 上显示图像。我使用 ImageIcon 来渲染图像,图像与类文件位于同一目录中。但是,图像未显示,并且没有发生错误。任何人都可以协助解决我的代码有什么问题...

I am trying to display an image on a JPanel. I'm using an ImageIcon for rendering the image, and the image is in the same directory as the class file. However, the image is not being displayed, and there are no errors occuring. Could anyone please assist in working out what's wrong with my code...

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);
    }
}


推荐答案

你应该使用

ImageIcon image = new ImageIcon(this.getClass()
                .getResource("org/myproject/mypackage/peanut.jpg"));

这篇关于显示ImageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆