2016-10-02 169 views
0

我从JPanel中创建了一个棋盘。使用的ImageIcon不工作,所以我看了看现场,但是这一切看起来很复杂,我如何将图像添加到一个数组一样将图像添加到数组中JPanel

tiles[0][0].setIcon(br); 

这是我的棋盘

创建的JPanel
private JPanel[][] tiles = new JPanel[6][6]; 

我已经试过这样:

ImageIcon bn = new ImageIcon("art/BN.gif"); 
    ImageIcon bb = new ImageIcon("art/BB.gif"); 
    ImageIcon br = new ImageIcon("art/BR.gif"); 
    ImageIcon wn = new ImageIcon("art/WN.gif"); 
    ImageIcon wb = new ImageIcon("art/WB.gif"); 
    ImageIcon wr = new ImageIcon("art/WR.gif"); 
    tiles[0][0].add(new JLabel(bn)); 
    tiles[0][1].add(new JLabel(wn)); 
    tiles[0][2].add(new JLabel(wb)); 
    tiles[0][3].add(new JLabel(wb)); 
    tiles[0][4].add(new JLabel(wn)); 
    tiles[0][5].add(new JLabel(wr)); 
    tiles[5][0].add(new JLabel(br)); 
    tiles[5][1].add(new JLabel(bn)); 
    tiles[5][2].add(new JLabel(bb)); 
    tiles[5][3].add(new JLabel(bb)); 
    tiles[5][4].add(new JLabel(bn)); 
    tiles[5][5].add(new JLabel(br)); 

但它不工作

+0

可以看到面板?你可以看到标签?有什么错误? –

+0

@Fast SnailCant查看标签 – PPasf

+0

ambs的答案是否适合您? –

回答

0

哪里是你的图像被存储?什么不工作?

我要在黑暗中拍摄一张照片,并假设您正在尝试加载嵌入在应用程序中的文件。

取自; https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html

ImageIcon(String filename) 

从指定文件一个ImageIcon。

ImageIcon(URL location) 

从指定的URL创建一个ImageIcon。

试试这个;

ImageIcon bn = new ImageIcon(getClass().getResource("art/BN.gif")); 

它会尝试创建由.getResource返回的URL一个ImageIcon()