![用JAVA编写计算器程序模拟Windows计算器_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-2/24/e2af443e-b2db-44ea-a1da-30fe993337b3/e2af443e-b2db-44ea-a1da-30fe993337b31.gif)
![用JAVA编写计算器程序模拟Windows计算器_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-2/24/e2af443e-b2db-44ea-a1da-30fe993337b3/e2af443e-b2db-44ea-a1da-30fe993337b32.gif)
![用JAVA编写计算器程序模拟Windows计算器_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-2/24/e2af443e-b2db-44ea-a1da-30fe993337b3/e2af443e-b2db-44ea-a1da-30fe993337b33.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、用JAVA编写计算器程序(模拟 Windows计算器)import java.awt.*;import java.awt.event.*;public class Calculation extends WindowAdapterimplements ActionListenerdouble dResult=0;double dNowI nput=O;double dMemory;int n=0; /记载小数位数int nOperation=1;/记录运算符类型int nBitsNum=0;记录总共输入的位数boolea n alreadyHaveDot=false;/ 已经有小数点?bool
2、ea n keyAvailable=true;boolean alreadyClickedEqueal=false;是否按下过"="?boolean isTempNowl nput=false;是否在计算出结果后直接按运算符将结果赋给了当前输入值?Frame f;Pa nel p1,p2,p3,p4,p5,p6;TextField tf1,tf2;Butt on b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;Butt on bDiv,bSqrt,bMulti,bMi nus,bPerce nt,bPlus,bReciprocal,bEqual,bDot,bNe
3、gative;Butt on bBackspace,bCE,bC,bMR,bMS,bMC,bM;public void display()f=new Frame(” 计算器");f.setSize(280,213);f.setLocatio n(200,200);f.setBackgrou nd(Color.LIGHT_GRAY);f.setResizable(false);f.setLayout (new BorderLayout(3,3);p1= new Pan el( new GridLayout(1,3,5,5);/用于存放 backspace,ce,cp2=new Pan
4、el( new GridLayout(4,5,5,5);/用于存放数字区及附近共设置可能不合理,以后调整p3=new Pa nel( new GridLayout(5,1,5,5);用于存放MC,MR,MS,M+三键20键,此处间隙键及显示M状态文本框,此处间隙设置可能不合理,以后调整p4=new Pan el( newFlowLayout();/用于存放p1,p2p5=new Pan el( newFlowLayout();p6=new Pan el( newFlowLayout();p4.add(p1);p4.add(p2);tf1= new TextField(35);/存放显示区tf1
5、.setText("0.");tf1.setEditable(false);p5.add(tf1);f.add(p5,BorderLayout.NORTH);f.add(p4,BorderLayout.CENTER);f.add(p3,BorderLayout.WEST);b1= new Butto n("1");b2=new Butto n( "2");b3=new Butt on ("3");b4=new Butto n( "4");b5=new Butt on ("5"
6、);b6=new Butto n("6");b7=new Butt on ("7");b8=new Butto n("8");b9=new Butto n("9");b0=new Butto n("0");bl.addAct ion Liste ner(this); b2.addAct ion Liste ner(this);b3.addAct ion Liste ner(this);b4.addAct ion Liste ner(this);b5.addAct ion Liste ner(th
7、is);b6.addAct ion Liste ner(this);b7.addAct ion Liste ner(this);b8.addAct ion Liste ner(this);b9.addAct ion Liste ner(this); bO.addAct ion Liste ner(this);bDiv= new Butto n("/"); bSqrt=new Butto n( "sqrt"); bMulti=new Butto n("*"); bMinus=new Butto n("-"); bPe
8、rce nt=newButto n("%");bPlus=newButto n("+");bReciprocal=newButto n("1/x");bEqual=newButt on ("=");bDot= new Butto n("."); bNegative=newButto n("+/-");bDiv.addActio nListe ner(this);bSqrt.addAct ion Liste ner(this); bMulti.addActio nListe n
9、er(this); bMinu s.addAct ion Liste ner(this); bPerce nt.addAct ion Liste ner(this); bPIus.addAct ion Liste ner(this); bReciprocal.addActi on Liste ner(this); bEqual.addAct ion Liste ner(this); bDot.addActi on Liste ner(this); bNegative.addAct ion Liste ner(this); p2.add(b7);p2.add(b8); p2.add(b9);p2
10、.add(bDiv);p2.add(bSqrt); p2.add(b4); p2.add(b5);p2.add(b6);p2.add(bMulti); p2.add(bPerce nt);p2.add(b1);p2.add(b2); p2.add(b3);p2.add(bMi nus); p2.add(bReciprocal);p2.add(b0); p2.add(bNegative);p2.add(bDot); p2.add(bPlus);p2.add(bEqual);bBackspace=newButt on ("Backspace");bCE=new Butt on
11、("CE");bC=new Butto n("C");bBackspace.addAct ion Liste ner(this);bCE.addAct ion Liste ner(this);bC.addActi on Liste ner(this);p1.add(bBackspace);p1.add(bCE);p1.add(bC);tf2=new TextField(2);tf2.setE nabled(false);tf2.setBackgrou nd(Color.LIGHT_GRAY);bMC=new Butto n("MC")
12、;bMR=new Butto n("MR");bMS=new Butto n("MS");bM=new Butt on ("M+");bMC.addActio nListe ner(this);bMR.addActio nListe ner(this);bMS.addActi on Liste ner(this);bM.addActio nListe ner(this);p6.add(tf2);p3.add(p6);p3.add(bMC);p3.add(bMR);p3.add(bMS);p3.add(bM);f.setVisible(
13、true);f.addWi ndowListe ner(this);public void actionPerformed(ActionEvente)/key 0 to 9if(this.keyAvailable && e.getActio nComma nd().len gth()=1&& e.getActi on Comma nd().compareTo("0")>=0&& e.getActionCommand().compareTo("9")<=0)if(this.isTempNowl n
14、put)this.dNowI nput=O;this.isTempNowI nput=false;this. nBitsNum+;if(this.alreadyHaveDot=false)this.dNow In put=this.dNow In put*10+Double.parseDouble(e.getActi on Comma nd();elsedouble temp=Double.parseDouble(e.getActi on Comma nd();for(int i=this.n;i<0;i+)temp*=0.1;this.dNow In put+=temp;this. n
15、-;this.tf1.setText(Double.toStri ng(this.dNow In put);/ key dotif(this.keyAvailable && e.getActionCommand()=".")if(this.alreadyHaveDot=false)this. nBitsNum+;this.alreadyHaveDot=true;this. n=-1;/key "+","-","*","/"if(this.keyAvailable &&am
16、p; e.getAct ion Comma nd()="+"| e.getAct ion Comma nd()="-"| e.getActionCommand()="*"| e.getActionCommand()="/")if(this.alreadyClickedEqueal)this.dNowI nput=this.dResult;this.isTempNowI nput=true;elseswitch(this .n Operati on)case1:this.dResult+=this.dNowl npu
17、t;break;case2:this.dResult-=this.dNowl nput;break;case3:this.dResult*=this.dNowl nput;break;case4:if(this.dNowl nput=O)tfl.setText(”除数不能为零”);this.keyAvailable=false;else this.dResult=this.dResult/this.dNowInput;if(this.keyAvailable)tf1.setText(Double.toStri ng(this.dResult);this.dNowI nput=O;if(e.ge
18、tActio nComma nd()="+")this .n Operati on=1;if(e.getActio nComma nd()="-")this .n Operati on=2;if(e.getActio nComma nd()="*")this .n Operati on=3;if(e.getActio nComma nd()="/")this .n Operati on=4;this. nBitsNum=O;this.alreadyClickedEqueal=false;/ key "+/
19、-"if(this.keyAvailable && e.getActionCommand()="+/_") this.dNowI nput=0-this.dNowl nput;tf1.setText(Double.toStri ng(this.dNowI nput);/ key "C"if(e.getActio nComma nd()="C")this. nBitsNum=O;this.dResult=0;this.dNowI nput=O;this.alreadyHaveDot=false;this. n=
20、0;this .n Operati on=1;this.keyAvailable=true;this.alreadyClickedEqueal=false;tf1.setText("0.");/ key "CE"if(e.getActio nComma nd()="CE")this. nBitsNum=O;this.dNowI nput=O;this.alreadyHaveDot=false;this. n=0;this .n Operati on=1;this.keyAvailable=true;tf1.setText("
21、0.");/ key "sqrt"if(this.keyAvailable && e.getActionCommand()="sqrt") if(this.alreadyClickedEqueal)if(this.dResult>=0)this.dResult=Math.sqrt(this.dResult);tf1.setText(Double.toStri ng(this.dResult);elsetf1.setText("函数输入无效”);this.keyAvailable=false;elseif(this
22、.dNowl nput>=0)this.dNow In put=Math.sqrt(this.dNow In put);tf1.setText(Double.toStri ng(this.dNow In put);elsetf1.setText("函数输入无效”);this.keyAvailable=false;/ key "1/x"if(this.keyAvailable && e.getActionCommand()="1/x")if(this.dNowl nput=O)tf1.setText("除数不能为零
23、”);this.keyAvailable=false;elsethis.dNowI nput=1/this.dNowl nput;tf1.setText(Double.toStri ng(this.dNow In put);/ key "="if(this.keyAvailable && e.getActionCommand()="=")this.alreadyClickedEqueal=true;switch(this .nO peratio n)case1:this.dResult+=this.dNowl nput;break;cas
24、e2:this.dResult-=this.dNowl nput;break;case3:this.dResult*=this.dNowl nput;break;case4:if(this.dNowI nput=0)tf1.setText("除数不能为零");this.keyAvailable=false;else this.dResult=this.dResult/this.dNowlnput;if(this.keyAvailable)tf1.setText(Double.toStri ng(this.dResult);/ key "MS"if(thi
25、s.keyAvailable && e.getActionCommand()="MS")this.dMemory=this.dNow In put;if(this.dMemory!=0)tf2.setText("M");/ key "MC"if(this.keyAvailable && e.getActionCommand()="MC")this.dMemory=0;tf2.setText("");/ key "MR"if(this.keyAv
26、ailable && e.getActionCommand()="MR")this.dNowI nput=this.dMemory;tf1.setText(Double.toStri ng(this.dNowI nput);/ key "M+"if(this.keyAvailable && e.getActionCommand()="M+")this.dMemory+=this.dNow In put;if(this.dMemory!=0)tf2.setText("M");else tf2.setText(""
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国微生态制剂药物行业市场深度分析及发展潜力预测报告
- 2025年度生态农业综合开发项目内部承包分包合同
- 2025年中国麻精药品行业市场全景分析及投资战略规划报告
- 2025年度建筑工程施工环境保护合同-@-6
- 2025年编制纸项目可行性研究报告
- 2025年木纹装饰面板项目投资可行性研究分析报告
- 2025年度高科技企业股份转让补充协议范本
- 提升学生体质健康水平实施过程中的问题与挑战
- 2025年度知识产权质押融资居间服务合同范本
- 2025年度酒店客房租赁合同及客房增值服务项目协议
- ## 外事领域意识形态工作预案
- CJJ 169-2012城镇道路路面设计规范
- 第八单元金属和金属材料单元复习题-2023-2024学年九年级化学人教版下册
- 钢铁是怎样炼成的保尔成长史
- 精神科护理技能5.3出走行为的防范与护理
- 煤矿机电运输培训课件
- 采购管理学教学课件
- 《供应商质量会议》课件
- 江苏省科技企业孵化器孵化能力评价研究的中期报告
- 供应商审核培训教程
- 酒精性肝硬化伴食管胃底静脉曲张破裂出血的护理查房
评论
0/150
提交评论