1

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 2

2

ImageIcon[] array and when clicking the button: label.setIcon(array[i]) in the actionPerformed method of the corresponding ActionListener.

Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou. Thomas the [] can have a number in it?
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).
Thanks Thomas Ive tried doing this how ever i am unsure how to initialise. pastebin.com/w935XcA6
Well, you already initialize the first element: ImageIcon[0] array = new ImageIcon("6.png"); Just do the same for the rest.
2

Also consider this alternate implementation of Card.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.