How to manipulate Labels using a loop

1

How can I use these labels using a loop, instead of creating 40 lines of code repeated?

jLabel1.setIcon(new ImageIcon(getClass().getResource("/cards/"+cards.get(1)+".png")));
jLabel2.setIcon(new ImageIcon(getClass().getResource("/cards/"+cards.get(1)+".png")));
jLabel3.setIcon(new ImageIcon(getClass().getResource("/cards/"+cards.get(2)+".png")));
jLabel4.setIcon(new ImageIcon(getClass().getResource("/cards/"+cards.get(3)+".png")));
    
asked by anonymous 12.09.2014 / 11:57

1 answer

2
___ erkimt ___ How to manipulate Labels using a loop ______ qstntxt ___

How can I use these labels using a loop, instead of creating 40 lines of code repeated?

JLabel [ ] labels = new JLabel [40];
for (int i=0; i < labels.length; i++){
   labels[i] = new JLabel ( );
   //Todo o codigo que voce precisar que aqui.
}
    
______ ___ azszpr32280

Rodrigo,

You can create a jLabel vector:

JLabel [ ] labels = new JLabel [40];
for (int i=0; i < labels.length; i++){
   labels[i] = new JLabel ( );
   //Todo o codigo que voce precisar que aqui.
}
    
___
12.09.2014 / 12:43