/**
* @(#)Calculater.java
*
*
* @author - Anant
* @version 1.1 2013/8/11
*/
package cal1_1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculater
{
public static void main(String args[])
{
myframe m=new myframe();
m.setTitle("Calculater Version - 1.1");
m.setSize(500,150);
m.setVisible(true);
m.setLocation(150,150);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class myframe extends JFrame
{
mypanel p;
myframe()
{
p=new mypanel();
Container c=getContentPane();
c.add(p);
}
class mypanel extends JPanel implements ActionListener
{
JTextField txt_show;
JButton btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_add,btn_sub,btn_mul,btn_div,btn_eql;
String str_get,str_set,str_final,Str_null="";
int load_add,load_sub,load_mul,load_div,getval;
int i=0;
mypanel()
{
setLayout(null);
setTitle("Calculater");
setSize(500,150);
setLocation(150,150);
setResizable(false);
txt_show=new JTextField();
txt_show.setBounds(0,0,500,30);
add(txt_show);
btn_add=new JButton("+");
btn_add.setBounds(0,30,100,30);
btn_add.addActionListener(this);
add(btn_add);
btn_sub=new JButton("-");
btn_sub.setBounds(100,30,100,30);
btn_sub.addActionListener(this);
add(btn_sub);
btn_mul=new JButton("*");
btn_mul.setBounds(200,30,100,30);
btn_mul.addActionListener(this);
add(btn_mul);
btn_div=new JButton("/");
btn_div.setBounds(300,30,100,30);
btn_div.addActionListener(this);
add(btn_div);
btn_eql=new JButton("=");
btn_eql.setBounds(400,30,100,30);
btn_eql.addActionListener(this);
add(btn_eql);
btn_1=new JButton("1");
btn_1.setBounds(0,60,100,30);
btn_1.addActionListener(this);
add(btn_1);
btn_2=new JButton("2");
btn_2.setBounds(100,60,100,30);
btn_2.addActionListener(this);
add(btn_2);
btn_3=new JButton("3");
btn_3.setBounds(200,60,100,30);
btn_3.addActionListener(this);
add(btn_3);
btn_4=new JButton("4");
btn_4.setBounds(300,60,100,30);
btn_4.addActionListener(this);
add(btn_4);
btn_5=new JButton("5");
btn_5.setBounds(400,60,100,30);
btn_5.addActionListener(this);
add(btn_5);
btn_6=new JButton("6");
btn_6.setBounds(0,90,100,30);
btn_6.addActionListener(this);
add(btn_6);
btn_7=new JButton("7");
btn_7.setBounds(100,90,100,30);
btn_7.addActionListener(this);
add(btn_7);
btn_8=new JButton("8");
btn_8.setBounds(200,90,100,30);
btn_8.addActionListener(this);
add(btn_8);
btn_9=new JButton("9");
btn_9.setBounds(300,90,100,30);
btn_9.addActionListener(this);
add(btn_9);
btn_0=new JButton("0");
btn_0.setBounds(400,90,100,30);
btn_0.addActionListener(this);
add(btn_0);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_0)
{
str_get=txt_show.getText();
str_set="0";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_1)
{
str_get=txt_show.getText();
str_set="1";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_2)
{
str_get=txt_show.getText();
str_set="2";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_3)
{
str_get=txt_show.getText();
str_set="3";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_4)
{
str_get=txt_show.getText();
str_set="4";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_5)
{
str_get=txt_show.getText();
str_set="5";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_6)
{
str_get=txt_show.getText();
str_set="6";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_7)
{
str_get=txt_show.getText();
str_set="7";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_8)
{
str_get=txt_show.getText();
str_set="8";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_9)
{
str_get=txt_show.getText();
str_set="9";
str_final=str_get.concat(str_set);
txt_show.setText(str_final);
}
else if(ae.getSource()==btn_add)
{
load_add=Integer.parseInt(txt_show.getText());
txt_show.setText(Str_null);
i=1;
}
else if(ae.getSource()==btn_sub)
{
load_sub=Integer.parseInt(txt_show.getText());
txt_show.setText(Str_null);
i=2;
}
else if(ae.getSource()==btn_mul)
{
load_mul=Integer.parseInt(txt_show.getText());
txt_show.setText(Str_null);
i=3;
}
else if(ae.getSource()==btn_div)
{
load_div=Integer.parseInt(txt_show.getText());
txt_show.setText(Str_null);
i=4;
}
else if(ae.getSource()==btn_eql)
{
String str1_get=txt_show.getText();
if(i==1)
{
int load_add1=Integer.parseInt(txt_show.getText());
int add=load_add+load_add1;
txt_show.setText(""+add);
}
else if(i==2)
{
int load_sub1=Integer.parseInt(txt_show.getText());
int sub=load_sub-load_sub1;
txt_show.setText(""+sub);
}
else if(i==3)
{
int load_mul1=Integer.parseInt(txt_show.getText());
int mul=load_mul*load_mul1;
txt_show.setText(""+mul);
}
else if(i==4)
{
int load_div1=Integer.parseInt(txt_show.getText());
int div=load_div/load_div1;
txt_show.setText(""+div);
}
else if(str1_get.equals(""))
{
new mydialong().setVisible(true);
}
}
}
}
}
class mydialong extends JDialog implements ActionListener
{
myframe mf;
JButton btn_yes;
JLabel lbl_msg;
Panel north_panel,south_panel;
mydialong()
{
super(new myframe(),"Error Massage",true);
setSize(250,150);
setLocation(100,100);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
north_panel=new Panel();
south_panel=new Panel();
setLayout(new FlowLayout());
lbl_msg=new JLabel("Field is Empty ... Enter some value ...");
north_panel.add(lbl_msg);
btn_yes=new JButton("Yes");
btn_yes.addActionListener(this);
south_panel.add(btn_yes);
add(north_panel,"North");
add(south_panel,"South");
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_yes)
{
dispose();
}
}
}