troubleshooting Question

Help with Combination Lock java program

Avatar of jwhmack
jwhmack asked on
Java
5 Comments1 Solution971 ViewsLast Modified:
Hi, I am doing an assignment for college where I have to create a combination lock program where I click a three button sequence and it closes the frame.  I chose the sequence of 3-9-3.  I can get it to react to the first number of 3 but after that it will not continue on to my else if logic to the number 9.  Can anyone offer me any suggestions on how I can write this in order to get this to work?

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;

public class Assign3 extends JFrame implements ActionListener
{

  int count=0;
 
  // create buttons for panels
  JButton b0 = new JButton("0");
  JButton b1 = new JButton("1");
  JButton b2 = new JButton("2");
  JButton b3 = new JButton("3");
  JButton b4 = new JButton("4");
  JButton b5 = new JButton("5");
  JButton b6 = new JButton("6");
  JButton b7 = new JButton("7");
  JButton b8 = new JButton("8");
  JButton b9 = new JButton("9");

  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel();
  JPanel panel4 = new JPanel();


  public Assign3()
    {
      setTitle( "Combination Lock" );

	  // add label and text pairs to the small panels
      panel1.add( b0 );
      panel2.add( b1 ); panel2.add( b2 ); panel2.add( b3 );
      panel3.add( b4 ); panel3.add( b5 ); panel3.add( b6 );
      panel4.add( b7 ); panel4.add( b8 ); panel4.add( b9 );


      b0.setActionCommand( "0" );
	  b1.setActionCommand( "1" );
	  b2.setActionCommand( "2" );
	  b3.setActionCommand( "3" );
	  b4.setActionCommand( "4" );
	  b5.setActionCommand( "5" );
	  b6.setActionCommand( "6" );
	  b7.setActionCommand( "7" );
	  b8.setActionCommand( "8" );
	  b9.setActionCommand( "9" );


      setLayout( new FlowLayout() );
      add(panel1);
      panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
      add(panel2);
      panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
      add(panel3);
      panel3.setLayout( new BoxLayout( panel3, BoxLayout.Y_AXIS ) );
      add(panel4);
      panel4.setLayout( new BoxLayout( panel4, BoxLayout.Y_AXIS ) );

      //register listener
      b0.addActionListener( this );
      b1.addActionListener( this );
      b2.addActionListener( this );
      b3.addActionListener( this );
      b4.addActionListener( this );
      b5.addActionListener( this );
      b6.addActionListener( this );
      b7.addActionListener( this );
      b8.addActionListener( this );
      b9.addActionListener( this );
     }

 public void actionPerformed( ActionEvent evt)
  {

    // check which command has been sent
    if ( count == 0 & evt.getActionCommand().equals( "3" ))
	 {
	  count =+ 1;
	  getContentPane().setBackground( Color.lightGray);
          repaint();
	 }
    else
	  if ( count == 1 & evt.getActionCommand().equals( "9" ))
	  {
	    count=+1;
	    getContentPane().setBackground(  Color.lightGray );
            repaint();
	  }
      else

        if ( evt.getActionCommand().equals( "3" ) & count == 2)
	  {
           this.dispose();
	  }
        else
           getContentPane().setBackground( Color.red );
           count = 0;
           repaint();
  }


    public static void main ( String[] args )
    {
      Assign3 frame  = new Assign3() ;
      frame.setSize( 275, 130 );
      frame.setVisible( true );
      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      frame.setBackground(  Color.lightGray );
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros