无法显示ImageIcon - swing。

3
public class GUI
{
    JFrame frame;
    JPanel squares[][];


    /* Constructor credited to stackoverflow user ranzy
        https://dev59.com/lkzSa4cB1Zd3GeqPqNpJ */
    public GUI()
    {
        frame = new JFrame("Chess");
        squares = new JPanel[8][8];
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(8, 8));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.white);
                } else {
                    squares[i][j].setBackground(Color.orange);
                }
                frame.add(squares[i][j]);
            }
        }

        ImageIcon pawnW = new ImageIcon(getClass().getResource("/images/pawnW.png"));
        ImageIcon knightW = new ImageIcon("images/knightW.png");
        ImageIcon bishopW = new ImageIcon("/images/bishopW.png");
        ImageIcon rookW = new ImageIcon("/images/rookW.png");
        ImageIcon queenW = new ImageIcon("/images/queenW.png");
        ImageIcon kingW = new ImageIcon("/images/kingW.png");

        ImageIcon pawnB = new ImageIcon("/images/pawnB.png");
        ImageIcon knightB = new ImageIcon("/images/knightB.png");
        ImageIcon bishopB = new ImageIcon("/images/bishopB.png");
        ImageIcon rookB = new ImageIcon("/images/rookB.png");
        ImageIcon queenB = new ImageIcon("/images/queenB.png");
        ImageIcon kingB = new ImageIcon("/images/kingB.png");

        squares[0][0].add(new JLabel(rookW));
        squares[1][0].add(new JLabel(knightW));
        squares[2][0].add(new JLabel(bishopW));
        squares[3][0].add(new JLabel(queenW));
        squares[4][0].add(new JLabel(kingW));
        squares[5][0].add(new JLabel(bishopW));
        squares[6][0].add(new JLabel(knightW));
        squares[7][0].add(new JLabel(rookW));

        squares[0][7].add(new JLabel(rookB));
        squares[1][7].add(new JLabel(knightB));
        squares[2][7].add(new JLabel(bishopB));
        squares[3][7].add(new JLabel(queenB));
        squares[4][7].add(new JLabel(kingB));
        squares[5][7].add(new JLabel(bishopB));
        squares[6][7].add(new JLabel(knightB));
        squares[7][7].add(new JLabel(rookB));

        for (int i = 0; i < 8; i++)
        {
            squares[i][1].add(new JLabel (pawnW));
            squares[i][6].add(new JLabel (pawnB));
        }

    }

}

我无法显示图标。我查看了多个相关教程以及其他人的代码。

在此输入图片描述

我尝试了三种不同的方法:

ImageIcon pawnW = new ImageIcon(getClass().getResource("/images/pawnW.png"));
ImageIcon knightW = new ImageIcon("images/knightW.png");
ImageIcon bishopW = new ImageIcon("/images/bishopW.png");

你是如何编译和构建你的代码的?使用IntelliJ吗? - Sotirios Delimanolis
2
@ahmedalkaff Class#getResource(..) 方法期望一个相对于类路径的路径。 - Sotirios Delimanolis
1
第一个应该可以工作,除非 images 从未构建到 bin 中。检查 bin 或其在 Intellij 中的等效物(我不知道那是什么),看看 images 是否存在。 - Paul Samsotha
@SotiriosDelimanolis 我的意思是可以尝试一下,如果成功了,就可以得出关于原因的结论。 - ahmedalkaff
1
很可能Intellij没有将src目录包含在运行应用程序的类路径中。您是否编译并构建了该项目?您有Jar文件吗?如果有,解压缩并查看其中包含了什么... - MadProgrammer
显示剩余10条评论
1个回答

6

在任何操作之前,需要注意的是,在添加所有组件之后,setVisible 应该是您做的最后一件事

另外请检查以下内容

使用 getClass().getResource() 对我来说运行良好

 String path = "/images/stackoverflow2.png";

 ImageIcon pawnW = new ImageIcon(getClass().getResource(path));

勾选以下所有内容,然后进行构建,最后运行。

在此输入图片描述

文件结构在构建之后。 图像应该被复制到类路径中。

在此输入图片描述

在此输入图片描述

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUI
{
    JFrame frame;
    JPanel squares[][];

    public GUI()
    {
        frame = new JFrame("Chess");
        squares = new JPanel[8][8];
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(8, 8));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.white);
                } else {
                    squares[i][j].setBackground(Color.orange);
                }
                frame.add(squares[i][j]);
            }
        }

        String path = "/images/stackoverflow2.png";

        ImageIcon pawnW = new ImageIcon(getClass().getResource(path));
        ImageIcon knightW = new ImageIcon(getClass().getResource(path));
        ImageIcon bishopW = new ImageIcon(getClass().getResource(path));
        ImageIcon rookW = new ImageIcon(getClass().getResource(path));
        ImageIcon queenW = new ImageIcon(getClass().getResource(path));
        ImageIcon kingW = new ImageIcon(getClass().getResource(path));

        ImageIcon pawnB = new ImageIcon(getClass().getResource(path));
        ImageIcon knightB = new ImageIcon(getClass().getResource(path));
        ImageIcon bishopB = new ImageIcon(getClass().getResource(path));
        ImageIcon rookB = new ImageIcon(getClass().getResource(path));
        ImageIcon queenB = new ImageIcon(getClass().getResource(path));
        ImageIcon kingB = new ImageIcon(getClass().getResource(path));

        squares[0][0].add(new JLabel(rookW));
        squares[1][0].add(new JLabel(knightW));
        squares[2][0].add(new JLabel(bishopW));
        squares[3][0].add(new JLabel(queenW));
        squares[4][0].add(new JLabel(kingW));
        squares[5][0].add(new JLabel(bishopW));
        squares[6][0].add(new JLabel(knightW));
        squares[7][0].add(new JLabel(rookW));

        squares[0][7].add(new JLabel(rookB));
        squares[1][7].add(new JLabel(knightB));
        squares[2][7].add(new JLabel(bishopB));
        squares[3][7].add(new JLabel(queenB));
        squares[4][7].add(new JLabel(kingB));
        squares[5][7].add(new JLabel(bishopB));
        squares[6][7].add(new JLabel(knightB));
        squares[7][7].add(new JLabel(rookB));

        for (int i = 0; i < 8; i++)
        {
            squares[i][4].add(new JLabel (pawnW));
            squares[i][6].add(new JLabel (pawnB));
        }

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new GUI();
            }
        });
    }
}

@Alex2410,我觉得至少要展示一下我的努力,这样它就不会被浪费了。也许楼主漏看了什么。我并不指望因此得到任何表扬(点赞):D - Paul Samsotha
那是“设置可见性”的问题... 你能否编辑你的回答以反映这一点? - Collin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接