Help?
I can't get my clear button to work on my calculator applet. Can someone help?
/* This is a Calculator Applet program. It will show the user
* Numbers 0 - 9 and allow the user to add, subtract, multiply and
* the numbers. It also allows the user to use decimals and provides
* the user with a clear button in order to perform more than one calculation*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calcApplet extends JApplet {
//Constant Variables
public final int width = 65;
public final int height = 35;
// Declare variables for the GUI object
static JTextField input; // Input box for users
JButton plus; // Add Button
JButton minus; // Subtract Button
JButton divide; // Divide Button
JButton multiply; // Multiply Button
JButton[] numbers; // Numbers Button(one for each number)
JButton point; // Decimal point
JButton equals; // The result will be calculated when this button is pushed
JButton clear; // This button clears the input field
Dimension size = new Dimension( width, height ); //Size of the buttons
// The 'setup' fuction that will do the work of setting everything up
// This method is also a constuctor
public void calculateApplet() {
Container box = getContentPane();
box.setLayout( new FlowLayout() );
box.setBackground(new java.awt.Color(200, 200, 0));
calculatorAction handle = new calculatorAction();
input = new JTextField( 25 );
input.addActionListener( handle );
box.add( input );
numbers = new JButton[11];
for ( int i = 0; i < 10; i++ ) {
numbers[i] = new JButton( "" + i );
numbers[i].setPreferredSize( size );
numbers[i].setBackground(java.awt.Color.b lue);
numbers[i].setForeground(java.awt.Color.m agenta);
numbers[i].setToolTipText( "" + (i + 1) );
numbers[i].setBorderPainted(false);
numbers[i].addActionListener( handle );
}
box.add( numbers[7] );
box.add( numbers[8] );
box.add( numbers[9] );
divide = new JButton( "/" );
divide.setPreferredSize( size );
divide.setBackground(java.awt.Color.blue) ;
divide.setForeground(java.awt.Color.mage nta);
divide.setToolTipText( "Click here for division" );
divide.setBorderPainted(false);
divide.addActionListener( handle );
box.add( divide );
box.add( numbers[4] );
box.add( numbers[5] );
box.add( numbers[6] );
multiply = new JButton( "*" );
multiply.setPreferredSize( size );
multiply.setBackground(java.awt.Color.bl ue);
multiply.setForeground(java.awt.Color.ma genta);
multiply.setToolTipText( "Click here for multiplication" );
multiply.setBorderPainted(false);
multiply.addActionListener( handle );
box.add( multiply );
box.add( numbers[1] );
box.add( numbers[2] );
box.add( numbers[3] );
minus = new JButton( "-" );
minus.setPreferredSize( size );
minus.setBackground(java.awt.Color.blue) ;
minus.setForeground(java.awt.Color.magen ta);
minus.setToolTipText( "Click here for subtraction" );
minus.setBorderPainted(false);
minus.addActionListener( handle );
box.add( minus );
box.add( numbers[0] );
point = new JButton( "." );
point.setPreferredSize( size );
point.setBackground(java.awt.Color.blue) ;
point.setForeground(java.awt.Color.magen ta);
point.setBorderPainted(false);
point.addActionListener( handle );
box.add( point );
equals = new JButton( "=" );
equals.setPreferredSize( size );
equals.setBackground(java.awt.Color.blue) ;
equals.setForeground(java.awt.Color.mage nta);
equals.setToolTipText( "Answer?" );
equals.setBorderPainted(false);
equals.addActionListener( handle );
box.add( equals );
plus = new JButton( "+" );
plus.setPreferredSize( size );
plus.setBackground(java.awt.Color.blue);
plus.setForeground(java.awt.Color.magent a);
plus.setToolTipText( "Click to add" );
plus.setBorderPainted(false);
plus.addActionListener( handle );
box.add( plus );
clear = new JButton( "C" );
clear.setPreferredSize( size );
clear.setBackground(java.awt.Color.blue) ;
clear.setForeground(java.awt.Color.magen ta);
clear.setToolTipText( "Clear the input Box" );
clear.setBorderPainted(false);
clear.addActionListener( handle );
box.add( clear );
addKeyListener( handle );
setSize( 300, 230 );
setVisible( true );
// From here on it is up to calculator action class to do everything.
} // End calculator constructor
public void init() {
calculateApplet();
} // End init()
public class calculatorAction implements ActionListener, KeyListener {
public double lastNumber = 0;
// flags to tell the last operation
public int lastOp = 0;
public void actionPerformed( ActionEvent e ) {
for ( int i = 0; i < 10; i++ )
{
if ( e.getSource() == numbers[i] )
{
String currText = input.getText();
currText += i;
input.setText( currText );
}
}
if ( e.getSource() == point )
{
String currText = input.getText();
currText += ".";
input.setText( currText );
}
if ( e.getSource() == plus )
{
calc( 1 );
} // End addition
if ( e.getSource() == minus )
{
calc( 2 );
} // End subtraction
if ( e.getSource() == multiply )
{
calc( 3 );
} // End multiply
if ( e.getSource() == divide )
{
calc( 4 );
} // End division
if ( e.getSource() == equals )
{
calc( 0 );
input.setText( "" + lastNumber );
lastNumber = 0;
if ( e.getSource() == clear)
{
String currText = input.getText();
input.setText( " " );
}
} // end equals
} // End actionPerformed
// The method do do the calculating
public void calc( int currOp ) {
switch ( lastOp ) {
case 0: lastNumber = Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 1: lastNumber += Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 2: lastNumber -= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 3: lastNumber *= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 4: lastNumber /= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
} // end case
} // end calc() method
public void keyPressed( KeyEvent e ) {
}
public void keyReleased( KeyEvent e ) {
}
public void keyTyped( KeyEvent e ) {
switch ( e.getKeyChar() ) {
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '0':
case '.': addChar( e.getKeyChar() );
break;
case '=': calc( 0 );
input.setText( "" + lastNumber );
lastNumber = 0;
break;
case '-': calc( 2 );
break;
case '+': calc( 1 );
break;
case '/': calc( 4 );
break;
case '*': calc( 3 );
break;
case 'C': calc( 0 );
break;
} // end switch
} // end keyTyped()
public void addChar( char add ) {
String currText = input.getText();
currText += add;
input.setText( currText );
} // End addChar()
} // End calculatorAction class
} // End calculator class
/* This is a Calculator Applet program. It will show the user
* Numbers 0 - 9 and allow the user to add, subtract, multiply and
* the numbers. It also allows the user to use decimals and provides
* the user with a clear button in order to perform more than one calculation*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calcApplet extends JApplet {
//Constant Variables
public final int width = 65;
public final int height = 35;
// Declare variables for the GUI object
static JTextField input; // Input box for users
JButton plus; // Add Button
JButton minus; // Subtract Button
JButton divide; // Divide Button
JButton multiply; // Multiply Button
JButton[] numbers; // Numbers Button(one for each number)
JButton point; // Decimal point
JButton equals; // The result will be calculated when this button is pushed
JButton clear; // This button clears the input field
Dimension size = new Dimension( width, height ); //Size of the buttons
// The 'setup' fuction that will do the work of setting everything up
// This method is also a constuctor
public void calculateApplet() {
Container box = getContentPane();
box.setLayout( new FlowLayout() );
box.setBackground(new java.awt.Color(200, 200, 0));
calculatorAction handle = new calculatorAction();
input = new JTextField( 25 );
input.addActionListener( handle );
box.add( input );
numbers = new JButton[11];
for ( int i = 0; i < 10; i++ ) {
numbers[i] = new JButton( "" + i );
numbers[i].setPreferredSize( size );
numbers[i].setBackground(java.awt.Color.b
numbers[i].setForeground(java.awt.Color.m
numbers[i].setToolTipText( "" + (i + 1) );
numbers[i].setBorderPainted(false);
numbers[i].addActionListener( handle );
}
box.add( numbers[7] );
box.add( numbers[8] );
box.add( numbers[9] );
divide = new JButton( "/" );
divide.setPreferredSize( size );
divide.setBackground(java.awt.Color.blue)
divide.setForeground(java.awt.Color.mage
divide.setToolTipText( "Click here for division" );
divide.setBorderPainted(false);
divide.addActionListener( handle );
box.add( divide );
box.add( numbers[4] );
box.add( numbers[5] );
box.add( numbers[6] );
multiply = new JButton( "*" );
multiply.setPreferredSize( size );
multiply.setBackground(java.awt.Color.bl
multiply.setForeground(java.awt.Color.ma
multiply.setToolTipText( "Click here for multiplication" );
multiply.setBorderPainted(false);
multiply.addActionListener( handle );
box.add( multiply );
box.add( numbers[1] );
box.add( numbers[2] );
box.add( numbers[3] );
minus = new JButton( "-" );
minus.setPreferredSize( size );
minus.setBackground(java.awt.Color.blue)
minus.setForeground(java.awt.Color.magen
minus.setToolTipText( "Click here for subtraction" );
minus.setBorderPainted(false);
minus.addActionListener( handle );
box.add( minus );
box.add( numbers[0] );
point = new JButton( "." );
point.setPreferredSize( size );
point.setBackground(java.awt.Color.blue)
point.setForeground(java.awt.Color.magen
point.setBorderPainted(false);
point.addActionListener( handle );
box.add( point );
equals = new JButton( "=" );
equals.setPreferredSize( size );
equals.setBackground(java.awt.Color.blue)
equals.setForeground(java.awt.Color.mage
equals.setToolTipText( "Answer?" );
equals.setBorderPainted(false);
equals.addActionListener( handle );
box.add( equals );
plus = new JButton( "+" );
plus.setPreferredSize( size );
plus.setBackground(java.awt.Color.blue);
plus.setForeground(java.awt.Color.magent
plus.setToolTipText( "Click to add" );
plus.setBorderPainted(false);
plus.addActionListener( handle );
box.add( plus );
clear = new JButton( "C" );
clear.setPreferredSize( size );
clear.setBackground(java.awt.Color.blue)
clear.setForeground(java.awt.Color.magen
clear.setToolTipText( "Clear the input Box" );
clear.setBorderPainted(false);
clear.addActionListener( handle );
box.add( clear );
addKeyListener( handle );
setSize( 300, 230 );
setVisible( true );
// From here on it is up to calculator action class to do everything.
} // End calculator constructor
public void init() {
calculateApplet();
} // End init()
public class calculatorAction implements ActionListener, KeyListener {
public double lastNumber = 0;
// flags to tell the last operation
public int lastOp = 0;
public void actionPerformed( ActionEvent e ) {
for ( int i = 0; i < 10; i++ )
{
if ( e.getSource() == numbers[i] )
{
String currText = input.getText();
currText += i;
input.setText( currText );
}
}
if ( e.getSource() == point )
{
String currText = input.getText();
currText += ".";
input.setText( currText );
}
if ( e.getSource() == plus )
{
calc( 1 );
} // End addition
if ( e.getSource() == minus )
{
calc( 2 );
} // End subtraction
if ( e.getSource() == multiply )
{
calc( 3 );
} // End multiply
if ( e.getSource() == divide )
{
calc( 4 );
} // End division
if ( e.getSource() == equals )
{
calc( 0 );
input.setText( "" + lastNumber );
lastNumber = 0;
if ( e.getSource() == clear)
{
String currText = input.getText();
input.setText( " " );
}
} // end equals
} // End actionPerformed
// The method do do the calculating
public void calc( int currOp ) {
switch ( lastOp ) {
case 0: lastNumber = Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 1: lastNumber += Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 2: lastNumber -= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 3: lastNumber *= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
case 4: lastNumber /= Double.parseDouble( input.getText() );
input.setText( "" );
lastOp = currOp;
break;
} // end case
} // end calc() method
public void keyPressed( KeyEvent e ) {
}
public void keyReleased( KeyEvent e ) {
}
public void keyTyped( KeyEvent e ) {
switch ( e.getKeyChar() ) {
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '0':
case '.': addChar( e.getKeyChar() );
break;
case '=': calc( 0 );
input.setText( "" + lastNumber );
lastNumber = 0;
break;
case '-': calc( 2 );
break;
case '+': calc( 1 );
break;
case '/': calc( 4 );
break;
case '*': calc( 3 );
break;
case 'C': calc( 0 );
break;
} // end switch
} // end keyTyped()
public void addChar( char add ) {
String currText = input.getText();
currText += add;
input.setText( currText );
} // End addChar()
} // End calculatorAction class
} // End calculator class
