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;
}
}