使用java写的一个简易的计算器,可以检测输入是否合法_第1页
使用java写的一个简易的计算器,可以检测输入是否合法_第2页
使用java写的一个简易的计算器,可以检测输入是否合法_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论