您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > Java简单计算器实验报告
简单计算器(Java)目录运行界面..............................................................................2实验代码..............................................................................2运行界面实验代码packageComputer;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassMyComputerextendsJFrameimplementsActionListener{JTextAreamemoryArea=newJTextArea(,1,3);JTextAreadispresult=newJTextArea(0.,1,20);JButtonclear=newJButton(C);JButtonjbuttons[]=newJButton[24];//***doubleresult=0,first=0,second=0;doublememery=0;charfirstsymbol='\0',secondsymbol='\0';booleanprev=true,repeat=true,dot=true;MyComputer(){//定义显示样式super(Calculator);try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){System.out.print(e);}JPanelresultField=newJPanel();//放置结果、清除键、保存标识的区域,上半部分JPanelbuttonField=newJPanel();//放置按键,下半部分Containerall=getContentPane();//组合上述两部分GridLayoutgrid1=newGridLayout(4,6,3,3);//按键memoryArea.setEditable(false);clear.addActionListener(this);dispresult.setEditable(false);resultField.add(memoryArea);resultField.add(clear);resultField.add(dispresult);all.setLayout(newFlowLayout());all.add(resultField);Stringbuttonname[]={sin,MC,0,1,2,+,cos,MR,3,4,5,-,tan,MS,6,7,8,x,+/-,M+,9,.,=,/};buttonField.setLayout(grid1);for(inti=0;i6;i++){for(intj=0;j4;j++){jbuttons[i*4+j]=newJButton(buttonname[i*4+j]);jbuttons[i*4+j].addActionListener(this);//为按键注册监听器buttonField.add(jbuttons[i*4+j]);}}all.add(buttonField);setSize(400,210);setResizable(true);setVisible(true);}publicvoidpressNumber(Stringn){if(prev){dispresult.setText(n);prev=false;}elsedispresult.append(n);}publicbooleandivide(doubled){if(d==0){dispresult.setText(除数不能为0!);prev=true;repeat=true;firstsymbol='\0';secondsymbol='\0';returntrue;}returnfalse;}publicvoidactionPerformed(ActionEvente){Objectsource=e.getSource();//判断事件源,进行相应处理if(source==clear){dispresult.setText(0.);firstsymbol='\0';secondsymbol='\0';prev=true;repeat=true;dot=true;return;}if(source==jbuttons[0]){doubletemp=Math.sin(Double.parseDouble(dispresult.getText()));dispresult.setText(String.valueOf(temp));prev=true;repeat=false;dot=true;return;}if(source==jbuttons[1]){memery=0;memoryArea.setText();prev=true;repeat=false;dot=true;return;}if(source==jbuttons[2]){pressNumber(0);repeat=false;return;}if(source==jbuttons[3]){pressNumber(1);repeat=false;return;}if(source==jbuttons[4]){pressNumber(2);repeat=false;return;}if(source==jbuttons[6]){doubletemp=Math.cos(Double.parseDouble(dispresult.getText()));dispresult.setText(String.valueOf(temp));prev=true;repeat=false;dot=true;return;}if(source==jbuttons[7]){if(memoryArea.getText().equals(M))dispresult.setText(String.valueOf(memery));prev=true;repeat=false;dot=true;return;}if(source==jbuttons[8]){pressNumber(3);repeat=false;return;}if(source==jbuttons[9]){pressNumber(4);repeat=false;return;}if(source==jbuttons[10]){pressNumber(5);repeat=false;return;}if(source==jbuttons[12]){doubletemp=Math.tan(Double.parseDouble(dispresult.getText()));dispresult.setText(String.valueOf(temp));prev=true;repeat=false;dot=true;return;}if(source==jbuttons[13]){memery=Double.parseDouble(dispresult.getText());if(memery!=0)memoryArea.setText(M);prev=true;repeat=false;dot=true;return;}if(source==jbuttons[14]){pressNumber(6);repeat=false;return;}if(source==jbuttons[15]){pressNumber(7);repeat=false;return;}if(source==jbuttons[16]){pressNumber(8);repeat=false;return;}if(source==jbuttons[18]){doubletemp=-Double.parseDouble(dispresult.getText());dispresult.setText(String.valueOf(temp));prev=true;repeat=false;dot=true;return;}if(source==jbuttons[19]){memery+=Double.parseDouble(dispresult.getText());if(memery!=0)memoryArea.setText(M);prev=true;repeat=false;dot=true;return;}if(source==jbuttons[20]){pressNumber(9);repeat=false;return;}if(source==jbuttons[21]){if(dot){pressNumber(.);dot=false;repeat=false;}return;}if(source==jbuttons[22]){second=Double.parseDouble(dispresult.getText());dot=true;switch(secondsymbol){case'*':second*=first;break;case'/':if(divide(second))return;second=first/second;}//endofswitch(secondsymbol)secondsymbol='\0';switch(firstsymbol){case'+':result+=second;break;case'-':result-=second;break;case'*':result*=second;dispresult.setText(String.valueOf(result));break;case'/':if(divide(second))return;result/=second;}//endofswitch(firstsymbol)if(firstsymbol!='\0')dispresult.setText(String.valueOf(result));firstsymbol='\0';prev=true;repeat=false;return;}if(source==jbuttons[5]){dot=true;if(repeat){firstsymbol='+';return;}second=Double.parseDouble(dispresult.getText());switch(secondsymbol){case'*':second*=first;break;case'/':if(divide(second))return;second=first/second;}//endofswitch(secondsymbol)secondsymbol='\0';switch(firstsymbol){case'\0':result=second;firstsymbol='+';break;case'+':result+=second;dispresult.setText(String.valueOf(result));break;case'-':result-=second;firstsymbol='+';dispresult.setText(String.valueOf(result));break;case'*':result*=second;firstsymbol='+';dispresult.setText(String.valueOf(result));break;case'/':if(divide(second))return;resul
本文标题:Java简单计算器实验报告
链接地址:https://www.777doc.com/doc-5649839 .html