Welcome to Dream.In.Code
Become a Java Expert!

Join 150,058 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,732 people online right now. Registration is fast and FREE... Join Now!




Clear all button not working

 
Reply to this topicStart new topic

Clear all button not working, Click twice before it works

jakkels
12 Jun, 2008 - 12:11 PM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions

Can someone look at problem in bold, i need to click the button twice to get it to clear. Why?

CODE


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


//=============
// Calculator
//=============

//Declaring that we are interested in using the action listener
//Extends frame - inherits
public class Calculator extends Frame implements ActionListener, WindowListener
{
    private TextField display;
    private TextField status;
    private Engine engine = new Engine();

    public static void main( String[] args )
    {
        Frame f = new Calculator();
        f.setSize( 200, 300 );
        f.setLocation( 300, 150 );
        f.setTitle( "Calculator" );
        f.show();
    }
  
    public Calculator()
    {
        addWindowListener( this );
      
      // Adding the display for the calculator
      display = new TextField( "" );
        display.setEditable( false );    
      add( display, "North" );
              
      //New panel specifying grid for all the numbers and functions
        Panel p = new Panel();
      p.setLayout( new GridLayout( 5, 4, 5, 4 ) );
      //p.setVgap ( 1, 1 ); THIS IS NOT WORKING

      //Adding the buttons
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      addButton( p, "CE" );
      addButton( p, "CA" );
      addButton ( p, "7" );
      addButton ( p, "8" );
      addButton ( p, "9" );
      addButton ( p, "/" );
      addButton( p, "M+" );
      addButton ( p, "4" );
      addButton ( p, "5" );
      addButton ( p, "6" );
      addButton ( p, "*" );
      addButton ( p, "MC" );
      addButton ( p, "1" );      
      addButton ( p, "2" );
      addButton ( p, "3" );
        addButton( p, "-" );
      addButton( p, "MRC" );
      addButton ( p, "0" );
      addButton ( p, "%" );
        addButton( p, "." );
        addButton( p, "+" );
        addButton( p, "=" );

        add( p, "Center" );
      
      //this is to add a textfield panel at the bottom
        status = new TextField();
        status.setEditable( false );
        status.setText("Welcome to my calculator");
        add( status, "South" );
   }

    public void addButton( Container c, String s )
    {
        Button b = new Button( s );
        c.add( b );
        b.addActionListener( this );
    }

    public void actionPerformed( ActionEvent evt )
       //Handling graphic user interface events
      {
        String s = evt.getActionCommand();
        int result;

//To try and limit the length of the textfield to 8      
//      if ( engine.started() )
//      display.setText( s );
//    else {
//          String str = display.getText();
//          if(str.length() < 8)
//                 display.setText( str + s );
//          }
//        engine.stop();
//        }

           if ( '0' <= s.charAt( 0 ) && s.charAt( 0 ) <= '9' )
           {
           
         if ( engine.started() )
                   display.setText( s );
               else
                   display.setText( display.getText() + s );
               engine.stop();
           }
        
         else
           {
               result = engine.calculate( Integer.parseInt( display.getText() ) );
               display.setText( "" + result );
               engine.operation( s );
               engine.start();
           }
[b]            
         if (s == " CA ")
         {
            display.setText( "0" );
            }
[/b]     
         //else if
         //{
         //(s == " M+ ")
         //{
         //  
         //}
         //
         //}
      }  

    public void windowActivated(WindowEvent e)
    {
        return;
    }

    public void windowClosed(WindowEvent e)
    {
        return;
    }

    //Close the window using windows listener to close the calculator
   public void windowClosing(WindowEvent e)
    {
        dispose();
    }

    public void windowDeactivated(WindowEvent e)
    {
        return;
    }

    public void windowDeiconified(WindowEvent e)
    {
        return;
    }

    public void windowIconified(WindowEvent e)
    {
        return;
    }

    public void windowOpened(WindowEvent e)
    {
        return;
    }
}




Engine class too if you want to compile.

CODE



public class Engine
{
    private int acc = 0;
    private String op = "=";
    public boolean start = true;

    public Engine()
    {
    }

    public void start()
    {
        start = true;
    }

    public void stop()
    {
        start = false;
    }

    public boolean started()
    {
        return start;
    }

    public void operation( String op )
    {
        this.op = op;
    }

    public int calculate( int n )
    {
        if ( op.equals( "+" ) )
            acc += n;
        else if ( op.equals( "-" ) )
            acc -= n;
        else if ( op.equals( "*" ) )
            acc *= n;
        else if ( op.equals( "/" ) )
            acc /= n;
        else if ( op.equals( "%" ) )
            acc %= n;
        else if ( op.equals( "=" ) )
            acc = n;
      else if ( op.equals( "CE" ) )
         acc = 0;
      else if ( op.equals( "CA" ) )
          acc = 0;
      //else if ( op.equals( "M+" ) )
      //   acc = n;
      //else if ( op.equals( "MC" ) )
      //   acc = n;
      //else if ( op.equals( "MRC" ) )
      //   acc = n;

        return acc;
    }
}


User is offlineProfile CardPM
+Quote Post

pbl
RE: Clear All Button Not Working
12 Jun, 2008 - 02:58 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Chances that

if(s == " CA ")

are very very very low, put a println if the if to check. You should do

if(s.equals(" SA ")

is the correct way to proceed

User is online!Profile CardPM
+Quote Post

jakkels
RE: Clear All Button Not Working
12 Jun, 2008 - 11:22 PM
Post #3

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
I admit I dn't know what I'm doing....this is what I tried but it's not right.

CODE


         if (s.equals( "CA" ) )
         {
            if ( engine.started() )
              
               display.setText ( "0" );
               else
                  status.setText( "All Cleared" );
                  engine.stop();
         }



User is offlineProfile CardPM
+Quote Post

mensahero
RE: Clear All Button Not Working
12 Jun, 2008 - 11:30 PM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
Hello bro.. I didn't test your code or look into it.. but while googling.. I bump into this thread about double click.. LInk below..

Double Click Me.. Nah Once is Enough..


blink.gif IDk if you'll find it helpful.. I just thought it would.. blink.gif

This post has been edited by mensahero: 12 Jun, 2008 - 11:31 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month