How can I make an array of image icons? When I click a JButton on a different class, the ImageIcons should be set to a JLabel.
2 Answers
ImageIcon[] array and when clicking the button: label.setIcon(array[i]) in the actionPerformed method of the corresponding ActionListener.
4 Comments
sam
Thankyou. Thomas the [] can have a number in it?
Thomas
1. You could do
ImageIcon[] array = new ImageIcon[10]; for an array of 10 ImageIcon instances, just like any other array (note that you'd still need to initialize the array elements). 2. You should be able to use that approach in what you pasted. In that case, however, I'd suggest to use a Map<card, ImageIcon> instead of an array in order to make looking up the correct ImageIcon easier (however, you could also use D_A.ordinal(), for example).sam
Thanks Thomas Ive tried doing this how ever i am unsure how to initialise. pastebin.com/w935XcA6
Thomas
Well, you already initialize the first element:
ImageIcon[0] array = new ImageIcon("6.png"); Just do the same for the rest.