Learning Java
Custom Search

Previous Next

Session 13 - Project 02

Part B - Calculator - Add Listener

In this part, we will add the ability for our interface to listen to and respond to input. In order to add the listener, we need to import the event package.

    import java.awt.event.*;

Then we have to implement the ActionListener interface.

    public class Calc extends JApplet implements ActionListener {

We must add the Action Listener on each of the buttons before they are placed on their respective JPanel in the init() method.

    btnClear.addActionListener(this);
    btnClearErr.addActionListener(this);

    btn1.addActionListener(this);
    btn2.addActionListener(this);
    btn3.addActionListener(this);
    btn4.addActionListener(this);
    btn5.addActionListener(this);
    btn6.addActionListener(this);
    btn7.addActionListener(this);
    btn8.addActionListener(this);
    btn9.addActionListener(this);
    btn0.addActionListener(this);
    btnDot.addActionListener(this);
    btnAdd.addActionListener(this);
    btnSubtract.addActionListener(this);
    btnMultiply.addActionListener(this);
    btnDivide.addActionListener(this);
    btnEqual.addActionListener(this);

Then we have to implement the only method that is in the Action Listener Interface. In this method, we test for each of the buttons and perform the necessary tasks. , We need to first pick up the value that is currently in the entry area and convert it to a floating point number, in case the key pressed was a mathematical operation.

    public void actionPerformed(ActionEvent evt) {
        get value in entry area
        if numeric key, accumulate digits
        if math operation, do the math using previous operator
        if clear key, reset values
    }

Other fields we need to define follows. We decided to do this at the top of our Calc class before the init() method, because we want to access them in more than one method.

float currResult = 0f;
float value = 0f;
String operatn = "";

The program code follows:

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

public class Calc extends JApplet implements ActionListener {

    JButton btn1        = new JButton("1");
    JButton btn2        = new JButton("2");
    JButton btn3        = new JButton("3");
    JButton btn4        = new JButton("4");
    JButton btn5        = new JButton("5");
    JButton btn6        = new JButton("6");
    JButton btn7        = new JButton("7");
    JButton btn8        = new JButton("8");
    JButton btn9        = new JButton("9");
    JButton btn0        = new JButton("0");

    JButton btnDot      = new JButton(".");
    JButton btnEqual    = new JButton("=");
    JButton btnAdd      = new JButton("+");
    JButton btnSubtract = new JButton("-");
    JButton btnMultiply = new JButton("*");
    JButton btnDivide   = new JButton("/");
    JButton btnClear    = new JButton("C");
    JButton btnClearErr = new JButton("CE");

    JLabel lblEnter = new JLabel("Enter:"); 
    JTextField txtEnter = new JTextField("", 5);
    JLabel lblResult = new JLabel("Result:");
    JLabel Result = new JLabel("..........");
    float currResult = 0f;
    float value = 0f;
    String operatn = "";

    public void init() {
	try {
	    UIManager.setLookAndFeel (
		UIManager.getSystemLookAndFeelClassName());
	} catch (Exception e) {
		System.err.println("Can't set look and feel: " + e);
	}

        Container panOuter = getContentPane();
        BorderLayout bLay = new BorderLayout();
        panOuter.setLayout(bLay);

	JPanel panNorth = new JPanel();
        GridLayout gLay1 = new GridLayout(2, 2);
        panNorth.setLayout(gLay1);
        panNorth.add(lblResult);
        panNorth.add(Result);
        panNorth.add(lblEnter);
        panNorth.add(txtEnter);
        panOuter.add("North", panNorth);

	JPanel panSouth = new JPanel();
        FlowLayout fLay = new FlowLayout();
        panSouth.setLayout(fLay);
        btnClear.addActionListener(this);
        btnClearErr.addActionListener(this);
        panSouth.add(btnClearErr);
        panSouth.add(btnClear);
        panOuter.add("South", panSouth);

	JPanel panCenter = new JPanel();
        GridLayout gLay2 = new GridLayout(4, 4);
        panCenter.setLayout(gLay2);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btn0.addActionListener(this);
        btnDot.addActionListener(this);
        btnAdd.addActionListener(this);
        btnSubtract.addActionListener(this);
        btnMultiply.addActionListener(this);
        btnDivide.addActionListener(this);
        btnEqual.addActionListener(this);
        panCenter.add(btn1);
        panCenter.add(btn2);
        panCenter.add(btn3);
        panCenter.add(btnAdd);
        panCenter.add(btn4);
        panCenter.add(btn5);
        panCenter.add(btn6);
        panCenter.add(btnSubtract);
        panCenter.add(btn7);
        panCenter.add(btn8);
        panCenter.add(btn9);
        panCenter.add(btnMultiply);
        panCenter.add(btn0);
        panCenter.add(btnDot);
        panCenter.add(btnDivide);
        panCenter.add(btnEqual);
        panOuter.add("Center", panCenter);
        setContentPane(panOuter);
    }

    public void actionPerformed(ActionEvent evt) {
	String txt = txtEnter.getText();

	if (!txt.equals("")) value = Float.parseFloat(txt);

	if (evt.getSource().equals(btn1))  {
	    txtEnter.setText(txtEnter.getText() + "1");
	} else if (evt.getSource().equals(btn2))  {
	    txtEnter.setText(txtEnter.getText() + "2");
	} else if (evt.getSource().equals(btn3))  {
	    txtEnter.setText(txtEnter.getText() + "3");
	} else if (evt.getSource().equals(btn4))  {
	    txtEnter.setText(txtEnter.getText() + "4");
	} else if (evt.getSource().equals(btn5))  {
	    txtEnter.setText(txtEnter.getText() + "5");
	} else if (evt.getSource().equals(btn6))  {
	    txtEnter.setText(txtEnter.getText() + "6");
	} else if (evt.getSource().equals(btn7))  {
	    txtEnter.setText(txtEnter.getText() + "7");
	} else if (evt.getSource().equals(btn8))  {
	    txtEnter.setText(txtEnter.getText() + "8");
	} else if (evt.getSource().equals(btn9))  {
	    txtEnter.setText(txtEnter.getText() + "9");
	} else if (evt.getSource().equals(btn0))  {
	    txtEnter.setText(txtEnter.getText() + "0");
	} else if (evt.getSource().equals(btnDot))  {
	    txtEnter.setText(txtEnter.getText() + ".");
	} else if (evt.getSource().equals(btnAdd))  {
	    doTheMath();
	    operatn = "+";
	} else if (evt.getSource().equals(btnSubtract))  {
	    doTheMath();
	    operatn = "-";
	} else if (evt.getSource().equals(btnMultiply))  {
	    doTheMath();
	    operatn = "*";
	} else if (evt.getSource().equals(btnDivide))  {
	    doTheMath();
	    operatn = "/";
	} else if (evt.getSource().equals(btnEqual))  {
	    doTheMath();
	    operatn = "=";
	} else if (evt.getSource().equals(btnClearErr))  {
	    txtEnter.setText("");
	} else if (evt.getSource().equals(btnClear))  {
	    txtEnter.setText("");
	    Result.setText("0");
	    currResult = 0f;
	    value = 0f;
	    operatn = " ";
	}
    }

    void doTheMath() {
	if (operatn.equals("+")) {
	    currResult = currResult + value;
	} else if (operatn.equals("-")) {
	    currResult = currResult - value;
	} else if (operatn.equals("*")) {
	    currResult = currResult * value;
	} else if (operatn.equals("/")) {
	    currResult = currResult / value;
	} else { currResult = value; 
	}

	Result.setText("" + currResult);
	txtEnter.setText("");
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved