import java.io.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import calculate.calc;
public class cal implements ActionListener
{
JFrame jf;
JToolBar jtb;
JButton jbadd,jbsub,jbmul,jbdiv,jbclear,jbequal;
JTextField jtf;
calc ca2=new calc();
int val;
int flag=0;
cal()
{
frame();
toolbar();
textfield();
button();
jf.setVisible(true);
}
void frame()
{
jf=new JFrame("calculator");
jf.setSize(500,500);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
void toolbar()
{
jtb=new JToolBar();
jf.add(jtb,BorderLayout.NORTH);
}
void textfield()
{
jtf=new JTextField();
jtb.add(jtf);
jtf.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
jtf.setText("");
}
});
}
void button()
{
jbadd=new JButton("+");
jbsub=new JButton("-");
jbmul=new JButton("*");
jbdiv=new JButton("/");
jbclear=new JButton("clear");
jbequal=new JButton("=");
jbadd.addActionListener(this);
jbsub.addActionListener(this);
jbmul.addActionListener(this);
jbdiv.addActionListener(this);
jbclear.addActionListener(this);
jbequal.addActionListener(this);
jtb.add(jbadd);
jtb.add(jbsub);
jtb.add(jbmul);
jtb.add(jbdiv);
jtb.add(jbclear);
jtb.add(jbequal);
}
public void actionPerformed(ActionEvent ae)
{
if(jtf.getText().length()== 0)
return;
val=Integer.parseInt(jtf.getText());
if(ae.getActionCommand().equals(jbadd.getText()))
{jtf.setText(ca2.add(val));
flag=1;
}
else if(ae.getActionCommand().equals(jbsub.getText()))
{jtf.setText(ca2.sub(val));
flag=2;
}
else if(ae.getActionCommand().equals(jbmul.getText()))
{jtf.setText(ca2.mul(val));
flag=3;
}
else if(ae.getActionCommand().equals(jbdiv.getText()))
{jtf.setText(ca2.div(val));
flag=4;
}
else if(ae.getActionCommand().equals(jbclear.getText()))
{
jtf.setText(ca2.clear());
}
else if(ae.getActionCommand().equals(jbequal.getText()))
{
if(flag==0 )
jtf.setText("");
switch(flag)
{
case 1: jtf.setText(ca2.add(val)); break;
case 2: jtf.setText(ca2.sub(val)); break;
case 3 : jtf.setText(ca2.mul(val)); break;
case 4: jtf.setText(ca2.div(val)); break;
}
flag=0;
}
public static void main(String args[])throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new cal();
}
});
}
}
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import calculate.calc;
public class cal implements ActionListener
{
JFrame jf;
JToolBar jtb;
JButton jbadd,jbsub,jbmul,jbdiv,jbclear,jbequal;
JTextField jtf;
calc ca2=new calc();
int val;
int flag=0;
cal()
{
frame();
toolbar();
textfield();
button();
jf.setVisible(true);
}
void frame()
{
jf=new JFrame("calculator");
jf.setSize(500,500);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
void toolbar()
{
jtb=new JToolBar();
jf.add(jtb,BorderLayout.NORTH);
}
void textfield()
{
jtf=new JTextField();
jtb.add(jtf);
jtf.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
jtf.setText("");
}
});
}
void button()
{
jbadd=new JButton("+");
jbsub=new JButton("-");
jbmul=new JButton("*");
jbdiv=new JButton("/");
jbclear=new JButton("clear");
jbequal=new JButton("=");
jbadd.addActionListener(this);
jbsub.addActionListener(this);
jbmul.addActionListener(this);
jbdiv.addActionListener(this);
jbclear.addActionListener(this);
jbequal.addActionListener(this);
jtb.add(jbadd);
jtb.add(jbsub);
jtb.add(jbmul);
jtb.add(jbdiv);
jtb.add(jbclear);
jtb.add(jbequal);
}
public void actionPerformed(ActionEvent ae)
{
if(jtf.getText().length()== 0)
return;
val=Integer.parseInt(jtf.getText());
if(ae.getActionCommand().equals(jbadd.getText()))
{jtf.setText(ca2.add(val));
flag=1;
}
else if(ae.getActionCommand().equals(jbsub.getText()))
{jtf.setText(ca2.sub(val));
flag=2;
}
else if(ae.getActionCommand().equals(jbmul.getText()))
{jtf.setText(ca2.mul(val));
flag=3;
}
else if(ae.getActionCommand().equals(jbdiv.getText()))
{jtf.setText(ca2.div(val));
flag=4;
}
else if(ae.getActionCommand().equals(jbclear.getText()))
{
jtf.setText(ca2.clear());
}
else if(ae.getActionCommand().equals(jbequal.getText()))
{
if(flag==0 )
jtf.setText("");
switch(flag)
{
case 1: jtf.setText(ca2.add(val)); break;
case 2: jtf.setText(ca2.sub(val)); break;
case 3 : jtf.setText(ca2.mul(val)); break;
case 4: jtf.setText(ca2.div(val)); break;
}
flag=0;
}
public static void main(String args[])throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new cal();
}
});
}
}
// store cal in a folder calculator
package calculate;
public class calc
{
public int a,result;
public String s;
public String add(int a)
{
s="";
s=s+result;
result+=a;
s="";
s=s+result;
return s;
}
public String sub(int a)
{
result-=a;
s="";
s=s+result;
return s;
}
public String mul(int a)
{
result*=a;
s="";
s=s+result;
return s;
}
public String div(int a)
{
if(result==0 )
{
result=0;
}
else
result/=a;
s="";
s=s+result;
return s;
}
public String clear()
{
a=0;
result=0;
return s;
}
}
Comments