图片不会出现在JLabel中 [英] Image won't appear in JLabel

查看:80
本文介绍了图片不会出现在JLabel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Netbeans的GUI Builder为我的应用程序创建了GUI.我试图显示包含JLabel和图像的JFrame,但无法显示Image.

I've created GUI for my application using Netbeans' GUI Builder. I am trying to display a JFrame containing a JLabel with an image, and I can't get the Image to display.

我生成的代码:

private void initComponents() {
        //...
        jLabel1 = new JLabel(new ImageIcon(myPicture));
}

我的班级代码:

public class GUIWindow extends javax.swing.JFrame {

    BufferedImage myPicture;

    /** Creates new form GUIWindow */
    public GUIWindow() throws IOException {
        myPicture = ImageIO.read(new File("images/logo.png"));
        initComponents();
        this.add(jLabel1);

    }
}

但是我仍然看不到图像... (图像文件的路径很好)诸如此类:

but I still don't see an image ... (path to the image file is fine) its sth like:

my-project :
  /build
  /dist
  /images/logo.png
  /nbproject
  /src (here I have all my source files)
  /build.xml
  /manifest.mf

推荐答案

您可以这样使用

URL imgSmartURL = this.getClass().getResource("your image path");
jLabel1 = new JLabel(new ImageIcon(imgSmartURL), JLabel.CENTER);

这篇关于图片不会出现在JLabel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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