0

I am creating a memory card game for class and am trying to figure out how to make my images clickable and evaluated in Java. I have used the previous tutorial on here about it but my images are named numerically, so they can be randomized.

It's giving me errors for that and I don't understand why.

Also, how to create some sort of evaluation method for these cards they are named 0-8 (0 being the back side of the card)? I know it needs to be some sort of boolean code.

  • Also I would like to know how to create some sort of evaluation method for these cards they are named 0-8. 0 being the back side of the card. I know it needs to be some sort of boolean code. – nick titus May 01 '12 at 19:38
  • *"Its giving me errors for that and I dont understand why."* I don't understand how you expect us to understand them if you do not copy/paste them into the question. BTW - a question can be edited. Please edit your comment back into the question, along with the compilation or run-time error output, & we can offer better help. – Andrew Thompson May 01 '12 at 20:13
  • *"my images are named numerically, so they can be randomized."* Things which are *not* named numerically, can easily be randomized. – Andrew Thompson May 01 '12 at 20:23
  • } ImageIcon 0 = new ImageIcon("img/0.jpg"); JButton guess = new JButton(0); guess.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) – nick titus May 01 '12 at 20:24
  • that is what I have for code for my button that is my guess function u could call it – nick titus May 01 '12 at 20:25
  • is there a way i can post all of my code on here? – nick titus May 01 '12 at 20:26
  • *"is there a way i can post all of my code on here?"* Probably not, if 'all' means 'a lot'. For better help sooner, post an [SSCCE](http://sscce.org/). It takes around a dozen lines of code to make an SSCCE of many simple compilation errors. When posting code, be sure to use code formatting. To do that, select the sample then click the `{}` button above the message posting/editing form. – Andrew Thompson May 01 '12 at 20:38

1 Answers1

2

Use a JButton with an ActionListener. An Icon can be set for a variety of conditions in the button, but if that does not suit the task, you might just change them in the actionPerformed() method of the listener.

This example shows some tricks to dealing with images in JToggleButton that also apply to JButton - removing the borders around the image.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433