无法显示ImageIcon - 摆动

时间:2014-02-12 06:08:39

标签: java swing intellij-idea imageicon

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


    /* Constructor credited to stackoverflow user ranzy
        http://stackoverflow.com/questions/2535417/chess-board-in-java */
    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));
        }

    }

}

我无法显示图标。我已经查看过这方面的多个教程,并查看了其他人的代码。

enter image description here

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

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

1 个答案:

答案 0 :(得分:3)

  

任何内容之前,作为旁注,{<1}}应该是最后之后添加所有内容部件

另请查看以下内容

使用setVisible

为我工作正常
getClass().getResource()

检查以下所有内容,然后在更正时构建,然后运行它。

enter image description here

构建之后

文件结构。图像应该复制到类路径

enter image description here

enter image description here

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

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