java 计算器代码

import java.awt.*;
import java.awt.event.*;


public class Calculator extends Frame
{
 public static void main(String args[])
 {
  new Calculator();
 }
 public static int i=1;
 public static boolean bl=true;
 public static  float num1=0,num2=0,num3=0;
 public static TextField tf=new TextField();
 Button btBackspace=new Button("Backspace");
 Button btCE=new Button("      CE      ");
 Button btC=new Button("         C         ");
 Button bt0=new Button("0");
 Button bt1=new Button("1");
 Button bt2=new Button("2");
 Button bt3=new Button("3");
 Button bt4=new Button("4");
 Button bt5=new Button("5");
 Button bt6=new Button("6");
 Button bt7=new Button("7");
 Button bt8=new Button("8");
 Button bt9=new Button("9");
 Button btDivide=new Button("/");
 Button btSqrt=new Button ("sqrt");
 Button btMultiply=new Button("*");
 Button btPercent=new Button("%");
 Button btSubtract=new Button("-");
 Button btReciprocal=new Button("1/x");
 Button btPANDS=new Button("+/-");
 Button btPaint=new Button(".");
 Button btPlus=new Button("+");
 Button btEqual=new Button("=");
 
 Panel p1=new Panel();
 Panel p2=new Panel();
 Panel p3=new Panel();
 public Calculator()
 {

  setTitle("计算器");
  setBounds(300,200,250,250);
  setBackground(new Color(238,229,222));
  setResizable(false);
  
  addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });

  add(tf,BorderLayout.NORTH);
  p1.setLayout(new BorderLayout());
  add(p1,BorderLayout.CENTER);
  p2.setLayout(new FlowLayout());
  p1.add(p2,BorderLayout.NORTH);
  p3.setLayout(new GridLayout(4,5));
  p1.add(p3,BorderLayout.CENTER);
  p2.add(btBackspace);
  p2.add(btCE);
  p2.add(btC);
  
  bt0.addActionListener(new Monitor());
  bt1.addActionListener(new Monitor());
  bt2.addActionListener(new Monitor());
  bt3.addActionListener(new Monitor());
  bt4.addActionListener(new Monitor());
  bt5.addActionListener(new Monitor());
  bt6.addActionListener(new Monitor());
  bt7.addActionListener(new Monitor());
  bt8.addActionListener(new Monitor());
  bt9.addActionListener(new Monitor());
  btPaint.addActionListener(new Monitor());
  btPlus.addActionListener(new Monitor());
  btSubtract.addActionListener(new Monitor());
  btMultiply.addActionListener(new Monitor());
  btReciprocal.addActionListener(new Monitor());
  btPANDS.addActionListener(new Monitor());
  btEqual.addActionListener(new Monitor());
  btDivide.addActionListener(new Monitor());
  btSqrt.addActionListener(new Monitor());
  btBackspace.addActionListener(new Monitor());
  btCE.addActionListener(new Monitor());
  btC.addActionListener(new Monitor());
  btPercent.addActionListener(new Monitor());

  
  
  p3.add(bt7);
  p3.add(bt8);
  p3.add(bt9);
  p3.add(btDivide);
  p3.add(btSqrt);
  p3.add(bt4);
  p3.add(bt5);
  p3.add(bt6);
  p3.add(btMultiply);
  p3.add(btPercent);
  p3.add(bt1);
  p3.add(bt2);
  p3.add(bt3);
  p3.add(btSubtract);
  p3.add(btReciprocal);
  p3.add(bt0);
  p3.add(btPANDS);
  p3.add(btPaint);
  p3.add(btPlus);
  p3.add(btEqual);
  
  
  setVisible(true);   
 }
 
 public class Monitor implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   if(e.getActionCommand().equals("+")){
    compute();
    Calculator.i=1;
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("-")){
    compute();
    Calculator.i=2;
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("*")){
    compute();
    Calculator.i=3;
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("/")){
    compute();
    Calculator.i=4;
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("         C         ")){
    Calculator.tf.setText("");
    Calculator.num1=0;
    Calculator.bl=true;
   }else if(e.getActionCommand().equals("=")){
    compute();
    Calculator.num1=0;
    Calculator.i=1;
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("+/-")){
    float number=0-Float.parseFloat(Calculator.tf.getText());
    Calculator.tf.setText(""+number);
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("1/x")){
    if(Float.parseFloat(Calculator.tf.getText())!=0){
    float number=1/Float.parseFloat(Calculator.tf.getText());
    Calculator.tf.setText(""+number);
    }else
     Calculator.tf.setText("被除数不能为零");
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("sqrt")){
    if(Float.parseFloat(Calculator.tf.getText())<0)
     Calculator.tf.setText("负数不能开根号");
    else{
     Calculator.tf.setText(""+Math.sqrt(Double.parseDouble(Calculator.tf.getText())));
    }
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("      CE      ")){
    Calculator.tf.setText("");
    Calculator.bl=false;
   }else if(e.getActionCommand().equals("Backspace")){
    int length=Calculator.tf.getText().length();
    Calculator.tf.setText(Calculator.tf.getText().substring(0,length-1));
    Calculator.bl=true;
   }else if(e.getActionCommand().equals("%")){
    compute();
    Calculator.i=5;
    Calculator.bl=false;
   }
   else
   
   {
    if(!bl) Calculator.tf.setText("");
    Calculator.tf.setText(Calculator.tf.getText()+e.getActionCommand());
    Calculator.bl=true;
   }
  }
  public void compute()
  {
   switch(i)
   {
    case 1:
     num1=num1+Float.parseFloat(Calculator.tf.getText());
     Calculator.tf.setText(Float.toString(num1));
     break;
    case 2:
     num1=num1-Float.parseFloat(Calculator.tf.getText());
     Calculator.tf.setText(""+num1);
     break;
    case 3:
     num1=num1*Float.parseFloat(Calculator.tf.getText());
     Calculator.tf.setText(""+num1);
     break;
    case 4:
     if(Float.parseFloat(Calculator.tf.getText())!=0){
     
      num1=num1/Float.parseFloat(Calculator.tf.getText());
      Calculator.tf.setText(""+num1);
     }else
     Calculator.tf.setText("被除数不能为零");
     break;
    case 5:
     num1=num1%Float.parseFloat(Calculator.tf.getText());
     Calculator.tf.setText(""+num1);
     break;
   }
 }
 }


}

你可能感兴趣的:(java 计算器代码)