【问题标题】:Cant get ImageIcon to display - swing无法让 ImageIcon 显示 - 摆动
【发布时间】:2014-02-12 06:08:38
【问题描述】:
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));
        }

    }

}

我无法让图标显示。我已经浏览了多个关于此的教程,并查看了其他人的代码。

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

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

【问题讨论】:

  • 您是如何编译和构建代码的?使用 Intellij?
  • 您是否尝试过使用完整路径?例如c:/users/collin/ ...
  • @ahmedalkaff Class#getResource(..) 方法需要一个相对于类路径的路径。
  • 第一个应该可以工作,除非 images 从未内置到 bin 中。检查 bin 或 Intellij 中的等效项(我不知道那是什么),看看 images 是否存在。
  • 很可能 intellij 没有在其运行应用程序的类路径中包含 src 目录。您是否编译并构建了项目?你有jar文件吗?如果你这样做,解压缩并查看包含的内容...

标签: java swing intellij-idea imageicon


【解决方案1】:

anything 之前,作为旁注,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 我想我至少展示了我的努力,所以它没有被浪费。也许OP错过了一些东西。没想到会有任何赞美(赞成):D
  • 这是set visible 的事情...您能否编辑您的答案以反映这一点?
猜你喜欢
  • 2015-07-31
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2014-05-04
  • 2019-04-18
  • 2023-03-20
相关资源
最近更新 更多