Scientific Calculator source code developed in java swing
Description : A simple Java Swing Calculator, Example for setBounds in java, Example for actionListener interface. Here i tried to Implement a simple Java Calculator. copy and paste the code to a notepad. compile the program with javac and run using command prompt java calculator
Author +belazy
/* this is a program to make a
calculator
using java program *//* For SNR Sons Lab exam ...
* @author belazy
*/
import java.awt.*;
import java.awt.event.*;
public class
Calculator
extends Frame implements ActionListener{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ex,p1,eq,min,div,mul,cl,back;
TextField t;
int n,i;
int n1,n2;
int ans;
Calculator()
{
this.setLayout(null);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
ex=new Button("exit");
p1=new Button("+");
min=new Button("-");
mul=new Button("X");
div=new Button("/");
eq=new Button("=");
cl=new Button("AC");
back=new Button("clear");
t=new TextField(40);
b0.setBounds(130,190,30,30);
b1.setBounds(100,100,30,30);
b2.setBounds(130,100,30,30);
b3.setBounds(160,100,30,30);
b4.setBounds(100,130,30,30);
b5.setBounds(130,130,30,30);
b6.setBounds(160,130,30,30);
b7.setBounds(100,160,30,30);
b8.setBounds(130,160,30,30);
b9.setBounds(160,160,30,30);
ex.setBounds(100,190,30,30);
p1.setBounds(190,100,30,30);
min.setBounds(190,130,30,30);
mul.setBounds(190,160,30,30);
div.setBounds(190,190,30,30);
eq.setBounds(160,190,30,30);
cl.setBounds(220,100,30,30);
back.setBounds(220,130,30,30);
t.setBounds(100,60,150,30);
this.add(b0);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(b6);
this.add(b7);
this.add(b8);
this.add(b9);
this.add(ex);
this.add(p1);
this.add(mul);
this.add(min);
this.add(div);
this.add(eq);
this.add(cl);
this.add(back);
this.add(t);
b0.addActionListener(this);
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);
ex.addActionListener(this);
p1.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
min.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
back.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==ex)
{
System.exit(0);
}
if(ae.getSource()==p1) // while pressing plus
{
n1=Integer.parseInt(t.getText());
i=1; // this expression is for equals
t.setText("0");
}
if(ae.getSource()==min) // while pressing minus
{
n1=Integer.parseInt(t.getText());
i=2;
t.setText("0");
}
if(ae.getSource()==mul) // while pressing multiplication
{
n1=Integer.parseInt(t.getText());
i=3;
t.setText("0");
}
if(ae.getSource()==div) // while pressing division
{
n1=Integer.parseInt(t.getText());
i=4;
t.setText("0");
}
if(ae.getSource()==cl) // while pressing all clear
{
//n1=Integer.parseInt(t.getText());
//i=4;
t.setText("0");
}
if (ae.getSource()==back) // for back space
{
//if (!(t.getText().equals("0") && t.getText().length() > 1)
t.setText(t.getText().substring(0,t.getText().length()-1));
}
if(ae.getSource()==b0)
{
if(t.getText().equals("0"))
t.setText("0");
else
t.setText(t.getText() +"0");
}
if(ae.getSource()==b1)
{
if(t.getText().equals("0"))
t.setText("1");
else
t.setText(t.getText() +"1");
}
if(ae.getSource()==b2)
{
if(t.getText().equals("0"))
t.setText("2");
else
t.setText(t.getText() +"2");
}
if(ae.getSource()==b3)
{
if(t.getText().equals("0"))
t.setText("3");
else
t.setText(t.getText() +"3");
}
if(ae.getSource()==b4)
{
if(t.getText().equals("0"))
t.setText("4");
else
t.setText(t.getText() +"4");
}
if(ae.getSource()==b5)
{
if(t.getText().equals("0"))
t.setText("5");
else
t.setText(t.getText() +"5");
}
if(ae.getSource()==b6)
{
if(t.getText().equals("0"))
t.setText("6");
else
t.setText(t.getText() +"6");
}
if(ae.getSource()==b7)
{
if(t.getText().equals("0"))
t.setText("7");
else
t.setText(t.getText() +"7");
}
if(ae.getSource()==b8)
{
if(t.getText().equals("0"))
t.setText("8");
else
t.setText(t.getText() +"8");
}
if(ae.getSource()==b9)
{
if(t.getText().equals("0"))
t.setText("9");
else
t.setText(t.getText() +"9");
}
System.out.println(t.getText());
if(ae.getSource()==eq) //while pressing equals
{
n2=Integer.parseInt(t.getText());
switch(i)
{
case 1:
{
ans=n1+n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 2:
{
ans=n1-n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 3:
{
ans=n1*n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 4:
{
ans=n1/n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
}
}
}
public static void main(String free[])
{
Calculator c= new Calculator();
c.setTitle("
simple java calculator source code
");c.setSize(600,600);
c.setVisible(true);
}
}
Hi Friends,
This is my first program in java. so please try it out and find the issues. Thanks to ravi, vishnu , madhu, bithesh and nithin for support
/* contact me at belazy1987atgmail.com */
Most selling calculator in amazon ( Price around 1000) The best one, Buy this , worth buying.
fx-991 Ex ( Casio - the best calculator makers )
Hurry !! Before stock out
Another Program send by my friend,
simple java calculator source code
import javax.swing.*;import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet {
public void init() {
CalculatorPanel calc=new CalculatorPanel();
getContentPane().add(calc);
}
}
class CalculatorPanel extends JPanel implements ActionListener {
JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
static JTextField result=new JTextField("0",45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;
private static void assign(String no)
{
if((result.getText()).equals("0"))
result.setText(no);
else if(lastCommand=="=")
{
result.setText(no);
lastCommand=null;
}
else
result.setText(result.getText()+no);
}
public CalculatorPanel() {
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300,200);
add(result,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4));
n7=new JButton("7");
panel.add(n7);
n7.addActionListener(this);
n8=new JButton("8");
panel.add(n8);
n8.addActionListener(this);
n9=new JButton("9");
panel.add(n9);
n9.addActionListener(this);
div=new JButton("/");
panel.add(div);
div.addActionListener(this);
n4=new JButton("4");
panel.add(n4);
n4.addActionListener(this);
n5=new JButton("5");
panel.add(n5);
n5.addActionListener(this);
n6=new JButton("6");
panel.add(n6);
n6.addActionListener(this);
mul=new JButton("*");
panel.add(mul);
mul.addActionListener(this);
n1=new JButton("1");
panel.add(n1);
n1.addActionListener(this);
n2=new JButton("2");
panel.add(n2);
n2.addActionListener(this);
n3=new JButton("3");
panel.add(n3);
n3.addActionListener(this);
minus=new JButton("-");
panel.add(minus);
minus.addActionListener(this);
dot=new JButton(".");
panel.add(dot);
dot.addActionListener(this);
n0=new JButton("0");
panel.add(n0);
n0.addActionListener(this);
equal=new JButton("=");
panel.add(equal);
equal.addActionListener(this);
plus=new JButton("+");
panel.add(plus);
plus.addActionListener(this);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==n1) assign("1");
else if(ae.getSource()==n2) assign("2");
else if(ae.getSource()==n3) assign("3");
else if(ae.getSource()==n4) assign("4");
else if(ae.getSource()==n5) assign("5");
else if(ae.getSource()==n6) assign("6");
else if(ae.getSource()==n7) assign("7");
else if(ae.getSource()==n8) assign("8");
else if(ae.getSource()==n9) assign("9");
else if(ae.getSource()==n0) assign("0");
else if(ae.getSource()==dot)
{
if(((result.getText()).indexOf("."))==-1)
result.setText(result.getText()+".");
}
else if(ae.getSource()==minus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="-";
result.setText("0");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="/";
result.setText("0");
}
else if(ae.getSource()==equal)
{
secVal=Double.parseDouble(result.getText());
if(lastCommand.equals("/"))
res=preRes/secVal;
else if(lastCommand.equals("*"))
res=preRes*secVal;
else if(lastCommand.equals("-"))
res=preRes-secVal;
else if(lastCommand.equals("+"))
res=preRes+secVal;
result.setText(" "+res);
lastCommand="=";
}
else if(ae.getSource()==mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand="*";
result.setText("0");
}
else if(ae.getSource()==plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="+";
result.setText("0");
}
}
}
The above is an example code for java swing scientific calculator, copy and paste it as calculator.java, compile the code and run it.
Please provide your feedback.
Java applet tutorial
Similar posts
-
calendar using java swing
- compare two images in java
- Developing an swing IDE in java
- Developing an Billing system in java using swing
- Chatting application in java
- Algorithm to find missing number in a sequence with minimum complexity