title: Java- Action Listener to check whether two buttons have been pressed
body: <p>I would like to create a program where there are 4 buttons and the user needs to match these together. </p>
  
  <p>So if there are 4 buttons: button1, button2, button3, button4 and the user presses button1 and button3 then the buttons change colour. else they stay the same. </p>
  
  <p>i have tried using an action listener and an if statement within the action listener but i am not too sure how i would do it so that it checks if both buttons have been pressed.</p>
  
  <p>Thanks. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.Color.*;
  import javax.swing.Box;
  import java.awt.Dimension;
  import java.awt.Insets;
  import java.awt.FlowLayout;
  import java.awt.event.*;
  import javax.swing.border.LineBorder;
  import javax.swing.border.EmptyBorder;
  import javax.swing.BorderFactory;
  import java.io.*;
  import java.net.URL;
  import javax.sound.sampled.*;
  
  
  public class test3 extends JPanel {
  
      JFrame frame;
      JPanel panel;
  
      public test3() {
  
      /*Frame and panel */
  
      frame = new JFrame("Keyboard");
      panel = new JPanel();
  
      /* Buttons fot letters*/
      final JButton button1 =new JButton("button1");
      final JButton button2 =new JButton("button2");
      final JButton button3 =new JButton("button3");
      final JButton button4 =new JButton("button4");
  
      frame.setVisible(true);
      frame.setSize(800,600);
      frame.setLocationRelativeTo(null);
  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); 
  
      panel.setLayout(null);
      panel.setBackground(Color.WHITE);
      panel.setCursor( new Cursor(Cursor.HAND_CURSOR) ); // set the cursor to a hand 
  
      frame.add(panel);
  
      Insets insets = panel.getInsets();
  
      button1.setLayout(null);
      button1.setBounds(130 + insets.left, 300 + insets.top, 50,50);
      button1.setBackground(Color.WHITE);
      button1.setBorder(BorderFactory.createEmptyBorder());
  
      button2.setLayout(null);
      button2.setBounds(180 + insets.left, 300 + insets.top,  50,50);
      button2.setBackground(Color.WHITE);
      button2.setBorder(BorderFactory.createEmptyBorder());
  
      button3.setLayout(null);
      button3.setBounds(230 + insets.left, 300 + insets.top,  50,50);
      button3.setBackground(Color.WHITE);
      button3.setBorder(BorderFactory.createEmptyBorder());
  
      button4.setLayout(null);
      button4.setBounds(280 + insets.left, 300 + insets.top,  50,50);
      button4.setBackground(Color.WHITE);
      button4.setBorder(BorderFactory.createEmptyBorder());
  
      panel.add(button1);
      panel.add(button2); 
      panel.add(button3);
      panel.add(button4);
      }
  
  
      public static void main(String[] args) {
      new test3();
      }
  }
  </code></pre>