I want to make a Memory Game with 4 x 4 buttons(JButtons - 16 blank buttons) and a "Play Again" and "Exit" button below. The 4 x 4 buttons are blank as the game starts, but when you push one of the buttons it should show a number. You can only click two buttons in a row. And if the two buttons show the same numbers, these buttons shall be disabled.
If you click "Play Again", the game should restart and if you click "Exit", you exit the application. If you complete the game, a window is suposed to show saying "Congratulations, you made it!".
Someone knows how to make that? Have to make it for a scool-delivery.
Have you read The Swing Tutorial? Once you've been through that make an attempt yourself and then come back with specific questions.
The tutorial is nice, but my main problem is how to make the programming for the application. The actual handlings, not the design. Programming with for-loops and math.random and so on. One of the problems is to make 16 JButtons, but in a array. I've tried it many times, but its not working. I propably write something wrong...
Do you mean in a JButton, or arranged in a grid? Post your code attempting this and we'll have a look.
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) and Math (Java Platform SE 6) will tell you all about for-loops and math.random
public class Memory extends JFrame implements ActionListener
private JButton btnPlayAgain, btnExit;
private int  hiddenNumbers;
JButton button = new JButton;
JPanel p1 = new JPanel();
for(int i = 0; i < button.length; i++)
button[i] = new JButton(Integer.toString(i+1));
Container c = getContentPane();
btnPlayAgain = new JButton("Play Again");
btnExit = new JButton("Exit");
JPanel pnlTopp = new JPanel(new GridLayout(1,1));
public static void main(Stringargs)
public void actionPerformed(ActionEvent event)
JButton clicked = (JButton)event.getSource();
if(clicked == btnExit)
else if(clicked == btnPlayAgain)
This may not be very good coding, because i've mixed together some few codes. hiddenNumbers are not made proparly, because i don't know how to hide them:P....
Remember to call validate() or revalidate() when you're done adding and removing components.
Not sure what you mean. Which method is that?