用java编写一个简单计算器

发表于:2007-07-04来源:作者:点击数: 标签:
/////calculator. java 代码 import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.event.*; public class calculator

/////calculator.java代码

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class calculator extends JApplet{
 
 
  Container contentPane;
  
  JButton btn0 = new JButton();
  JButton btn1 = new JButton();
  JButton btn2 = new JButton();
  JButton btn3 = new JButton();
  JButton btn4 = new JButton();
  JButton btn5 = new JButton();
  JButton btn6 = new JButton();
  JButton btn7 = new JButton();
  JButton btn8 = new JButton();
  JButton btn9 = new JButton();
  
  //特殊按钮
  JButton btnClear = new JButton();        //清除
  JButton btnEqual = new JButton();        //等于
  JButton btnPlus = new JButton();         //+操作符
  JButton btnMinus = new JButton();        //-操作符
  JButton btnMultiply = new JButton();     //*操作符
  JButton btnDivide = new JButton();       //除操作符
  
  JTextField texResult = new JTextField();
 
  boolean flag=false;
  String operand1;
  String operand2;
  double result;
  String action;
 
 public void init(){
 
  
  
  
  contentPane = getContentPane();
     contentPane.setLayout(null);
     setSize(new Dimension(250, 250));
    
     btn0.setBounds(new Rectangle(50, 230, 45, 45));
     btn0.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14));
     btn0.setText("0");
     btn0.addActionListener(new btn0(this));
    
     //清除操作符
     btnClear.setBounds(new Rectangle(100,230,45,45));
     btnClear.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btnClear.setText("C");
     btnClear.addActionListener(new btnClear(this));
    
     //=操作符
     btnEqual.setBounds(new Rectangle(150,230,45,45));
     btnEqual.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btnEqual.setText("=");
     btnEqual.addActionListener(new btnEqual(this));
    
     //+操作符
     btnPlus.setBounds(new Rectangle(200,230,45,45));
     btnPlus.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btnPlus.setText("+");
     btnPlus.addActionListener(new btnPlus(this));
    
     btn1.setBounds(new Rectangle(50,180,45,45));
     btn1.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn1.setText("1");
     btn1.addActionListener(new btn1(this));
    
     btn2.setBounds(new Rectangle(100,180,45,45));
     btn2.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn2.setText("2");
     btn2.addActionListener(new btn2(this));
    
     btn3.setBounds(new Rectangle(150,180,45,45));
     btn3.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn3.setText("3");
     btn3.addActionListener(new btn3(this));
    
     //-操作符
     btnMinus.setBounds(new Rectangle(200,180,45,45));
     btnMinus.setFont(new java.awt.Font("Tahoma",Font.PLAIN,16));
     btnMinus.setText("-");
     btnMinus.addActionListener(new btnMinus(this));
    
     btn4.setBounds(new Rectangle(50,130,45,45));
     btn4.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn4.setText("4");
     btn4.addActionListener(new btn4(this));
    
     btn5.setBounds(new Rectangle(100,130,45,45));
     btn5.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn5.setText("5");
     btn5.addActionListener(new btn5(this));
    
     btn6.setBounds(new Rectangle(150,130,45,45));
     btn6.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn6.setText("6");
     btn6.addActionListener(new btn6(this));
    
     //*操作符
     btnMultiply.setBounds(new Rectangle(200,130,45,45));
     btnMultiply.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btnMultiply.setText("*");
     btnMultiply.addActionListener(new btnMultiply(this));
    
     btn7.setBounds(new Rectangle(50,80,45,45));
     btn7.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn7.setText("7");
     btn7.addActionListener(new btn7(this));
    
     btn8.setBounds(new Rectangle(100,80,45,45));
     btn8.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn8.setText("8");
     btn8.addActionListener(new btn8(this));
    
     btn9.setBounds(new Rectangle(150,80,45,45));
     btn9.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btn9.setText("9");
     btn9.addActionListener(new btn9(this));
    
     //除操作符
     btnDivide.setBounds(new Rectangle(200,80,45,45));
     btnDivide.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
     btnDivide.setText("/");
     btnDivide.addActionListener(new btnDivide(this));
    
     texResult.setText("");
        texResult.setBounds(new Rectangle(40, 35, 215, 35));
        texResult.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14));
        texResult.setHorizontalAlignment(JTextField.RIGHT);
    
     contentPane.add(btn0);
     contentPane.add(btn1);
     contentPane.add(btn2);
     contentPane.add(btn3); 
     contentPane.add(btn4);
     contentPane.add(btn5);
     contentPane.add(btn6);
     contentPane.add(btn7);
     contentPane.add(btn8);
     contentPane.add(btn9);
    
     contentPane.add(btnClear);
     contentPane.add(btnEqual);
     contentPane.add(btnPlus);
     contentPane.add(btnMinus);
     contentPane.add(btnMultiply);
     contentPane.add(btnDivide);
    
     contentPane.add(texResult);
  }
 
 
  //0的事件监听器
  public void btn0_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn0.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn0.getActionCommand()); 
   } 
  }
   
    class btn0 implements ActionListener{
     
     private calculator adaptee;

     btn0(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn0_actionPerformed(e);
     }
 }
 
 
 //1的事件监听器
 public void btn1_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn1.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn1.getActionCommand()); 
   } 
  }
   
    class btn1 implements ActionListener{
     
     private calculator adaptee;

     btn1(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn1_actionPerformed(e);
     }
 }
 
 //2的事件监听器
 public void btn2_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn2.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn2.getActionCommand()); 
   } 
  }
   
    class btn2 implements ActionListener{
     
     private calculator adaptee;

     btn2(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn2_actionPerformed(e);
     }
 }
 
 //3的事件监听器
 public void btn3_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn3.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn3.getActionCommand()); 
   } 
  }
   
    class btn3 implements ActionListener{
     
     private calculator adaptee;

     btn3(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn3_actionPerformed(e);
     }
 }
 
 //4的事件监听器
 public void btn4_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn4.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn4.getActionCommand()); 
   } 
  }
   
    class btn4 implements ActionListener{
     
     private calculator adaptee;

     btn4(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn4_actionPerformed(e);
     }
 }
 
 //5的事件监听器
 public void btn5_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn5.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn5.getActionCommand()); 
   } 
  }
   
    class btn5 implements ActionListener{
     
     private calculator adaptee;

     btn5(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn5_actionPerformed(e);
     }
 }
 
 //6的事件监听器
 public void btn6_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn6.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn6.getActionCommand()); 
   } 
  }
   
    class btn6 implements ActionListener{
     
     private calculator adaptee;

     btn6(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn6_actionPerformed(e);
     }
 }
 
 //7的事件监听器
 public void btn7_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn7.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn7.getActionCommand()); 
   } 
  }
   
    class btn7 implements ActionListener{
     
     private calculator adaptee;

     btn7(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn7_actionPerformed(e);
     }
 }
 
 //8的事件监听器
 public void btn8_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn8.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn8.getActionCommand()); 
   } 
  }
   
    class btn8 implements ActionListener{
     
     private calculator adaptee;

     btn8(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn8_actionPerformed(e);
     }
 }
 
 //9的事件监听器
 public void btn9_actionPerformed(ActionEvent e){
 
   if(flag){
    texResult.setText(btn9.getActionCommand());
    flag = false; 
   }
   else{
    
    texResult.setText(texResult.getText()+btn9.getActionCommand()); 
   } 
  }
   
    class btn9 implements ActionListener{
     
     private calculator adaptee;

     btn9(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btn9_actionPerformed(e);
     }
 }
 
 //清除操作
 public void btnClear_actionPerformed(ActionEvent e){
 
   texResult.setText("");
  }
   
    class btnClear implements ActionListener{
     
     private calculator adaptee;

     btnClear(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnClear_actionPerformed(e);
     }
 }
 
 //+操作事件监听器
 public void btnPlus_actionPerformed(ActionEvent e){
 
   operand1 = texResult.getText();
   action = "plus";
   flag = true;
  }
   
    class btnPlus implements ActionListener{
     
     private calculator adaptee;

     btnPlus(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnPlus_actionPerformed(e);
     }
 }
 
 //-操作事件监听器
 public void btnMinus_actionPerformed(ActionEvent e){
 
   operand1 = texResult.getText();
   action = "minus";
   flag = true;
  }
   
    class btnMinus implements ActionListener{
     
     private calculator adaptee;

     btnMinus(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnMinus_actionPerformed(e);
     }
 }
 
 //*操作事件监听器
 public void btnMultiply_actionPerformed(ActionEvent e){
 
   operand1 = texResult.getText();
   action = "multiply";
   flag = true;
  }
   
    class btnMultiply implements ActionListener{
     
     private calculator adaptee;

     btnMultiply(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnMultiply_actionPerformed(e);
     }
 }
 
 //除操作事件监听器
 public void btnDivide_actionPerformed(ActionEvent e){
 
   operand1 = texResult.getText();
   action = "divide";
   flag = true;
  }
   
    class btnDivide implements ActionListener{
     
     private calculator adaptee;

     btnDivide(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnDivide_actionPerformed(e);
     }
 }
 
 //=操作事件监听器
 public void btnEqual_actionPerformed(ActionEvent e){
 
   double digit1;
   double digit2;
   operand2 = texResult.getText();
   
   if(!flag){
    if(action.equals("plus")){
     
     digit1 = Double.parseDouble(operand1);
     digit2 = Double.parseDouble(operand2);
     result = digit1 + digit2;
     texResult.setText("" + result);
     flag = true; 
    }
    else if(action.equals("minus")){
     
     digit1 = Double.parseDouble(operand1);
     digit2 = Double.parseDouble(operand2);
     result = digit1 - digit2;
     texResult.setText(""+result);
     flag = true; 
    }
    else if(action.equals("multiply")){
     
     digit1 = Double.parseDouble(operand1);
     digit2 = Double.parseDouble(operand2);
     result = digit1 * digit2;
     texResult.setText(""+result);
     flag = true;
    }
    else if(action.equals("divide")){
     
     digit1 = Double.parseDouble(operand1);
     digit2 = Double.parseDouble(operand2);
     result = digit1 / digit2;
     texResult.setText(""+result);
     flag = true; 
    } 
   }
  }
   
    class btnEqual implements ActionListener{
     
     private calculator adaptee;

     btnEqual(calculator adaptee){
         this.adaptee=adaptee;
     }

     public void actionPerformed(ActionEvent e){
         adaptee.btnEqual_actionPerformed(e);
     }
 }
}

////calculator.html文件代码

<html>
<head><title>计算器</title></head>
<center>
<applet code=calculator.class width=300 height=300>
</applet>
</center>
</html>

以上只要有java运行环境JDK,将calculator.java文件编译,然后建一文本文件,直接运行calculator.html就可以看到效果,不过不可以连乘、除、加、减不足之处多多指点。

原文转自:http://www.ltesting.net