简体   繁体   中英

How do I change the ImageIcon of a JLable stored within an array?

I have 9 JLables which I created and set a default image.

Now I need to change a specific JLable image within the array. How can I do this?

Thanks!

 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

First off modern idiomatic Java doesn't use raw arrays, it uses type safe List<JLabel> implementations.

But to answer you specific question:

imgBoxArray[3].setIcon();

will let you modify what is in the 4th slot.

如果要更改数组的ith索引(例如索引1 )的JLabel,则可以简单地使用:

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

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

imgBoxArray[index].setIcon(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM