- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package calculator;
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /**
- *
- * @author BLH
- */
- public class Calculator extends Applet implements ActionListener
- {
- private TextField input = new TextField("",30);
- private TextField empty = new TextField("",0);
- private Button backSpace = new Button("BackSpace");
- private Button CE = new Button(" CE ");
- private Button C = new Button(" C ");
- private Button MC = new Button(" MC ");
- private Button num7 = new Button("7");
- private Button num8 = new Button("8");
- private Button num9 = new Button("9");
- private Button division = new Button("/");
- private Button sqrt = new Button("sqrt");
- private Button MR = new Button(" MR ");
- private Button num4 = new Button("4");
- private Button num5 = new Button("5");
- private Button num6 = new Button("6");
- private Button multiply = new Button("*");
- private Button percent = new Button(" % ");
- private Button MS = new Button(" MS ");
- private Button num1 = new Button("1");
- private Button num2 = new Button("2");
- private Button num3 = new Button("3");
- private Button subtract = new Button("-");
- private Button reciprocal = new Button(" 1/x ");
- private Button MAdd = new Button(" M+ ");
- private Button num0 = new Button("0");
- private Button symbol = new Button("+/-");
- private Button decimal = new Button(".");
- private Button plus = new Button("+");
- private Button amount = new Button(" = ");
- private StringBuffer str=new StringBuffer();
- double x,y;
- int estimate; //用于判断加减乘除等符号
- /**
- * Initialization method that will be called after the applet is loaded
- * into the browser.
- */
- @Override
- public void init()
- {
- // TODO start asynchronous download of heavy resources
- resize(255,255);
- add(input);
- add(empty);
- empty.setEnabled(false);
- empty.addActionListener(this);
- add(backSpace);
- backSpace.setForeground(new Color(255,0,0));
- backSpace.addActionListener(this);
- add(CE);
- CE.setForeground(new Color(255,0,0));
- CE.addActionListener(this);
- add(C);
- C.setForeground(new Color(255,0,0));
- C.addActionListener(this);
- add(MC);
- MC.setForeground(new Color(255,0,0));
- MC.addActionListener(this);
- add(num7);
- num7.setForeground(new Color(0,0,255));
- num7.addActionListener(this);
- add(num8);
- num8.setForeground(new Color(0,0,255));
- num8.addActionListener(this);
- add(num9);
- num9.setForeground(new Color(0,0,255));
- num9.addActionListener(this);
- add(division);
- division.setForeground(new Color(255,0,0));
- division.addActionListener(this);
- add(sqrt);
- sqrt.setForeground(new Color(0,0,255));
- sqrt.addActionListener(this);
- add(MR);
- MR.setForeground(new Color(255,0,0));
- MR.addActionListener(this);
- add(num4);
- num4.setForeground(new Color(0,0,255));
- num4.addActionListener(this);
- add(num5);
- num5.setForeground(new Color(0,0,255));
- num5.addActionListener(this);
- add(num6);
- num6.setForeground(new Color(0,0,255));
- num6.addActionListener(this);
- add(multiply);
- multiply.setForeground(new Color(255,0,0));
- multiply.addActionListener(this);
- add(percent);
- percent.setForeground(new Color(0,0,255));
- percent.addActionListener(this);
- add(MS);
- MS.setForeground(new Color(255,0,0));
- MS.addActionListener(this);
- add(num1);
- num1.setForeground(new Color(0,0,255));
- num1.addActionListener(this);
- add(num2);
- num2.setForeground(new Color(0,0,255));
- num2.addActionListener(this);
- add(num3);
- num3.setForeground(new Color(0,0,255));
- num3.addActionListener(this);
- add(subtract);
- subtract.setForeground(new Color(255,0,0));
- subtract.addActionListener(this);
- add(reciprocal);
- reciprocal.setForeground(new Color(0,0,255));
- reciprocal.addActionListener(this);
- add(MAdd);
- MAdd.setForeground(new Color(255,0,0));
- MAdd.addActionListener(this);
- add(num0);
- num0.setForeground(new Color(0,0,255));
- num0.addActionListener(this);
- add(symbol);
- symbol.setForeground(new Color(0,0,255));
- symbol.addActionListener(this);
- add(decimal);
- decimal.setForeground(new Color(0,0,255));
- decimal.addActionListener(this);
- add(plus);
- plus.setForeground(new Color(255,0,0));
- plus.addActionListener(this);
- add(amount);
- amount.setForeground(new Color(0,0,255));
- amount.addActionListener(this);
- }
- // TODO overwrite start(), stop() and destroy() methods
- @Override
- public void start()
- {
- }
- @Override
- public void stop()
- {
- }
- @Override
- public void destroy()
- {
- }
- @Override
- public void paint(Graphics g )
- {
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==plus)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=0;
- }
- else if(e.getSource()==subtract)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=1;
- }
- else if(e.getSource()==multiply)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=2;
- }
- else if(e.getSource()==division)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=3;
- }
- else if(e.getSource()==CE)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=4;
- }
- else if(e.getSource()==percent)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=5;
- }
- else if(e.getSource()==reciprocal)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=6;
- }
- else if(e.getSource()==C)
- {
- x=Double.parseDouble(input.getText().trim());
- str.setLength(0);
- estimate=7;
- }
- else if(e.getSource()==amount)
- {
- str.setLength(0);
- switch(estimate)
- {
- case 0:input.setText(""+(x+y));break;
- case 1:input.setText(""+(x-y));break;
- case 2:input.setText(""+(x*y));break;
- case 3:input.setText(""+(x/y));break;
- case 4:input.setText(""+(x-x));break;
- case 5:input.setText(""+(x%y));break;
- case 6:input.setText(""+(1/x));break;
- case 7:input.setText(""+(x-x));break;
- }
- }
- else
- {
- input.setText(str.append(e.getActionCommand()).toString());
- y=Double.parseDouble(input.getText().trim());
- }
- }
- }