📄 mycalc.java
字号:
import java.awt.*;import java.util.*;import java.awt.event.*;import java.applet.*;import java.math.*;import java.io.*;import java.lang.*;public class MyCalc extends Applet implements ActionListener,ItemListener,KeyListener{ private MyFile f=new MyFile();//class MyFile is an inner class contains the methods we used to act with the files private CheckboxGroup deg; private TextArea ta; private Label l,memoryTag; private MyButton[] button; private TextField t; private String s="0",s1="",s2="",op="",s3=""; private double m=0,n=0,mem=0; private Panel p1,p2,p3,p4,p5,p6,p7,p8; private String[] Sbutton={"Power","Advanced","Shift","C","CE","Back Space","7","8","9","4", "5","6","1","2","3","0",".","PI","=","Ans" ,"Sin","Cos","Sqrt","Sqr","X!","Pow","%","1/X","+/-","S_Store", "E_Store","/","Del","*","Me","-","M+","+","M-","MR", "R","Previow","Close"}; private boolean cond=false,cond1=false,cond2=false,cond3=false,cond4=false,cond5=true,pressed=false; /* the conditions : 1:cond :for advanced button 2:cond1 :for Power 3:cond3 :for checkbox radian or degrees 4:cond4 :for start saving and end saving into the file 5:cond5 :for the simecolon 5:pressed for making the textfield = "" after any calculation */ public void init() { setLayout(new BorderLayout(0,7));//the layout for the applet setBackground(new Color(58,110,165)); l=new Label(" Bad Info Guys"); memoryTag=new Label(" "); memoryTag.setEnabled(true); l.setFont(new Font("Times Roman",Font.BOLD,17)); memoryTag.setFont(new Font("Times Roman",Font.BOLD,17)); memoryTag.setForeground(Color.black); l.setForeground(Color.green); l.setForeground(Color.green); ta=new TextArea("Bahaa Mourad\nGoergeElias\nSamer Al_Fakih\nBashar Mahasin\n-----------------------\n",8,17,TextArea.SCROLLBARS_VERTICAL_ONLY); ta.setFont(new Font("Times Roman",Font.BOLD,12)); ta.setEditable(false); deg = new CheckboxGroup();//the container of the checkboxs Checkbox degrees = new Checkbox("Deg",deg,true); Checkbox radians = new Checkbox("Rad",deg,false); button =new MyButton[43]; for(int i=0;i<20;i++)//creating the Advanced buttons like sin & cos & arcsin etc.. button[i]=new MyButton(Sbutton[i],new Color(174,168,217)); for(int i=20;i<43;i++)//creating the numbers button[i]=new MyButton(Sbutton[i],new Color(58,222,160)); t=new TextField(30); t.setFont(new Font("Times Roman",Font.BOLD,12)); t.addKeyListener(this); t.setText("0"); //constructin the panels in the applet p1=new Panel(); p2=new Panel(); p3=new Panel(); p4=new Panel(); p5=new Panel(); p6=new Panel(); p7=new Panel(); p8=new Panel();//adding the components to matching panel p5.setBounds(20,1,275,30); p5.add(button[0]); p5.add(l); add(p5); p1.setLayout(new GridLayout(3,3,2,2)); p1.setBounds(20,104,225,78); for(int i=20;i<29;i++) p1.add(button[i]); p1.setVisible(false); add(p1); p2.setBounds(20,32,280,70); p2.add(t); p2.add(memoryTag); p2.add(p4); p4.add(button[1]); p4.add(button[2]); p4.add(degrees); p4.add(radians); t.setBackground(Color.black); add(p2); p3.setLayout(new GridLayout(6,3,2,3)); p3.setBounds(20,190,225,156); for (int i=3;i<20;i++) p3.add(button[i]); add(p3); p7.setBounds(300,1,150,182); p7.add(ta); p7.add(button[41]); p7.add(button[42]); add(p7); p6.setLayout(new GridLayout(6,2,2,2)); p6.setBounds(300,190,150,156); p8.setBounds(0,0,400,40); for(int i=29;i<41;i++) p6.add(button[i]); add(p6); add(p8);//befor you can use the Calculator you have to press the power for(int i=1;i<43;i++) button[i].setEnabled(false); t.setEditable(false);//adding the action listener for(int i=0;i<43;i++) button[i].addActionListener(this); degrees.addItemListener(this); radians.addItemListener(this); }//this procedure compute the calculation needed public void calculate() { double l=0; int o=0; String opp=""; s2=s; s="0"; m=Double.valueOf(s1).doubleValue(); n=Double.valueOf(s2).doubleValue(); opp=op; if (op.compareTo("+")==0) { l=m+n; op=""; } else if (op.compareTo("-")==0) { l=m-n; op=""; } else if (op.compareTo("*")==0) { l=m*n; op=""; } else if (op.compareTo("/")==0) { l=m/n; op=""; } else if (op.compareTo("")==0) { l=Double.valueOf(s2).doubleValue(); } else if (op.compareTo("Pow")==0) { o=Integer.valueOf(s2).intValue(); l=MyMath.mypow(m,o); op=""; } cond5=true; pressed=true; s=""+l; t.setText(s); f.addTofile(s1+opp+s2+"="+l); } public void keyPressed(KeyEvent e) { double ac=e.getKeyCode(); if((ac==13)||(ac==10)) calculate(); else if(ac==8) { if(s.compareTo("")==0) t.setText("Error"); else { s=s.substring(0,s.length()-1); t.setText(s); } } } public void keyTyped(KeyEvent e) { 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')) { if(pressed) { s="0"; pressed=false; } if(s=="0") s=""+ch; else s=s+ch; } else if(ch=='.'&&cond5) { if(pressed) { s="0"; pressed=false; } s=s+ch; cond5=false; } else if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')) { op=""+ch; s1=s; s="0"; } else if(ch=='=') calculate(); t.setText(s); } public void keyReleased(KeyEvent e) { } public void itemStateChanged(ItemEvent e) { } public void actionPerformed(ActionEvent e) { if (e.getSource()==button[29])//for begin store in the file { cond4=true; button[30].setEnabled(true); button[29].setEnabled(false); f.CreateFile(); } else if (e.getSource()==button[30])//end of the storing { cond4=false; button[29].setEnabled(true); button[30].setEnabled(false); f.endStore(); } else if (e.getSource()==button[41])//preview { button[41].setEnabled(false); button[42].setEnabled(true); f.WriteFileOnTextArea(); } else if (e.getSource()==button[42])//close for textarea { button[41].setEnabled(true); button[42].setEnabled(false); ta.setText("Bahaa Mourad\nGoergeElias\nSamer Al_Fakih\nBashar Mahasin\n-----------------------\n"); } else if (e.getSource()==button[32])//Del button { f.CreateFile(); } else if (e.getSource()==button[1])//for advanced buttons { if(!cond) { cond=true; p1.setVisible(true); button[1].setLabel("Simple"); for(int i=20;i<29;i++) button[i].setEnabled(true); } else { button[1].setLabel("Advanced"); p1.setVisible(false); cond=false; } } else if (e.getSource()==button[0])//power { if(!cond1) { cond5=true; t.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; t.setBackground(Color.black); ta=new TextArea("Bahaa Mourad\nGoergeElias\nSamer Al_Fakih\nBashar Mahasin\n-----------------------\n"); f.delFile(); s="0";s1="";s2=""; memoryTag.setText(" "); t.setText(s); mem=0; button[0].setLabel("ON"); for(int i=1;i<43;i++) button[i].setEnabled(false); } } else if(e.getSource()==button[16])//semicolon { if(pressed) { s="0"; pressed=false; } if (cond5) { s=s+"."; cond5=false; t.setText(s); } }//The implemetation 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) { s="0"; pressed=false; } if(s.compareTo("0")==0) s=e.getActionCommand(); else s=s+e.getActionCommand(); s3=s; t.setText(s); showStatus(e.getActionCommand()); }//the opertor section (simple opertaion) else if ((e.getSource()==button[37])||(e.getSource()==button[35])||(e.getSource()==button[33])||(e.getSource()==button[31])||(e.getSource()==button[25])) { cond5=true; op=e.getActionCommand(); s1=s; s3=s; s="0"; t.setText(s); } else if ((e.getSource()==button[18])) calculate(); else if (e.getSource()==button[5]) { s=s.substring(0,s.length()-1); t.setText(s); } else if (e.getSource()==button[3]) { op=""; s=s3; t.setText(s); } else if (e.getSource()==button[4]) { t.setText("0"); cond5=true; s="0"; s1=""; s2=""; } else if (e.getSource()==button[27]) { pressed=true; float g=0,p=0; p=Float.valueOf(s).floatValue(); if(p==0) { s="0"; t.setText("Invinite"); } else{ g=(1/p); s=""+g; s3=s; t.setText(s); f.addTofile("1/"+p+"="+s); } } else if (e.getSource()==button[28]) { if (!(s.compareTo("0")==0)) { if(s.charAt(0)=='-') s=s.substring(1,s.length()); else s="-"+s; t.setText(s); } } else if(e.getSource()==button[34]) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -