Hello, I've been trying to make a memory game (the classic match pairs one).
CODE
import java.awt. *;
import java.awt.event. *;
import javax.swing. *;
public class Memory extends JFrame implements ActionListener, EventListener{
private JButton ab = new JButton ("Clicky");
private JButton bc = new JButton ("Clicky");
private JButton cd = new JButton ("Clicky");
private JButton de = new JButton ("Clicky");
private JButton ef = new JButton ("Clicky");
private JButton gh = new JButton ("Clicky");
private JButton hi = new JButton ("Clicky");
private JButton ij = new JButton ("Clicky");
private JButton jk = new JButton ("Clicky");
private int[] num = new int [4];
public Memory () {
setLayout (new GridLayout (3,5));
add (ab); add (bc); add (cd); add (ef); add (gh); add (hi); add (ij); add (jk);
getContentPane() .setBackground(Color.blue);
ab.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
bc.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
cd.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
de.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
ef.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
fg.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
gh.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
hi.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
ij.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
jk.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
ab.addActionListener (this);
ab.addMouseListener (this);
bc.addActionListener (this);
de.addActionListener (this);
ef.addActionListener (this);
fg.addActionListener (this);
gh.addActionListener (this);
hi.addActionListener (this);
ij.addActionListener (this);
jk.addActionListener (this);
setSize (350,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent a) {
if (a.getSource () == ab){
ab.setIcon (new ImageIcon ("H:\\Mina bilder\\simon cowell.jpg"));
ab.setText("");
}
else if (a.getSource () == bc){
Icon i1 = new ImageIcon ("H:\\Mina bilder\\simon cowell.jpg");
bc.setIcon (i1);
bc.setText("");
}
else if (a.getSource () == cd){
}
else if (a.getSource () == de){
}
else if (a.getSource () == ef){
}
else if (a.getSource () == fg){
}
else if (a.getSource () == gh){
}
else if (a.getSource () == hi){
}
else if (a.getSource () == jk){
}
else if (a.getSource () == kl){
}
}
public static void main (String[] args){
Memory n = new Memory();
}
}
As you can see I've been making JLabels and adding action listeners to them. However I'm at a loss for how to get the program to recognize when two similar pictures have been selected and so on and so forth. Is this a valid approach or am I just way off?
Any help would be appreciated.