Skip to main content

Calculator in Java

This is One simple calculator using java swing so.. enjoy it....

her all buttons and textbox are put in panel so every row in calculator its separate on panel.
This calculator made by me during 7th semester.

Realy java is my favourite Language . so

Steps for run Below code:-

 1) Copy below code in notepade
 2) Save this file as cal.java
 3) open cmd and goto path of cal.java
 4) Now Firts compile it using below command
        javac cal.java
 5) and run it using following cammand
        java cal.java


Code:
File : cal.java


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

/*
<applet code="cal" width=250 height=300>
</applet>
*/

class calc extends JFrame implements ActionListener  
{  
 FlowLayout fl;
 GridLayout gr;
  JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9;
 JButton b[],plus,eql,minus,mul,div,ce;
 JTextField t1;
 int a,c,r=0;
 String s="";
 public calc()
           {
gr = new GridLayout(8,1);
setLayout(gr);
fl = new FlowLayout(0); 
p1 = new JPanel();
                        p1.setLayout(fl);
t1 = new JTextField(15);
p1.add(t1); 
add(p1);
b = new JButton[10];
for(int i=0;i<10;i++)
{
b[i] = new JButton(    String.valueOf(i)    );
  }
      p3 = new JPanel();
                                p3.setLayout(fl);
plus = new JButton("+");
plus.addActionListener(this);
p3.add(plus);
ce = new JButton("      CE        ");
ce.addActionListener(this);
p3.add(ce);
add(p3);
p4 = new JPanel();
               p4.setLayout(fl);
mul = new JButton("* ");
mul.addActionListener(this);
p4.add(mul); 
div = new JButton("/ ");
div.addActionListener(this);
p4.add(div);
minus = new JButton(" -");
minus.addActionListener(this);
p4.add(minus);
add(p4);
p5 = new JPanel();
                                p5.setLayout(fl);
p5.add(  b[7]  );
b[7].addActionListener(this);
p5.add(  b[8]  );
b[8].addActionListener(this);
p5.add(  b[9]  );
b[9].addActionListener(this);
add(p5);
p6 = new JPanel();
                                p6.setLayout(fl);
p6.add(b[4]);
b[4].addActionListener(this);
p6.add(b[5]);
b[5].addActionListener(this);
p6.add(b[6]);
b[6].addActionListener(this); 
add(p6);
p7 = new JPanel();
                                p7.setLayout(fl);
p7.add(b[1]);
b[1].addActionListener(this);
p7.add(b[2]);
b[2].addActionListener(this);
p7.add(b[3]);
b[3].addActionListener(this); 
add(p7);
p8 = new JPanel();
                        p8.setLayout(fl);
p8.add(b[0]); 
b[0].addActionListener(this);
eql = new JButton("        =         ");
eql.addActionListener(this);
p8.add(eql);
add(p8);
setTitle("O@K Tech. RVS");
setLocation(100,100);
setSize(200,300);
               setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
public void actionPerformed(ActionEvent e)
{
for(int i=0; i<b.length; i++)
{
if(e.getSource() == b[i])
{
switch(i)
{
case 0:
s=s+"0";
break;
case 1:
s=s+"1";
break;
case 2:
s=s+"2";
break;
case 3:
s=s+"3";
break;
case 4:
s=s+"4";
break;
case 5:
s=s+"5";
break;
case 6:
s=s+"6";
break;
case 7:
s=s+"7";
break;
case 8:
s=s+"8";
break;
case 9:
s="9";
break;
}
t1.setText(s);
}
}
if(e.getSource() == plus)
{
a=Integer.parseInt(s);
s="";
t1.setText("");
r=1;
}
else if(e.getSource() == minus)
{
a=Integer.parseInt(s);
s="";
t1.setText("");
r=2;
}
else if(e.getSource() == mul)
{
a=Integer.parseInt(s);
s="";
t1.setText("");
r=3;
}
else if(e.getSource() == div)
{
a=Integer.parseInt(s);
s="";
t1.setText("");
r=4;
}
else if(e.getSource() == ce)
{
s="";
t1.setText("");
}
else if(e.getSource() == eql)
{
switch(r)
{
       case 1:
c = a + Integer.parseInt(t1.getText());
break;
      case 2:
c = a - Integer.parseInt(t1.getText());
break;
      case 3:
c = a * Integer.parseInt(t1.getText());
break;
      case 4:
c = a / Integer.parseInt(t1.getText());
break;  
}
t1.setText(""+c);
}
}
}
public class cal
{
public static void main(String args[])
{
calc cl = new calc();
}
}


Output of Program:



Comments

Popular posts from this blog

Login Application using Database with dialog demo

Follow Below Steps for make simple login applicaiton in Android using database with Dialog demo. 1)DataBaseDemoActivity.java This file diplay login page and trasfer two edit text data into second activity........  package database.co.in; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DataBaseDemoActivity extends Activity implements OnClickListener {     /** Called when the activity is first created. */     EditText mtxt_email, mtxt_password;     Button mLogin; @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mtxt_email = (EditText)findViewById(R.id.editEmail_id);   ...

Audio Recording in Android

Simple State Diagram for Media Recorder... Here Sequence of Method call is as per state diagram of media Recorder .... Otherwise Invalid sate call Error will be Occur........ Steps: 1) Also her two permission add in Android manifest file <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2) This is java file coding  audiorecording.java package com.example.recording; import java.io.File; import java.io.FileDescriptor; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import ...

Android Activity

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with  setContentView(View) . While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with  windowIsFloating  set) or embedded inside of another activity (using  ActivityGroup ). There are two methods almost all subclasses of Activity will implement: onCreate(Bundle)  is where you initialize your activity. Most importantly, here you will usually call  setContentView(int)  with a layout resource defining your UI, and using  findViewById(int)  to retrieve the widgets in that UI that you need to interact with programmatically. onPause()  is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point...