Sunday, 19 November 2017

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


5 comments:

  1. Initial You got a awesome blog .I determination be involved in plus uniform minutes. i view you got truly very functional matters , i determination be always checking your blog blesss. 25 c to f

    ReplyDelete
  2. This blog is literally what I was searching for.
    mortgage calculator

    ReplyDelete
  3. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!! online antiderivative calculator

    ReplyDelete
  4. A loan calculator is a tool to figure out the term, costs and monthly payments on a certain loan, based on a number of variables. The various results of the loan formula, which change based on your treatment of the variables, must then be considered in light of your budget as a whole. It is all very well to work out a $700 mortgage based on a $40,000 down payment, but you are getting a little ahead of yourself if you don't have the $40,000 yet. hours calculator payroll

    ReplyDelete
  5. A home loan calculator is able to calculate monthly mortgage payments. All you have to do is input the length and total amount of your mortgage, along with the starting date, interest rate and the program will give you a monthly payment figure. calculator hours and minutes

    ReplyDelete