Java ImageIcon不适用于我

Java ImageIcon不适用于我,java,swing,imageicon,Java,Swing,Imageicon,我编写了非常简单的代码来显示葡萄的图标,但代码仍然没有显示任何内容 这是我的密码 import javax.swing.*; import java.awt.*; public class Code { ImageIcon ii = new ImageIcon("image/grapes2.jpg"); JLabel label = new JLabel("Grapes", ii, SwingConstants.CENTER); JFrame frame = new JFrame("Image

我编写了非常简单的代码来显示葡萄的图标,但代码仍然没有显示任何内容

这是我的密码

import javax.swing.*;
import java.awt.*;

public class Code {
ImageIcon ii = new ImageIcon("image/grapes2.jpg");
JLabel label = new JLabel("Grapes", ii, SwingConstants.CENTER);
JFrame frame = new JFrame("ImageIcon");

public void ui(){
    label.setHorizontalTextPosition(SwingConstants.CENTER);
    label.setVerticalTextPosition(SwingConstants.BOTTOM);
    label.setIconTextGap(5);

    label.setOpaque(true);
    label.setBackground(Color.GRAY);

    frame.setSize(2300,2300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);

}

}

如果在最后调用
frame.setVisible(true)
后图像图标仍不可见,请查看在相同上下文中询问的我的其他帖子


试一试


这是值得一读的,这是直接从那里得到的样本

ImageIcon icon = createImageIcon("images/middle.gif",
                             "a pretty but meaningless splat");
label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
...
label3 = new JLabel(icon);

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
请阅读更多直观的解释

例如,目录omega中的类文件。图像位于
omega/images
目录中


图像放在哪里?您是否将图像放在图像目录中以及java文件所在的目录中?同一项目文件中有一个文件。。名为“imege”的文件应该是
image
而不是
imege
。只需重新调整框架的大小,看看会发生什么。对不起。。这是一张照片。。拼写错误。。我的坏**+我重新调整了大小,但什么也没发生。同样的错误
ImageIcon icon = createImageIcon("images/middle.gif",
                             "a pretty but meaningless splat");
label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
...
label3 = new JLabel(icon);

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}