图像不会出现在JLabel中

Bri*_*own 2 java user-interface swing image jframe

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

我生成的代码:

private void initComponents() {
        //...
        jLabel1 = new JLabel(new ImageIcon(myPicture));
}
Run Code Online (Sandbox Code Playgroud)

我的班级代码:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

但我仍然没有看到图像...... (图像文件的路径很好)它的样子:

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

Sha*_*jee 6

你可以这样使用

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