📄 calculator_app.java
字号:
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
import java.math.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
/*
<applet code=Calculator_app.class width =500 height=400>
</applet>
*/
public class Calculator_app extends JApplet implements KeyListener, ActionListener
{
private File_class f=new File_class();// The File_class class is an inner class. The File_class class consists of methods used to work with files.
private ButtonGroup degree_cont;//To group the two checkbox options.
private JTextArea textarea1;
private JLabel label1,memoryTagLabel;
private Button_class[] button;// Declaring variable of Button_class.
private JTextField textf;// Declaring the text field to the input given.
private String str="0",str1="",str2="",op="", str3="";// Declaring the 5 string variable.
private double m=0,n=0,mem=0;
private JPanel pa1,pa2,pa3,pa4,pa5,pa6,pa7, pa8;// Declaring the eight variable.
// Declaring the label for the button.
private String[] Strbutton={"Power" , "Advanced" ,"Shift","C","CE", "BackSpace" ,"7", "8","9","4","5","6","1","2","3","0",".","Pie", "=","Ans","Sin","Cos","Sqrt","Sqr","X!","Pow" ,"%","1/X","+/-","StartStoring","EndStoring" ,"/", "Del","*","Me","-","M+","+","M-","MR","R","Preview","Close"};
private boolean cond=false,cond1=false, cond2=false,cond3=false,cond4=false,cond5=true,pressed=false;
public void init()// Init method is used to initialise the values.
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(0,7));
// Setting the layout as borderlayout.
// contentPane.setBackground(Color.yellow);
label1=new JLabel(" CALCULATOR ");
memoryTagLabel=new JLabel(" ");
memoryTagLabel.setEnabled(true);
label1.setFont(new Font("Times Roman",Font.BOLD,20));
memoryTagLabel.setFont(new Font("Times Roman",Font.BOLD,17));
memoryTagLabel.setForeground(Color.black);
label1.setForeground(Color.blue);
textarea1=new JTextArea("Developed\n By\n Aman ",4,11);
textarea1.setFont(new Font("Times Roman",Font.BOLD,12));
textarea1.setEditable(false);
boolean wrap = true;
textarea1.setLineWrap(wrap);
degree_cont = new ButtonGroup();//The container of the checkboxes.
JRadioButton degrees = new JRadioButton("Deg",true);
degrees.setActionCommand("Deg");
//degrees.addItemListener(this);
JRadioButton radians = new JRadioButton("Rad",false);
radians.setActionCommand("Rad");
//radians.addItemListener(this);
degree_cont.add(degrees);
degree_cont.add(radians);
button =new Button_class[43];
for(int i=0;i<20;i++)//Creating the Advanced buttons such as, sin & cos & arcsin.
button[i]=new Button_class (Strbutton[i],Color.black);
// new Color(55,168,200)
for(int i=20;i<43;i++)//Creating the numbers.
button[i]=new Button_class (Strbutton[i],Color.black);
textf=new JTextField(22);
textf.setHorizontalAlignment(JTextField.RIGHT );
textf.setFont(new Font("Times Roman", Font.BOLD,12));
textf.addKeyListener(this);
textf.setText("0");
//Constructs panels in the applet.
pa1=new JPanel();
pa2=new JPanel();
pa3=new JPanel();
pa4=new JPanel();
pa5=new JPanel();
pa6=new JPanel();
pa7=new JPanel();
pa8=new JPanel();
pa5.setBounds(20,1,275,30);
pa5.add(button[0]);
pa5.add(label1);
contentPane.add(pa5);
pa3.setLayout(new GridLayout(6,3,2,3));
pa3.setBounds(20,120,225,156);
for (int i=3;i<19;i++)
pa3.add(button[i]);
contentPane.add(pa3);
pa1.setLayout(new GridLayout(3,3,2,2));
pa1.setBounds(20,276,225,78);
for(int i=20;i<29;i++)
pa1.add(button[i]);
contentPane.add(pa1);
pa2.setBounds(20,32,300,90);
pa2.add(textf);
pa2.add(memoryTagLabel);
pa2.add(pa4);
pa4.setBounds(40,50,260,80);
pa4.add(button[1]);
pa4.add(button[2]);
pa4.add(degrees);
pa4.add(radians);
textf.setBackground(Color.black);
contentPane.add(pa2);
pa7.setBounds(320,1,150,182);
pa7.add(textarea1);
pa7.add(button[41]);
pa7.add(button[42]);
contentPane.add(pa7);
pa6.setLayout(new GridLayout(6,2,2,2));
pa6.setBounds(280,196,200,156);
pa8.setBounds(0,0,400,40);
for(int i=29;i<41;i++)
pa6.add(button[i]);
contentPane.add(pa6);
contentPane.add(pa8);
//Before you can use the Calculator you have to press the power button on the calculator interface to enable buttons on the calculator interface.
for(int i=1;i<43;i++)
button[i].setEnabled(false);
textf.setEditable(false);
//Adding the action listener.
for(int i=0;i<43;i++)
button[i].addActionListener(this);
//degrees.addItemListener(this);
//radians.addItemListener(this);
}//End of init method.
class Button_class extends JButton
{
public Button_class(String name, Color xyz)
{
this.setLabel(name);
this.setForeground(xyz);
}
}
public void keyTyped(KeyEvent e)//Denotes a key press followed by a key release.
{
char ch;
ch=e.getKeyChar();
if ((ch=='1')||(ch=='2') || (ch=='3') ||(ch=='4')||
(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9')
||(ch=='0')) //To check the source label of the pressed key.
{
if(pressed)
{
str="0";
pressed=false;
}
if(str=="0")//Check whether string is empty or not.
str=""+ch;
else
str=str+ch;
}//End if.
else if(ch=='.'&& cond5)
{
if(pressed)
{
str="0";
pressed=false;
}
str=str+ch;
cond5=false;
}
else if((ch=='+')||(ch=='-')||(ch=='*') ||(ch=='/'))
{
op=""+ch;
str1=str;
str="0";
}
else if(ch=='=')
calculate();
textf.setText(str);
}//End of method declaration KeyTyped().
public void keyPressed(KeyEvent e)//Indicates a key is pushed down.
{
double ac=e.getKeyCode();
if((ac==13)||(ac==10))
calculate();
else if(ac==8)
{
if(str.compareTo("")==0)
textf.setText("Error");
else
{
str = str.substring(0,str.length()-1);
textf.setText(str);
}
}
}//End of method KeyPressed.
public void keyReleased(KeyEvent e)//Indicates a key is Released.
{
}
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("Developed\n by\n Aman\n" );
}
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -