Pages

Saturday, April 21, 2018

Simple Calculator in Java

Simple Calculator in Java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calc2 extends JFrame implements ActionListener
{
  JPanel p1,p2,p3;
  JTextField txt1;
  double secondno;
   double firstno;
   double num1=0;
   double result;
   String sign;
   double num;
  JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,plus,minus,multi,divide,root,back,pow,equal,c;
  Calc2()
  {
  setSize(350,400);
  setTitle("Calculator2");
  setLayout(new BorderLayout());
 
   p1=new JPanel();
   p1.setPreferredSize(new Dimension(10,30));
   add(p1,BorderLayout.NORTH);
   p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
 
   p1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
    txt1=new JTextField(20);
    txt1.setSize(200,300);
    p1.add(txt1);
   
   
     p2=new JPanel();
   p2.setPreferredSize(new Dimension(200,650));
   add(p2,BorderLayout.WEST);
    p2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
 
    p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
    b1=new JButton("1");
    //b1.setBounds(10,80,50,20);
    b2=new JButton("2");
    b3=new JButton("3");
   
   
    p2.add(b1);
   
    p2.add(b2);
    p2.add(b3);
   
     p2.setLayout(new FlowLayout(FlowLayout.LEFT,20,10));
      b4=new JButton("4");
    //b1.setBounds(10,80,50,20);
    b5=new JButton("5");
    b6=new JButton("6");
    b7=new JButton("7");
    b8=new JButton("8");
    b9=new JButton("9");
    b10=new JButton("0");
    b11=new JButton(".");
    b12=new JButton("00");
   
   
    p2.add(b4);
   
    p2.add(b5);
    p2.add(b6);
    p2.add(b7);
    p2.add(b8);
    p2.add(b9);
    p2.add(b11);
    p2.add(b10);
    p2.add(b12);
 
   
      p3=new JPanel();
   p3.setPreferredSize(new Dimension(130,650));
   add(p3,BorderLayout.EAST);
    p3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
   
   
    plus=new JButton("+");
    minus=new JButton("-");
    multi=new JButton("x");
    divide=new JButton("/");
    root=new JButton("Root");
    back=new JButton("<-");
    pow=new JButton("1/x");
    equal=new JButton("=");
    c=new JButton("C");
   
    p3.add(c);
    p3.add(plus);
    p3.add(minus);
    p3.add(multi);
    p3.add(divide);
    p3.add(root);
    p3.add(back);
    p3.add(pow);
    p3.add(equal);
   
   
   
   
   
 
  setVisible(true);
   b1.addActionListener(this);
   b2.addActionListener(this);
   b3.addActionListener(this);
   b4.addActionListener(this);
   b5.addActionListener(this);
   b6.addActionListener(this);
   b7.addActionListener(this);
   b8.addActionListener(this);
   b9.addActionListener(this);
   b10.addActionListener(this);
   b11.addActionListener(this);
   b12.addActionListener(this);
 
   plus.addActionListener(this);
   minus.addActionListener(this);
   multi.addActionListener(this);
   divide.addActionListener(this);
   root.addActionListener(this);
   back.addActionListener(this);
   pow.addActionListener(this);
   equal.addActionListener(this);
   c.addActionListener(this);
  }
 
  public void actionPerformed(ActionEvent ff)
  {
   // try{
    if (ff.getSource()==b1)
    {
      txt1.setText("");
    txt1.setHorizontalAlignment(txt1.RIGHT);
       String Enternumber=txt1.getText()+b1.getText();
       txt1.setText(Enternumber);
        // JOptionPane.showMessageDialog(this,"Data is Deleted Successfully.");
    }
   // }catch(Exception e){
   // JOptionPane.showMessageDialog(null, e);
    //}
    if(ff.getSource()==b2)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b2.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b3)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b3.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b4)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b4.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b5)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b5.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b6)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b6.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b7)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b7.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b8)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b8.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b9)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b9.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b10)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b10.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b11)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b11.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==b12)
    {
    txt1.setHorizontalAlignment(txt1.RIGHT);
    String num=txt1.getText()+b12.getText();
    txt1.setText(num);
    }
   
    if(ff.getSource()==plus)
    {
     
     // double sum;
     
      sign="+";
      firstno=Double.parseDouble(txt1.getText());
      //num1=firstno;
     // sum=num1+firstno;
      txt1.setHorizontalAlignment(txt1.RIGHT);
     
      txt1.setText("");
     
     
    }
   
    if(ff.getSource()==c)
    {
    txt1.setText("");
    }
    if(ff.getSource()==minus)
    {
      firstno=Double.parseDouble(txt1.getText());
      txt1.setHorizontalAlignment(txt1.RIGHT);
      txt1.setText("");
      sign="-";
    }
    if(ff.getSource()==multi)
    {
      firstno=Double.parseDouble(txt1.getText());
      txt1.setHorizontalAlignment(txt1.RIGHT);
      txt1.setText("");
      sign="*";
    }
    if(ff.getSource()==divide)
    {
      sign="/";
      firstno=Double.parseDouble(txt1.getText());
      txt1.setHorizontalAlignment(txt1.RIGHT);
      txt1.setText("");
     
    }
   
    if(ff.getSource()==equal)
    {
      try{
      secondno=Double.parseDouble(txt1.getText());
     
     
    if (sign.equals("+"))
            result=firstno+secondno;
        if (sign.equals("-"))
            result=firstno-secondno;
        if (sign.equals("/"))
            result=firstno/secondno;
        if (sign.equals("*"))
            result=firstno*secondno;
        txt1.setText(String.valueOf(result));
      }catch(Exception e)
      {
      JOptionPane.showMessageDialog(this,e);
      }
    }
     
   
 
   
  }
 
public static void main(String[] arg)
{
new Calc2();
}
}

Watch in Youtube....
How to make a simple calculator in java using Notepad


No comments:

Post a Comment