欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 23:39:55
本文实例讲述了java实现简单的计算器类。分享给大家供大家参考。具体如下: package chap; import java.awt.borderlayout...

本文实例讲述了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程序设计有所帮助。