0

我创建了 9 个 JLables 并设置了默认图像。

现在我需要更改数组中的特定 JLable 图像。我怎样才能做到这一点?

谢谢!

 for (int i = 0; i < imgBoxArray.length; i++)
    {
        imgBoxArray[i] = new JLabel(new ImageIcon("/Users/cameronmurray/Dropbox/JAVA 2/SmartHome/src/smarthome/clock.jpg"));
        imgBoxArray[i].setOpaque(true);
        imagePanel.add(imgBoxArray[i]);
    }    

    imgBoxArray[i].ImageIcon("/Users/cameronmurray/Dropbox/JAVA 2/SmartHome/src/smarthome/clock.jpg")); //ERROR
4

3 回答 3

0

首先,现代惯用的 Java 不使用原始数组,它使用类型安全List<JLabel>的实现。

但要回答你的具体问题:

imgBoxArray[3].setIcon();

将让您修改第 4 个插槽中的内容。

于 2013-04-28T20:38:35.183 回答
0

如果你想在数组的ith索引(比如索引)处更改 JLabel,那么你可以简单地使用:1

imgBoxArray[1].setIcon(new ImageIcon("/Users/cameronmurray/Dropbox/JAVA 2/SmartHome/src/smarthome/clock.jpg")) ;
于 2013-04-28T20:39:14.927 回答
0

检索“好”的 JLabel 索引,然后:

imgBoxArray[index].setIcon(...)
于 2013-04-28T20:39:19.547 回答