Program to create a simple Calculator using Java AWT components

This tutorial explains you how to create a simple Calculator using Java AWT components. The output of the program is given below. 



















PROGRAM
import java.awt.*;
import java.awt.event.*;

class MyCalculator extends Frame implements ActionListener {
 
 TextField tfInput;
 Panel panel;
 
 String btnString[] = {"7", "8", "9", "+",
                 "4", "5", "6", "-",
                 "1", "2", "3", "*",
                 "C", "0", "=", "/"};
Button btn[] = new Button[16];   
 int num1 = 0, num2 = 0, result = 0;
 char op;
 
 public MyCalculator() {
 
  Font f = new Font("Cambria", Font.BOLD, 18);
  
  tfInput = new TextField(10);
  tfInput.setFont(f);
  
  panel = new Panel();
  
  add(tfInput, "North");
  add(panel, "Center");
  
  panel.setLayout(new GridLayout(4,4));
  
  for(int i=0; i < 16; i++) {
   
   btn[i] = new Button(btnString[i]);
   btn[i].setFont(f);
   btn[i].addActionListener(this);
   panel.add(btn[i]);
  }
  
  addWindowListener(new WindowAdapter(){
   
   public void windowClosing(WindowEvent we) {
    System.exit(0);
   }
  });
 }
 
 public void actionPerformed(ActionEvent ae) {
  
  String str = ae.getActionCommand();
  
  if(str.equals("+")) {
   
   op = '+';
   num1 = Integer.parseInt(tfInput.getText());
   tfInput.setText("");
  }
  else if(str.equals("-")) {
   op = '-';
   num1 = Integer.parseInt(tfInput.getText());
   tfInput.setText("");
  }
  else if(str.equals("*")) {
   op = '*';
   num1 = Integer.parseInt(tfInput.getText());
   tfInput.setText("");
  }
  else if(str.equals("/")) {
   op = '/';
   num1 = Integer.parseInt(tfInput.getText());
   tfInput.setText("");
  }
  else if(str.equals("=")) {
   
   num2 = Integer.parseInt(tfInput.getText());
   
   switch(op) {
    
    case '+' : result = num1 + num2;
     break;
    case '-' : result = num1 - num2;
     break;
    case '*' : result = num1 * num2;
     break;
    case '/' : result = num1 / num2;
     break;
   }
   tfInput.setText(result + "");
   result = 0;
  }
  else if(str.equals("C")) {
   
   tfInput.setText("");
   num1 = num2 = result = 0;
  }
  else {
   tfInput.setText(tfInput.getText() + str);
  }
 }
 
 public static void main(String args[]) {
  
  MyCalculator m = new MyCalculator();
  m.setTitle("My Calculator");
  m.setSize(250,300);
  m.setVisible(true);
 }
}


Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.