感谢你浏览本站,本站所有收费项目均做了严格测试,如使用过程中有问题请联系站长QQ:903857121
当前位置:主页 > 项目源码 > Java源码 > 正文

Java Swing实现简单的计算器程序001

09-29 Java源码

1,首先展示效果图

2.核心=代码如下:


  1.  
    public void actionPerformed(ActionEvent e) {
  2.  
     
  3.  
    text.setText(text.getText()+equ.getText());
  4.  
    if(text.getText().contains("+")){//做加法运算
  5.  
    StringTokenizer st1 = new StringTokenizer(text.getText(),"+");
  6.  
    double d1,d2 = 0;
  7.  
    d1 = Double.parseDouble(st1.nextToken());
  8.  
    while(st1.hasMoreTokens()){
  9.  
    StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"=");
  10.  
    d2 = Double.parseDouble(st2.nextToken());
  11.  
    }
  12.  
     
  13.  
    text.setText(text.getText()+" "+(d1+d2));
  14.  
    }else if(text.getText().contains("-")){//做减法运算
  15.  
    StringTokenizer st1 = new StringTokenizer(text.getText(),"-");
  16.  
    double d1,d2 = 0;
  17.  
    d1 = Double.parseDouble(st1.nextToken());
  18.  
    while(st1.hasMoreTokens()){
  19.  
    StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"=");
  20.  
    d2 = Double.parseDouble(st2.nextToken());
  21.  
    }
  22.  
     
  23.  
    text.setText(text.getText()+" "+(d1-d2));
  24.  
    }else if(text.getText().contains("*")){//做乘法运算
  25.  
    StringTokenizer st1 = new StringTokenizer(text.getText(),"*");
  26.  
    double d1,d2 = 0;
  27.  
    d1 = Double.parseDouble(st1.nextToken());
  28.  
    while(st1.hasMoreTokens()){
  29.  
    StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"=");
  30.  
    d2 = Double.parseDouble(st2.nextToken());
  31.  
    }
  32.  
     
  33.  
    text.setText(text.getText()+" "+(d1*d2));
  34.  
    }else if(text.getText().contains("/")){//做除法运算
  35.  
    StringTokenizer st1 = new StringTokenizer(text.getText(),"/");
  36.  
    double d1,d2 = 0;
  37.  
    d1 = Double.parseDouble(st1.nextToken());
  38.  
    while(st1.hasMoreTokens()){
  39.  
    StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"=");
  40.  
    d2 = Double.parseDouble(st2.nextToken());
  41.  
    }
  42.  
     
  43.  
    text.setText(text.getText()+" "+(d1/d2)); //将运算结果放到text区域
  44.  
    }
  45.  
    }
 

3.完整代码见


链接:https://pan.baidu.com/s/1nfGh3j5ge_dhadgo8gZHoQ 

提取码:b1b1 

 

版权保护: 本文由 主页 原创,转载请保留链接: http://www.cnitstudy.com/a/yuanma/Javayuanma/2020/0929/1.html


7*24小时在线
优质源码
请加微信联系
毕业设计
项目修改
项目定制开发