Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改存储在阵列中的JLable的ImageIcon?_Java_Imageicon - Fatal编程技术网

Java 如何更改存储在阵列中的JLable的ImageIcon?

Java 如何更改存储在阵列中的JLable的ImageIcon?,java,imageicon,Java,Imageicon,我创建了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")); imgBoxAr

我创建了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
for(int i=0;i
首先,现代惯用Java不使用原始数组,而是使用类型安全的
列表
实现

但要回答您的具体问题:

imgBoxArray[3].setIcon();

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

如果您想更改数组的第i个索引(例如索引
1
)处的JLabel,则只需使用:

imgBoxArray[1].setIcon(new ImageIcon("/Users/cameronmurray/Dropbox/JAVA 2/SmartHome/src/smarthome/clock.jpg")) ;

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

imgBoxArray[index].setIcon(...)

imgBoxArray[foo].setImageIcon(条形)什么让你特别困惑?你不能在循环之外使用
i
,因为它超出范围,只存在于循环内部。这与Swing无关,而与基本Java有关。