0

Please note that I am new to programing, thus my code is probably incredibly redundant and hard to read, but I REALLY need help!

Im trying to create a matching game, so that when the user picks two buttons that relate to one another, you get a match! I'm currently trying to get the action listener to work properly, but whenever I run the code every if statement implements and all the buttons turn green.

public class Panel extends JPanel
{
   int[] num = new int[2];
   private JButton movie1;
   private JButton movie2;
   private JButton movie3;
   private JLabel question;
  private JButton name1;
  private JButton name2;
  private JButton name3;

  public Panel()
  {
     ImageIcon movie1pic = new ImageIcon("movie1.jpg");
     movie1 = new JButton (movie1pic);
     movie1.addActionListener(new ButtonHandler()); 

     ImageIcon movie2pic = new ImageIcon("movie2.png");
     movie2 = new JButton (movie2pic);
     movie2.addActionListener(new ButtonHandler()); 

     ImageIcon movie3pic = new ImageIcon("movie3.png");
     movie3 = new JButton (movie3pic);
     movie3.addActionListener(new ButtonHandler()); 

     question = new JLabel ("match");
     ImageIcon name1pic = new ImageIcon("name1.png");
     name1 = new JButton (name1pic);
     name1.addActionListener(new ButtonHandler());

     ImageIcon name2pic = new ImageIcon("name2.jpg");
     name2 = new JButton (name2pic);
     ImageIcon name3pic = new ImageIcon("name3.jpg");
     name3 = new JButton (name3pic);
     setLayout(new BorderLayout());
     JPanel southPanel = new JPanel(new FlowLayout());
     JPanel northPanel = new JPanel(new
        FlowLayout());
     southPanel.add(name1);
     southPanel.add(name2);
     southPanel.add(name3);
     northPanel.add(movie1);
     northPanel.add(movie2);
     northPanel.add(movie3);
     add(question, BorderLayout.CENTER);
     add(northPanel, BorderLayout.NORTH);
     add(southPanel, BorderLayout.SOUTH);





     private class ButtonHandler implements ActionListener
     {
        public void actionPerformed(ActionEvent e)
        { 
           if ( e.getSource() == "movie1.jpg" );
           {
              num[0] = 1;
              ImageIcon green = new ImageIcon("green.png");
              movie1.setIcon(green);
           }
           if (e.getSource() == "movie2.png");
           {
              num[0] = 2;
              ImageIcon green = new ImageIcon("green.png");
              movie2.setIcon(green);
           }
           else if (e.getSource() =="movie3.png" );
           {
              num[0] = 3;
              ImageIcon green = new ImageIcon("green.png");
              movie3.setIcon(green);
           }  


        }
     }
  }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Raina
  • 1
  • 1
  • 1
    Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Jun 02 '17 at 02:01
  • 1
    You could also consider using `ActionEvent#getActionCommand`, which you can configure from using `JButton#setActionCommand` to determine what the button means – MadProgrammer Jun 02 '17 at 02:02
  • Also, I don't think you need to have more than one instance of the ButtonHandler class, try to use the same instance. That will save some memory ;-) – Ademir Constantino Jun 02 '17 at 02:14

0 Answers0