简体   繁体   English

如何更改数组中存储的JLable的ImageIcon?

[英]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. 我有9个JLable,它们是我创建并设置默认图像的。

Now I need to change a specific JLable image within the array. 现在,我需要更改数组中的特定JLable图像。 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. 首先,现代惯用的Java不使用原始数组,而是使用类型安全的List<JLabel>实现。

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(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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