下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/*使用java编写的简易计算器,可以实现+,-,*,/四则运算。注意:括号没有作用,就是一个装饰。待日后有时间定会更新。DEL是退格,CLC是清空。并带有日期,输入语法检测功能*/importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Container;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListene
2、r;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.ScrollPaneConstants;importjavax.swing.Timer;publicclassCalculatorextendsJFrameimplement
3、sActionListenerprivateStringoperandString=""publicstaticCalculatorcal;privateJTextAreatextArea;privateJPanelpanel;privateContainercontainer;privatedoubleoperand=newdouble100;privateintoperandCount;privateStringoperator=newString100;privateintoperatorCount;privateJScrollPanejScrollPane;publ
4、icCalculator(Stringtitle)super(title);publicCalculator()operand=newdouble200;operator=newString100;cal=newCalculator("计算器");cal.setTitle("Calculator");container=cal.getContentPane();container.setBackground(null);cal.setBackground(newColor(100,250,150);cal.setSize(320,400);cal.set
5、Location(100,100);container.setLayout(newGridLayout(0,1,2,1);newDateApplication().getNewInstance("");showComponent();cal.setDefaultCloseOperation(EXIT_ON_CLOSE);cal.setResizable(false);cal.setVisible(true);privatevoidshowComponent()/container.add(newJTextArea(40,20),BorderLayout.NORTH);Str
6、ingtextString="789+456-123*0./="textArea=newJTextArea(1,2);jScrollPane=newJScrollPane(textArea);System.out.println("jScrollPane="+jScrollPane);textArea.setBackground(Color.CYAN);textArea.setForeground(Color.BLACK);textArea.setEditable(false);textArea.setFont(newFont("",
7、Font.BOLD,30);textArea.setWrapStyleWord(true);textArea.setLineWrap(true);jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);/jScrollPane.setViewportView(textArea);container.add(jScrollPane);panel=newJPanel();panel.setLayout(newGridLayout(5,4);for(inti=0;i<tex
8、tString.length();i+)addButton(textString.substring(i,i+1),panel);StringleftBracket="("StringrightBracket=")"Stringback="DEL"StringsetNull="CLC"/panel.add(newJButton(leftBracket);/panel.add(newJButton(rigthBracke);/panel.add(newJButton(back);/panel.add(newJButt
9、on(setNull);addButton(leftBracket,panel);addButton(rightBracket,panel);addButton(back,panel);addButton(setNull,panel);container.add(panel);privatevoidaddButton(Stringstr,JPanelpanel2)/TODOAuto-generatedmethodstubButtonbutton=newButton(str);button.setFont(newFont("",Font.BOLD,14);if(button.
10、getLabel().equals(".")|button.getLabel().equals("*")button.setFont(newFont("",Font.BOLD,30);if(button.getLabel().equals("-")button.setFont(newFont("",Font.BOLD,24);button.setBackground(Color.GREEN);button.addActionListener(this);panel2.add(button);pu
11、blicstaticvoidmain(Stringargs)newCalculator();OverridepublicvoidactionPerformed(ActionEvente)textArea.setForeground(Color.BLACK);Stringstr=e.getActionCommand();/System.out.println(str);/if(operandString=null|operandString.contains(s)if(operandString.contains("=")operandString=""t
12、extArea.setText(operandString);operand=newdouble200;operator=newString200;operandCount=0;operatorCount=0;if(str.equals("(")|str.equals(")")return;if(str.equals("DEL")if(operandString.length()>0)operandString=operandString.substring(0,operandString.length()-1);textAre
13、a.setText(operandString);return;if(str.equals("CLC")operandString=""textArea.setText(operandString);return;operandString=operandString+str;System.out.println(operandString);operandString.trim();textArea.setText(operandString);if(str.equals("=")handleString(operandString
14、);publicvoidhandleString(StringalString)intbeginIndex=0;Stringtemp=null;for(inti=0;i<alString.length();i+)if(containsOperator(alString.substring(i,i+1)operatoroperatorCount=alString.substring(i,i+1);operatorCount+;if(operatorCount=1)temp=alString.substring(0,i);beginIndex=i;elsetemp=alString.subs
15、tring(beginIndex+1,i);beginIndex=i;checkLegal(temp);/for(inti=0;i<operatorCount;i+)/System.out.println(operatori);countResult();publicbooleancontainsOperator(Stringsubstring)/TODOAuto-generatedmethodstubStringstr="+-*/="if(str.contains(substring)returntrue;returnfalse;publicvoidcheckLeg
16、al(Stringstr)doubletempDouble=0;trytempDouble=Double.parseDouble(str);operandoperandCount=tempDouble;operandCount+;catch(Exceptione)JOptionPane.showMessageDialog(null,"输入有错");operandString=operandString+"="textArea.setText(operandString);publicvoidcountResult()doubletempResult;fo
17、r(inti=0;i<operatorCount-1;i+)if(operatori.equals("*")tempResult=operandi*operandi+1;backFunc(tempResult,i);i-;elseif(operatori.equals("/")trytempResult=operandi/operandi+1;catch(Exceptione)JOptionPane.showMessageDialog(null,"除数不能为0");operandString=operandString+&quo
18、t;="return;backFunc(tempResult,i);i-;tempResult=operand0;for(inti=0;i<operandCount;i+)if(operatori.equals("+")tempResult+=operandi+1;if(operatori.equals("-")tempResult-=operandi+1;if(operatori.equals("=")break;intsimilarInteger=Calculator.round(tempResult);textArea.setForeground(Color.RED);if(Math.abs(tempResult-similarInteger)<Math.pow(10,-7)operandString+=similarInteger;System.o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年危险品模拟试题题及参考答案
- 2026年体育专业复模拟试题及参考答案
- 吉林市2026年及参考答案民生银行招聘模拟试题及参考答案
- 继续医学教育-医疗机构结核病防控策略与实践模拟试题及参考答案
- 2026年北京对外经济贸易大学后勤处非事业编制人员招聘模拟考试题及参考答案
- 2027年法律常识测试卷刑法分则重点罪名解析
- 苏教版高二数学第九十八章数列求和练习题
- GB 30184-2026《建筑防水卷材和摩擦材料单位产品能源消耗限额》
- Solifenacin-impurity-10-生命科学试剂-MCE
- 广东江门市2026-2027学年八年级上学期11月期中英语试题(含答案)
- 金鱼繁殖后代的研究报告
- 学校消防安全评估指南
- DB37∕T 4859-2025 强制隔离戒毒诊断评估规范
- 下肢静脉曲张课件讲解
- 煤矿机电基本知识培训课件
- 数学阅读教学课件
- 五音入五脏课件
- 学堂在线 不朽的艺术:走进大师与经典 章节测试答案
- 诊断学讲义 常用临床免疫学检查学习资料
- 三年级数学上册苏教版《解决问题的策略-从条件出发》听评课记录
- 【MOOC】研究生英语科技论文写作-北京科技大学 中国大学慕课MOOC答案
评论
0/150
提交评论