jeudi 12 mars 2015

Assigning random value to JButton

I need to assign different number from 1 to 8 to JButton Array. But the elements of array has to be different.



Random random = new Random();
JButton[] number = {jButton1,jButton2,jButton3,jButton4,jButton5,jButton6,jButton7,jButton8,jButton9};
for(int i = 0; i<data.lentgh; i++)
{
number[i].setText(1+random.nextInt(9)+" ");
}


With this code, the elements of array can be different and sometimes some of elements can be same. I searched the internet but I couldn't find.


How can I fix this? Thank you.





Aucun commentaire:

Enregistrer un commentaire