[原创]java applet Calculator 计算器,仿照Windows计算器

Code:
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package calculator;   
  7.   
  8. import java.applet.Applet;   
  9. import java.awt.Button;   
  10. import java.awt.Color;   
  11. import java.awt.Graphics;   
  12. import java.awt.TextField;   
  13. import java.awt.event.ActionEvent;   
  14. import java.awt.event.ActionListener;   
  15.   
  16. /**  
  17.  *  
  18.  * @author BLH  
  19.  */  
  20. public class Calculator extends Applet implements ActionListener   
  21. {   
  22.     private TextField input = new TextField("",30);   
  23.   
  24.     private TextField empty = new TextField("",0);   
  25.     private Button backSpace = new Button("BackSpace");   
  26.     private Button CE = new Button("   CE    ");   
  27.     private Button C = new Button("    C    ");   
  28.   
  29.     private Button MC = new Button("   MC    ");   
  30.     private Button num7 = new Button("7");   
  31.     private Button num8 = new Button("8");   
  32.     private Button num9 = new Button("9");   
  33.     private Button division = new Button("/");   
  34.     private Button sqrt = new Button("sqrt");   
  35.   
  36.     private Button MR = new Button("   MR    ");   
  37.     private Button num4 = new Button("4");   
  38.     private Button num5 = new Button("5");   
  39.     private Button num6 = new Button("6");   
  40.     private Button multiply = new Button("*");   
  41.     private Button percent = new Button(" %  ");   
  42.   
  43.     private Button MS = new Button("   MS     ");   
  44.     private Button num1 = new Button("1");   
  45.     private Button num2 = new Button("2");   
  46.     private Button num3 = new Button("3");   
  47.     private Button subtract = new Button("-");   
  48.     private Button reciprocal = new Button(" 1/x ");   
  49.   
  50.     private Button MAdd = new Button("   M+     ");   
  51.     private Button num0 = new Button("0");   
  52.     private Button symbol = new Button("+/-");   
  53.     private Button decimal = new Button(".");   
  54.     private Button plus = new Button("+");   
  55.     private Button amount = new Button(" =  ");   
  56.        
  57.     private StringBuffer str=new StringBuffer();   
  58.     double x,y;   
  59.     int estimate; //用于判断加减乘除等符号   
  60.        
  61.     /**  
  62.      * Initialization method that will be called after the applet is loaded  
  63.      * into the browser.  
  64.      */  
  65.     @Override  
  66.     public void init()   
  67.     {   
  68.         // TODO start asynchronous download of heavy resources   
  69.         resize(255,255);   
  70.         add(input);   
  71.   
  72.         add(empty);   
  73.         empty.setEnabled(false);   
  74.         empty.addActionListener(this);   
  75.         add(backSpace);   
  76.         backSpace.setForeground(new Color(255,0,0));   
  77.         backSpace.addActionListener(this);   
  78.         add(CE);   
  79.         CE.setForeground(new Color(255,0,0));   
  80.         CE.addActionListener(this);   
  81.         add(C);   
  82.         C.setForeground(new Color(255,0,0));   
  83.         C.addActionListener(this);   
  84.   
  85.         add(MC);   
  86.         MC.setForeground(new Color(255,0,0));   
  87.         MC.addActionListener(this);   
  88.         add(num7);   
  89.         num7.setForeground(new Color(0,0,255));   
  90.         num7.addActionListener(this);   
  91.         add(num8);   
  92.         num8.setForeground(new Color(0,0,255));   
  93.         num8.addActionListener(this);   
  94.         add(num9);   
  95.         num9.setForeground(new Color(0,0,255));   
  96.         num9.addActionListener(this);   
  97.         add(division);   
  98.         division.setForeground(new Color(255,0,0));   
  99.         division.addActionListener(this);   
  100.         add(sqrt);   
  101.         sqrt.setForeground(new Color(0,0,255));   
  102.         sqrt.addActionListener(this);   
  103.   
  104.         add(MR);   
  105.         MR.setForeground(new Color(255,0,0));   
  106.         MR.addActionListener(this);   
  107.         add(num4);   
  108.         num4.setForeground(new Color(0,0,255));   
  109.         num4.addActionListener(this);   
  110.         add(num5);   
  111.         num5.setForeground(new Color(0,0,255));   
  112.         num5.addActionListener(this);   
  113.         add(num6);   
  114.         num6.setForeground(new Color(0,0,255));   
  115.         num6.addActionListener(this);   
  116.         add(multiply);   
  117.         multiply.setForeground(new Color(255,0,0));   
  118.         multiply.addActionListener(this);   
  119.         add(percent);   
  120.         percent.setForeground(new Color(0,0,255));   
  121.         percent.addActionListener(this);   
  122.   
  123.         add(MS);   
  124.         MS.setForeground(new Color(255,0,0));   
  125.         MS.addActionListener(this);   
  126.         add(num1);   
  127.         num1.setForeground(new Color(0,0,255));   
  128.         num1.addActionListener(this);   
  129.         add(num2);   
  130.         num2.setForeground(new Color(0,0,255));   
  131.         num2.addActionListener(this);   
  132.         add(num3);   
  133.         num3.setForeground(new Color(0,0,255));   
  134.         num3.addActionListener(this);   
  135.         add(subtract);   
  136.         subtract.setForeground(new Color(255,0,0));   
  137.         subtract.addActionListener(this);   
  138.         add(reciprocal);   
  139.         reciprocal.setForeground(new Color(0,0,255));   
  140.         reciprocal.addActionListener(this);   
  141.   
  142.         add(MAdd);   
  143.         MAdd.setForeground(new Color(255,0,0));    
  144.         MAdd.addActionListener(this);   
  145.         add(num0);   
  146.         num0.setForeground(new Color(0,0,255));    
  147.         num0.addActionListener(this);   
  148.         add(symbol);   
  149.         symbol.setForeground(new Color(0,0,255));   
  150.         symbol.addActionListener(this);   
  151.         add(decimal);   
  152.         decimal.setForeground(new Color(0,0,255));   
  153.         decimal.addActionListener(this);   
  154.         add(plus);   
  155.         plus.setForeground(new Color(255,0,0));   
  156.         plus.addActionListener(this);   
  157.         add(amount);   
  158.         amount.setForeground(new Color(0,0,255));   
  159.         amount.addActionListener(this);   
  160.     }   
  161.   
  162.     // TODO overwrite start(), stop() and destroy() methods   
  163.   
  164.     @Override  
  165.     public void start()   
  166.     {   
  167.   
  168.     }   
  169.     @Override  
  170.     public void stop()   
  171.     {   
  172.   
  173.     }   
  174.     @Override  
  175.     public void destroy()   
  176.     {   
  177.   
  178.     }   
  179.   
  180.     @Override  
  181.     public void paint(Graphics g )   
  182.     {   
  183.     }   
  184.   
  185.     public void actionPerformed(ActionEvent e)    
  186.     {   
  187.          if(e.getSource()==plus)   
  188.          {   
  189.              x=Double.parseDouble(input.getText().trim());   
  190.              str.setLength(0);   
  191.              estimate=0;   
  192.          }   
  193.          else if(e.getSource()==subtract)   
  194.          {   
  195.              x=Double.parseDouble(input.getText().trim());   
  196.              str.setLength(0);   
  197.              estimate=1;   
  198.          }   
  199.          else if(e.getSource()==multiply)   
  200.          {   
  201.              x=Double.parseDouble(input.getText().trim());   
  202.              str.setLength(0);   
  203.              estimate=2;   
  204.          }   
  205.          else if(e.getSource()==division)   
  206.          {   
  207.              x=Double.parseDouble(input.getText().trim());   
  208.              str.setLength(0);   
  209.              estimate=3;   
  210.          }   
  211.          else if(e.getSource()==CE)   
  212.          {   
  213.              x=Double.parseDouble(input.getText().trim());   
  214.              str.setLength(0);   
  215.              estimate=4;   
  216.          }   
  217.          else if(e.getSource()==percent)   
  218.          {   
  219.              x=Double.parseDouble(input.getText().trim());   
  220.              str.setLength(0);   
  221.              estimate=5;   
  222.          }   
  223.          else if(e.getSource()==reciprocal)   
  224.          {   
  225.              x=Double.parseDouble(input.getText().trim());   
  226.              str.setLength(0);   
  227.              estimate=6;   
  228.          }   
  229.          else if(e.getSource()==C)   
  230.          {   
  231.              x=Double.parseDouble(input.getText().trim());   
  232.              str.setLength(0);   
  233.              estimate=7;   
  234.          }   
  235.          else if(e.getSource()==amount)   
  236.          {   
  237.              str.setLength(0);   
  238.              switch(estimate)   
  239.              {   
  240.                  case 0:input.setText(""+(x+y));break;   
  241.                  case 1:input.setText(""+(x-y));break;   
  242.                  case 2:input.setText(""+(x*y));break;   
  243.                  case 3:input.setText(""+(x/y));break;   
  244.                  case 4:input.setText(""+(x-x));break;   
  245.                  case 5:input.setText(""+(x%y));break;   
  246.                  case 6:input.setText(""+(1/x));break;   
  247.                  case 7:input.setText(""+(x-x));break;   
  248.              }   
  249.          }   
  250.          else  
  251.          {   
  252.              input.setText(str.append(e.getActionCommand()).toString());   
  253.              y=Double.parseDouble(input.getText().trim());   
  254.          }   
  255.     }   
  256. }   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值