用java编写的计算器(Calculator)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjava.util.*;publicclassCalculator{publicstaticvoidmain(String[]args){newMyfirstCalculator();}}classMyfirstCalculator{Framef=newFrame(calculator);TextFieldtf=newTextField(50);ArrayListStringsa=newArrayListString();//sa用来存储sb中的数据StringBuildersb=newStringBuilder();//sb用来存储0-9中的数据StringBuildersb1=newStringBuilder();//用作判断,里面数据为:+,-,*,/四个中的任意一个或多个StringBuildersb2=newStringBuilder();//用作判断,里面数据为:=MyfirstCalculator(){init();}publicvoidinit(){Buttonb0=newButton(0);Buttonb1=newButton(1);Buttonb2=newButton(2);Buttonb3=newButton(3);Buttonb4=newButton(4);Buttonb5=newButton(5);Buttonb6=newButton(6);Buttonb7=newButton(7);Buttonb8=newButton(8);Buttonb9=newButton(9);Buttonba=newButton(=);Buttonbb=newButton(-);Buttonbc=newButton(+);Buttonbd=newButton(*);Buttonbe=newButton(/);//Buttonbf=newButton(D);//定义一个清空按钮,用来从头开始计数f.add(tf);f.add(b0);f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(b9);f.add(ba);f.add(bb);f.add(bc);f.add(bd);f.add(be);//f.add(bf);f.setBounds(400,400,300,300);f.setVisible(true);f.setLayout(newGridLayout(4,4));//下面的b[0-9]为按钮事件,让点击的按钮数字能够在TextField(tf)中能够显示b0.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());//在按=之后在按0-9之间的数字,就清空sb中的数据,从头开始计数}tf.setText();sb.append(0);//点击数字按钮,就在sb中加一个数字;tf.setText(sb.toString());//在tf中显示按钮所输入的数字}});b1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(1);tf.setText(sb.toString());}});b2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(2);tf.setText(sb.toString());}});b3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(3);tf.setText(sb.toString());}});b4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(4);tf.setText(sb.toString());}});b5.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(5);tf.setText(sb.toString());}});b6.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(6);tf.setText(sb.toString());}});b7.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(7);tf.setText(sb.toString());}});b8.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}tf.setText();sb.append(8);tf.setText(sb.toString());}});b9.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente)//下面同b0判断的方法一样{if(sb2.indexOf(=)!=-1){sb.delete(0,sb.length());}sb.append(9);tf.setText(sb.toString());}});//bc在此处为+事件,调动-(bb),*(bd),/(be)这三个中的其中一个算法bc.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){sa.add(sb.toString());//把sb中的数据即tf(TextField)中显示的数据添加到sa集合中,用于做加减乘除运算sb.delete(0,sb.length());//清空sb中的缓存,以便于sb存入tf中显示的下一个数字sb1.append(+);//当点击=按钮,+用作加法运算判断的标志sb2.delete(0,sb2.length());//清空sb2中的数据/*//此处为加法运算,--测试代码if(sb1.indexOf(+)!=-1)){intsum=0;for(Strings:sa){sum+=Integer.parseInt(s);}//sb1.append(+);System.out.println(+2...+sum);sa.clear();sa.add(+sum);System.out.println(sa);tf.setText(+sum);//sa.add(sb.toString());System.out.println(+...+sa);sb.delete(0,sb.length());sb1.delete(0,sb1.length());sb1.append(+);}*///此处为减法运算if(sb1.indexOf(-)!=-1){intsum=0;intcount=0;for(Strings:sa){if(count==0){sum=Integer.parseInt(s);//System.out.println(+sum);--测试代码}else{sum+=-(Integer.parseInt(s));}count++;}//System.out.println(-2...+sum);--测试代码sa.clear();sa.add(+sum);//System.out.println(sa);--测试代码tf.setText(+sum);//System.out.println(-...+sa);--测试代码sb.delete(0,sb.length());sb1.delete(0,sb1.length());sb1.append(+);}//此处为乘法运算if(sb1.indexOf(*)!=-1){intsum=1;for(Strings:sa){sum=sum*Integer.parseInt(s);}//System.out.println(*2...+sum);--测试代码sa.clear();sa.add(+sum);//System.out.println(sa);--测试代码tf.setText(+sum);//System.out.println(*...+sa);--测试代码sb.delete(0,sb.length());sb1.delete(0,sb1.length());sb1.append(+);}//此处为除法运算if(sb1.indexOf(/)!=-1){intsum=0;intcount=0;for(Strings:sa){if(count==0){sum=Integer.parseInt(s);}else{sum/=Integer.parseInt(s);}count++;}//System.out.println(-2...+sum);--测试代码sa.clear();sa.add(+sum);//System.out.println(sa);--测试代码tf.setText(+sum);//System.out.println(/...+sa);--测试代码sb.delete(0,sb.length());sb1.delete(0,sb1.length());sb1.append(+);}}});//ba在此处为=事件,调动-(bb),+(ba),*(bd),/(be),这四个中的其中一个算法ba.addActionListener(newActionLi

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功