//计算器packagecom.gui;importjava.awt.*;importjavax.swing.*;importjavax.swing.border.Border;importjavax.swing.text.AbstractDocument.BranchElement;importjava.awt.event.*;publicclassCalculatorextendsJFrameimplementsActionListener,KeyListener{//创建组件MenuBarbar;Menumenu_Look,menu_Edie,menu_Help;MenuItemitem_Biaozhun,item_Kexue;MenuItemitem_Copy,item_PastItem;MenuItemitem_LookHelp,item_About;JTextFieldxianshi;JButton[]numButtons,functionButtons,signButtons;JPaneljPanel;Fontfont=newFont(Serif,Font.BOLD,20);//设置字号booleanroot=false;//记录是否有过操作StringnumString1;//作为字符串和double之间的中间转换着StringnumstrString2;doublenum_1,num_2;doubleresult_double;//接收计算结果Stringresult_string;//将结果转化成字符串StringysfString=+;Stringms=newString();//用于MS键位接收数据doubledbms;//用来接收转换后的MS值doubledbm;//用来接收M+的值Stringscpy;//用来接收拷贝的数据publicCalculator(){super(计算器);bar=newMenuBar();menu_Look=newMenu(查看[V]);menu_Edie=newMenu(编辑[E]);menu_Help=newMenu(帮助[H]);item_Biaozhun=newMenuItem(标准型);item_Kexue=newMenuItem(科学型);item_Copy=newMenuItem(复制);item_PastItem=newMenuItem(粘贴);item_LookHelp=newMenuItem(查看帮助);item_About=newMenuItem(关于计算器);xianshi=newJTextField(0,12);xianshi.setFont(font);xianshi.setEditable(false);xianshi.setHorizontalAlignment(SwingConstants.RIGHT);jPanel=newJPanel();jPanel.setLayout(null);//菜单的添加menu_Look.add(item_Biaozhun);menu_Look.add(item_Kexue);menu_Edie.add(item_Copy);menu_Edie.add(item_PastItem);menu_Help.add(item_LookHelp);menu_Help.add(item_About);bar.add(menu_Look);bar.add(menu_Edie);bar.add(menu_Help);setMenuBar(bar);//将下拉菜单添加到组件//主操作区xianshi.setBounds(25,10,200,30);//功能键String[]strings={MC,MR,MS,M+,M-,←,CE,C,±,√,7,8,9,/,%,4,5,6,*,1/x,1,2,3,-,=,0,.,+};functionButtons=newJButton[strings.length];intj=2;for(inti=0;istrings.length;i++){functionButtons[i]=newJButton(strings[i]);functionButtons[i].addActionListener(this);functionButtons[i].setMargin(newInsets(0,0,0,0));if(i5){functionButtons[i].setBounds(25+40*i,50,35,30);}elseif(i=5&&i10){functionButtons[i].setBounds(25+40*(i-5),90,35,30);}elseif(i=10&&i15){functionButtons[i].setBounds(25+40*(i-10),130,35,30);}elseif(i=15&&i20){functionButtons[i].setBounds(25+40*(i-15),170,35,30);}elseif(i=20&&i24){functionButtons[i].setBounds(25+40*(i-20),210,35,30);}if(i==24){functionButtons[i].setBounds(25+40*(i-20),210,35,70);}if(i==25){functionButtons[i].setBounds(25,250,75,30);}elseif(i==26|i==27){functionButtons[i].setBounds(25+j*40,250,35,30);j++;}jPanel.add(functionButtons[i]);}item_Biaozhun.addActionListener(this);item_Kexue.addActionListener(this);item_Copy.addActionListener(this);item_About.addActionListener(this);item_LookHelp.addActionListener(this);item_PastItem.addActionListener(this);jPanel.add(xianshi);add(jPanel);}publicstaticvoidmain(String[]args){Calculatorcalculator=newCalculator();calculator.setSize(250,350);calculator.setLocationRelativeTo(null);calculator.setVisible(true);calculator.setResizable(false);calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@OverridepublicvoidkeyTyped(KeyEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidkeyPressed(KeyEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidkeyReleased(KeyEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubStringxin=e.getActionCommand();//接受键盘Stringold=xianshi.getText();//显示框原有数据//0-9键盘if(0123456789.indexOf(xin)!=-1){if(root){xianshi.setText(old+xin);}else{xianshi.setText(xin);root=true;}}//运算符键盘elseif(+-*/.indexOf(xin)!=-1){//判断选择的运算符,numString1=old;//选择运算符后,将运算符之前的数据保存,ysfString=xin;//重置ysfString,将选择的晕算符赋值给ysfStringStringlinshiString=old+xin;xianshi.setText(linshiString);root=false;}//小数点键elseif(..equals(xin)){//判断输入的是不是“.”if(old.indexOf(xin)==-1){//判断显示区域是否已经有“.”xianshi.setText(old+xin);root=true;}}//正负号键elseif(±.equals(xin)){if(old.startsWith(-)){//判断输入的字符串如果是以“-”开头,就只是输出符号后面的字符串xianshi.setText(old.substring(1));}else{xianshi.setText(-+old);}}//1/x键位elseif(1/x.equals(xin)){num_1=Double.parseDouble(old);if(num_1==0){xianshi.setText(除数不能为零);root=false;}else{result_double=1/num_1;result_string=Double.toString(result_double);xianshi.setText(result_string);root=false;}}//%键位elseif(%.equals(xin)){doubledb=Double.parseDouble(xianshi.getText());db=db/100;Stringsrg=Double.toString(db);xianshi.setText(srg);root=false;}//=键位elseif(=.equals(xin)){num_1=Double.parseDouble(numString1);num_2=Double.parseDouble(xianshi.getText());if(numString1.equals()||xianshi.getText().equals()){System.out.print(没有数据。);}if(ysfString==+){result_double=num_1+num_2;result_string=Double.toString(result_double);xianshi.setText(result_string);}elseif(ysfString==-){result_double=num_1-num_2;result_string=Double.toString(result_double);xianshi.setText(result_string);}elseif(ysfString==*){result_double=num_1*num_2;result_string=Double.toString(result_double);xianshi.setText(result_string);}elseif(ysfString==/){if(num_2==0){xianshi.setText(除数不能为零!);}else{result_double=num_1/num_2;result_string=Double.toString(result_double);xianshi.setText(result_string);}}root=false;}//CE键位el