Ajuda com array

2 respostas
fabiodurgante

seguinte

ImageIcon[] icon= null; 
 JLabel[] labels = new JLabel[2];  

       icon[0] = new ImageIcon("MS.GIF"); 
        icon[1] = new ImageIcon("tomcat.gif"); 

  for(int i = 0; i < labels.length; i++) {
      labels[i] = new JLabel(icone);
      labels[i].setIcon(icone);
           return  labels[i];
   }
porem aqui

icon[0] = new ImageIcon(MS.GIF);

icon[1] = new ImageIcon(tomcat.gif);
trava todo o programa ele nao coloca no array as image se eu fizer assim

ImageIcon icone=null;

icone = new  ImageIcon(tomcat.gif);

no lugar do array funciona mas quero setar para cada jlabel uma imagem

2 Respostas

fabiodurgante

resolvido

ImageIcon[] icon= new ImageIcon[2];

M

Faltou instanciar seu array de ImageIcon.
Veja:

// cria e instancia o array de ImageIcon e JLabel
ImageIcon[] icons = new ImageIcon[2]; // faltou isso!
JLabel[] labels = new JLabel[2];

// carrega as imagens no array
icon[0] = new ImageIcon("MS.GIF");   
icon[1] = new ImageIcon("tomcat.gif");

// dentro de um laço for, muda a imagem dos JLabel
for (int i = 0; i < labels.length; i++) {
    labels[i].setIcon(icons[i]);
}

E no trecho os JLabel também não são instanciados. Se você não fez isso em outra parte do código, trate de fazer! :slight_smile:

Criado 7 de fevereiro de 2009
Ultima resposta 7 de fev. de 2009
Respostas 2
Participantes 2