Rachel
Rachel

Reputation: 23

Java- Action Listener to check whether two buttons have been pressed

I would like to create a program where there are 4 buttons and the user needs to match these together.

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.

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.

Thanks.

Here is my 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();
    }
}

Upvotes: 0

Views: 10766

Answers (2)

A human being
A human being

Reputation: 1220

Since at a time, only one ActionEvent will be fired, you need to set a flag whenever button1 or button3 is pressed for first time, second time, whenever a button1/button3 is pressed, check the status of the flag, if it's true change the color of the button, else don't change. Also don't forget to set the flag to false, when you'll be done with color change.Here is the 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 implements ActionListener {

    JFrame frame;
    JPanel panel;
    boolean flag;
    String buttonPressed;
    final JButton button1;
    final JButton button2;
    final JButton button3;
    final JButton button4;
    Color color1, color2;

    public test3() {

    /*Frame and panel */

    frame = new JFrame("Keyboard");
    panel = new JPanel();
    color1 = new Color(0,0,0);

    /* Buttons fot letters*/
    button1 =new JButton("button1");
    button2 =new JButton("button2");
    button3 =new JButton("button3");
    button4 =new JButton("button4");

    color2 = button1.getBackground();

    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());

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);

    panel.add(button1);
    panel.add(button2); 
    panel.add(button3);
    panel.add(button4);
    flag = false;
    buttonPressed = "";
    }

    public void actionPerformed(ActionEvent ae)
    {
    JButton b = (JButton)ae.getSource();
    if( b.equals(button1) )
        {
        flag = true;
        if( buttonPressed.equals("button3") )
            {
            if( button1.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else if( b.equals(button3) ) 
        {
        flag = true;
        if( buttonPressed.equals("button1") )
            {
            if( button3.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else
        {
        flag = false;
        }

    }

    public static void main(String[] args) {
    new test3();
    }
}

Upvotes: 4

0x6C38
0x6C38

Reputation: 7076

You are better off using toogleButtons:

    final JToggleButton tb1 = new JToggleButton();
    final JToggleButton tb2 = new JToggleButton();
    final JToggleButton tb3 = new JToggleButton();
    final JToggleButton tb4 = new JToggleButton();


    ChangeListener stateChangeListener = new javax.swing.event.ChangeListener() {
        @Override
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            toggleButtonStateChanged(evt);
        }

        private void toggleButtonStateChanged(ChangeEvent evt) {
            if (tb1.isSelected() && tb3.isSelected()) {
                tb1.setBackground(Color.WHITE);
                tb2.setBackground(Color.WHITE);
                tb3.setBackground(Color.WHITE);
                tb4.setBackground(Color.WHITE);
            } else {
                //Set the color to whatever it was before
            }
        }
    };

    tb1.addChangeListener(stateChangeListener);
    tb2.addChangeListener(stateChangeListener);
    tb3.addChangeListener(stateChangeListener);
    tb4.addChangeListener(stateChangeListener);

Upvotes: 2

Related Questions