Implementation of actionPerformed method for Calculator Application


Implementing the actionPerformed() Method


The actionPerformed() method is called. When the button on the calculator interface is pressed. The actionPressed() method is a built-in listener method of ActionListener class that is implemented by Calculator_app class for trapping the button event.

Declaration of the method actionPerformed()


public void actionPerformed(ActionEvent e)//  Action listener method for  buttons on the calculator.
{
if (e.getSource()==button[29])//For begin store in the file. // " StartStoring" button
{
cond4=true;
button[30].setEnabled(true);// for //button with label StartStoring "EndStoring " Button
button[29].setEnabled(false);
f.CreateFile();// Invokes the method for creating a new file.
}
else if (e.getSource()==button[30])//End of the storing.
{
cond4=false;
button[29].setEnabled(true);
button[30].setEnabled(false);
f.endStore();// Call method  for stop storing the file.
}
else if (e.getSource() == button[41]) //Preview button.
{
button[41].setEnabled(false);
button[42].setEnabled(true);
f.WriteFileOnTextArea();
}
else if (e.getSource()==button[42]) // "Close" Button
{
button[41].setEnabled(true);
button[42].setEnabled(false);
textarea1.setText("Developedn byn Amann" );
}
else if (e.getSource()==button[32]) // "Del" Button
//Del button.
{
f.CreateFile();
}
else if (e.getSource()==button[1])  //For advanced buttons.
{
if(!cond)
{
cond=true;
pa1.setVisible(true);
button[1].setLabel("Simple");
for(int i=20;i<29;i++)
button[i].setEnabled(true);
}
else
{
button[1].setLabel("Advanced" );
pa1.setVisible(false);
cond=false;
}
}

else if(e.getSource()==button[20])
{
pressed=true;
double x=Double.valueOf(str).doubleValue();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while(x>=360)
x=x-360;
str=""+Math_class.sin_func(Math_class.convert_deg_rad(x));
textf.setText(""+str);
}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.sin_func(x);
textf.setText(str);
}
f.CreateFile();
f.addTofile("Sin("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.asin(x));
textf.setText(""+str);

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_deg_rad(Math.acos(x));
textf.setText(str);

}
str3=str;
f.CreateFile();
f.addTofile("aSin("+x+")= "+str);
}
}
else if(e.getSource()==button[21])
{
pressed=true;
double x=Double.valueOf(str). doubleValue ();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while (x>=360)
x=x-360;
str=""+Math_class.cos_func(Math_class.convert_deg_rad(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.cos_func(x);
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("Cos("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("aCos("+x+") ="+str);
}
}

else if (e.getSource()==button[0])//Power button.
{
if(!cond1)
{
cond5=true;
textf.setBackground (Color.white);
cond1=true;
button[0].setLabel("OFF");
for(int i=1;i<42;i++)
button[i].setEnabled (true);
button[30].setEnabled(false);
}
else
{
cond1=false;
cond5=true;
pressed=false;
textf.setBackground (Color.black);
button[0].setLabel("ON");
for(int i=1;i<43;i++)
button[i].setEnabled (false);
memoryTagLabel.setText(" ");
f.delFile();
str="0";str1="";str2="";
textf.setText(str);
mem=0;

}
}
else if(e.getSource()== button[16]) // "." Button
{
if(pressed)
{
str="0";
pressed=false;
}
if (cond5)
{
str=str+".";
cond5=false;
textf.setText(str);
}
}
//The implementation of the Number Buttons.
else if ((e.getSource()==button[6]) || (e.getSource()==button[7]) || (e.getSource()==button[8]) || (e.getSource()==button[9]) || (e.getSource()==button[10]) || (e.getSource()==button[11]) || (e.getSource()==button[12]) || (e.getSource()==button[13]) || (e.getSource()==button[14]) ||(e.getSource()==button[15]) )
{
if(pressed)
{
str="0";
pressed=false;
}
if(str.compareTo("0")==0||op!="")
str=e.getActionCommand();
else str=str+e.getActionCommand();
str3=str;
textf.setText(str);
showStatus(e.getActionCommand());
}
else if ((e.getSource()==button[37])|| (e.getSource()==button[35])||(e.getSource()==button[33])||(e.getSource()==button[31])||(e.getSource()==button[25]))
{ // "+" "+" "-" "*" "/" Buttons
cond5=true;
String tempStr="";
op=e.getActionCommand();
str1=str;
str3=str;
tempStr = str;
str="0";
textf.setText(tempStr);
}
else if ((e.getSource()==button[18])) // "=" Button
{ calculate();
//f.CreateFile();
}
else if (e.getSource()==button[5]) //  "BackSpace" Button
{
if(str.length() != 0)
{
str=str.substring(0,str.length()-1);
textf.setText(str);
}
else
JOptionPane.showMessageDialog(null, "Do not use backspace now.");
}
else if (e.getSource()==button[3]) // "C" Button
{
op="";
str=str3;
textf.setText(str);
}
else if (e.getSource()==button[4]) //"CE" Button
{
textf.setText("0");
cond5=true;
str="0";
str1="";
str2="";
}
else if (e.getSource()==button[27]) //  "1/X" button
{
pressed=true;
float g=0,p=0;
p=Float.valueOf(str).floatValue();
if(p==0)
{
str="0";
textf.setText("Infinite");
}
else
{
g=(1/p);
str=""+g;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("1/"+p+"="+str);
}
}
else if (e.getSource()==button[28]) // "+/-" Button
{
if (!(str.compareTo("0")==0))
{
if(str.charAt(0)=='-')
str=str.substring(1,str.length());
else
str="-"+str;
textf.setText (str);
}
}
else if(e.getSource()==button[34]) // "Me" button
{
pressed=true;
memoryTagLabel.setText("M");
mem = Double.valueOf(str).doubleValue();
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[36]) //"M+" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem+(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[38]) //"M-" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem-(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);

}
else if(e.getSource()==button[39]) //"MR" Button
{
str=""+mem;
str3=str;
textf.setText(str3);
}

else if(e.getSource()==button[23])
{
pressed=true;
double y=0;
double x=Double.valueOf(str).doubleValue();
y=Math_class.sqr_func(x);
textf.setText(""+y);
str=""+y;
str3=str;
f.CreateFile();
f.addTofile("sqr("+x+")="+str);
}
else if(e.getSource()==button[24])
{
pressed=true;
int x=Integer.valueOf (str).intValue();
if(x>39)
textf.setText("Infinite");
else
{
str=""+Math_class.fact_func(x);
textf.setText(""+str);
str3=str;
f.CreateFile();
f.addTofile(""+x+"!="+str);
}

}
else if(e.getSource()==button[2]) //"Shift" Button
{
if(!cond3)
{
button[20].setLabel("arcSin");
button[21].setLabel("arcCos");
cond3=true;
}
else
{
cond3=false;
button[20].setLabel ("Sin");
button[21].setLabel ("Cos");
}
}
else if(e.getSource()==button[40]) //" R" Button
{
textf.setText("0");
mem=0;
memoryTagLabel.setText("");
}
else if(e.getSource()==button[26])
{
pressed=true;
float p,r;
p=Float.valueOf(str).floatValue();
r=p/100;
str=""+r;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile(""+p+"%="+str);
}
else if(e.getSource()==button[22])
{
pressed=true;
double l=0;
m=Double.valueOf(str).doubleValue();
l=Math.sqrt(m);
str=""+l;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("sqrt("+m+") ="+str);
}
else if (e.getSource()==button[19])
{
pressed=true;
textf.setText(str3);
str=str3;
showStatus(str3);
}
else if (e.getSource()==button[17])
{
pressed=true;
textf.setText(""+3.1415926);
str=""+3.1415926;
}

}//Method ends action performed.

The above listing shows the declaration of the actionPerformed() method. The actionPerformed() method is invoked if a button is clicked on the calculator interface. The CreateFile() method is invoked if the player clicks on the StartStoring button. The WriteFileOnTextArea() method is invoked if the player clicks on the preview button.

0 Comments:

Post a Comment