java实现简单的计算器类实例

本文实例讲述了java实现简单的计算器类。分享给大家供大家参考。具体如下:

package chap;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
  private JFrame frame;
  private JPanel panel,panelKeys,panelKeys_up,panelKeys_down;
  private JTextField textComputer;//计算区域
  private JButton buttonBk,buttonC;//退格键和清零键
  private JButton button[];//数字键组
  private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual;
  private double result;//计算结果
  private final short ADD = 1;
  private final short SUB = 2;
  private final short MUL = 3;
  private final short DIV = 4;
  private short operator = -1;//运算符
  public Calculator(){
    frame = new JFrame("计算机");
    frame.setSize(400, 250);
    panel = new JPanel();//全局面板
    panel.setVisible(true);
    frame.setVisible(true);
    frame.getContentPane().add(panel);
    ActionListener listener = new ComputerActionListener();//按键监听器
    //计算区
    textComputer = new JTextField(15);
    textComputer.setText("");
    textComputer.setEditable(false);
    textComputer.setBackground(new Color(255,255,255));
    //功能键上半部分
    panelKeys_up = new JPanel();
    panelKeys_up.setLayout(new FlowLayout(FlowLayout.RIGHT));
    buttonBk = new JButton("Backspace");
    buttonBk.setForeground(new Color(255,0,0));
    buttonC = new JButton("C");
    buttonC.setForeground(new Color(255,0,0));
    buttonBk.addActionListener(listener);
    buttonC.addActionListener(listener);
    panelKeys_up.add(buttonBk);
    panelKeys_up.add(buttonC);
    //功能键下半部分
    panelKeys_down = new JPanel();
    panelKeys_down.setLayout(new GridLayout(4,5));
    button = new JButton[10];
    for(int i = 0;i < button.length;i++){
      button[i] = new JButton(Integer.toString(i));
      button[i].setForeground(new Color(255,0,0));
    }
    buttonAddSub = new JButton("+/-");
    buttonAddSub.setForeground(new Color(255,0,0));
    buttonAdd = new JButton("+");
    buttonAdd.setForeground(new Color(255,0,0));
    buttonSub = new JButton("-");
    buttonSub.setForeground(new Color(255,0,0));
    buttonMul = new JButton("*");
    buttonMul.setForeground(new Color(255,0,0));
    buttonDiv = new JButton("/");
    buttonDiv.setForeground(new Color(255,0,0));
    button1 = new JButton();
    button2 = new JButton();
    button3 = new JButton();
    button1.setForeground(new Color(255,0,0));
    button2.setForeground(new Color(255,0,0));
    button3.setForeground(new Color(255,0,0));
    buttonEqual = new JButton("=");
    buttonEqual.setForeground(new Color(255,0,0));
    buttonAddSub.addActionListener(listener);
    buttonAdd.addActionListener(listener);
    buttonSub.addActionListener(listener);
    buttonMul.addActionListener(listener);
    buttonDiv.addActionListener(listener);
    buttonEqual.addActionListener(listener);
    for(int i = 0 ; i <=9 ; i++){
      button[i].addActionListener(listener);
    }
    for(int i = 0 ; i <=9 ; i++){
      panelKeys_down.add(button[i]);
    }
    panelKeys_down.add(buttonAddSub);
    panelKeys_down.add(buttonAdd);
    panelKeys_down.add(buttonSub);
    panelKeys_down.add(buttonMul);
    panelKeys_down.add(buttonDiv);
    panelKeys_down.add(buttonEqual);
    panel.setLayout(new BorderLayout());
    panel.add(textComputer,BorderLayout.NORTH);
    panel.add(panelKeys_up,BorderLayout.CENTER);
    panel.add(panelKeys_down,BorderLayout.SOUTH);
  }
  class ComputerActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent event) {
      // TODO Auto-generated method stub
      Object keyButton = event.getSource();
      String text = textComputer.getText();
      DecimalFormat df = new DecimalFormat("0.###########");
      //Backspace
      if(keyButton == buttonBk && text.length() > 0){
        textComputer.setText(text.substring(0,text.length()-1));
      }
      //C键
      if(keyButton == buttonC){
        result = 0;
        textComputer.setText("");
      }
      //数字键
      for(int i=0;i<10;i++){
        if(keyButton == button[i]){
          textComputer.setText(text+i);
        }
      }
      if(keyButton == buttonAdd){
        operator = 1;
      }
      if(keyButton == buttonSub){
        operator = 2;
      }
      if(keyButton == buttonMul){
        operator = 3;
      }
      if(keyButton == buttonDiv){
        operator = 4;
      }
      //符号键
      if(keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual){
        switch(operator){
        case ADD:
          result += Double.parseDouble(text);
          break;
        case SUB:
          result -=Double.parseDouble(text);
          break;
        case MUL:
          result *=Double.parseDouble(text);
          break;
        case DIV:
          result /=Double.parseDouble(text);
          break;
        default:
          result = Double.parseDouble(text);
        }
        textComputer.setText("");
      }
      if(keyButton == buttonEqual){
        textComputer.setText(String.valueOf(result));
      }
    }
  }
  public static void main(String args[]){
    new Calculator();
  }
}

希望本文所述对大家的java程序设计有所帮助。

你可能感兴趣的:(java实现简单的计算器类实例)