Hi, this is a matching pairs game i'm making. basically if the user manages to find a matching pair they score a hit if not they score a miss. What i'm finding though, is that when i click on the cards (JToggleButtons), they wont change, only once i press the 'solve' button do they change on the click of a mouse. I can't figure it out for the life of me? Please could someone show me where im going wrong.. thanks!
import javax.swing.*; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ItemListener; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.*; public class MatchingPairs { // Create images for toggle buttons private ImageIcon backImg = new ImageIcon("back.jpg"); private ImageIcon heartImg = new ImageIcon("heart.jpg"); private ImageIcon diamondImg = new ImageIcon("diamond.jpg"); private ImageIcon spadeImg = new ImageIcon("spade.jpg"); private ImageIcon clubImg = new ImageIcon("club.jpg"); //Create JToggleButtons private JToggleButton heart = new JToggleButton(backImg); private JToggleButton heartDup = new JToggleButton(backImg); private JToggleButton diamond = new JToggleButton(backImg); private JToggleButton diamondDup = new JToggleButton(backImg); private JToggleButton spade = new JToggleButton(backImg); private JToggleButton spadeDup = new JToggleButton(backImg); private JToggleButton club = new JToggleButton(backImg); private JToggleButton clubDup = new JToggleButton(backImg); //Create JButtons private JButton scramble = new JButton("Scramble"); private JButton solve = new JButton("Solve"); //Create JTextFields private JTextField hitValue = new JTextField(3); private JTextField missValue = new JTextField(3); //Create JLabels private JLabel hitLabel = new JLabel("Hits"); private JLabel missLabel = new JLabel("Miss"); // Variables private int hit; private int miss; //Window for GUI private JFrame window = new JFrame("Matching Pairs"); private JPanel cardTable = new JPanel(); // MatchingPairs() - constructor public MatchingPairs(){ // configure GUI window.add(cardTable); window.setSize(1400, 800); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setBackground(Color.GREEN); window.add(cardTable); cardTable.setBackground(Color.GREEN); hitValue.setEditable(false); hitValue.setFont(new Font("Arial",Font.BOLD,30)); hitValue.setText(String.valueOf(hit)); missValue.setEditable(false); missValue.setFont(new Font("Arial",Font.BOLD,30)); missValue.setText(String.valueOf(miss)); // add event listeners solve.addActionListener(new SolveButton()); scramble.addActionListener(new ScrambleButton()); heart.addItemListener(new Cards()); heartDup.addItemListener(new Cards()); diamond.addItemListener(new Cards()); diamondDup.addItemListener(new Cards()); spade.addItemListener(new Cards()); spadeDup.addItemListener(new Cards()); club.addItemListener(new Cards()); clubDup.addItemListener(new Cards()); cardTable.setLayout(new GridLayout(3, 6, 10, 10)); cardTable.add(hitValue); hitValue.setPreferredSize(new Dimension(180, 300)); cardTable.add(missValue); cardTable.add(scramble); cardTable.add(solve); cardTable.add(heart); cardTable.add(heartDup); cardTable.add(diamond); cardTable.add(diamondDup); cardTable.add(spade); cardTable.add(spadeDup); cardTable.add(club); cardTable.add(clubDup); //display GUI window.setVisible(true); } // handle solve button press class SolveButton implements ActionListener{ public SolveButton(){ } public void actionPerformed(ActionEvent e){ // Switch ImageIcon to card faces heart.setSelectedIcon(heartImg); heartDup.setSelectedIcon(heartImg); diamond.setSelectedIcon(diamondImg); diamondDup.setSelectedIcon(diamondImg); spade.setSelectedIcon(spadeImg); spadeDup.setSelectedIcon(spadeImg); club.setSelectedIcon(clubImg); clubDup.setSelectedIcon(clubImg); } } // handle scramble button press (Does nothing at the moment) class ScrambleButton implements ActionListener { public ScrambleButton(){ } } class Cards implements ItemListener{ // Flip card on click public Cards(){ } public void itemStateChange(ItemEvent e){ if (e.getStateChange() == ItemEvent.SELECTED){ heart.setSelectedIcon(heartImg); heartDup.setSelectedIcon(heartImg); diamond.setSelectedIcon(diamondImg); diamondDup.setSelectedIcon(diamondImg); spade.setSelectedIcon(spadeImg); spadeDup.setSelectedIcon(spadeImg); club.setSelectedIcon(clubImg); clubDup.setSelectedIcon(clubImg); } } } public static void main(String[] args) { MatchingPairs gui = new MatchingPairs(); } }