您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 设计及方案 > JAVA模拟科学计算器
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCaextendsJFrameimplementsActionListener{//***********************************************************************//标准型计算器控件键钮staticJFramef1=newJFrame(计算器);privateStringKEYS[]={7,8,9,/,sqrt,4,5,6,*,%,1,2,3,-,1/x,0,+/-,.,+,=};privateStringM[]={,MC,MR,MS,M+};privateStringCOM[]={Backspace,CE,C};privateJButtonkeys[]=newJButton[KEYS.length];privateJButtoncom[]=newJButton[COM.length];privateJButtonm[]=newJButton[M.length];privateJTextFieldresult,hh,jj;MenuBarmenubar1=newMenuBar();Menuedit=newMenu(编辑(E)),view=newMenu(查看(V)),help=newMenu(帮助(H));MenuItemcopy=newMenuItem(复制(C)),paste=newMenuItem(粘贴(P)),standar=newMenuItem(标准型(T)),science=newMenuItem(科学型(S)),numgroup=newMenuItem(数字分组(I)),helpt=newMenuItem(帮助主题(H)),about=newMenuItem(关于计算器(A));privatebooleanfirstDigit=true;//判断第一个输入的字符是否是数字privatedoubleresultNum=0.0,resultNum2=0.0;//中间结果privateStringoperator==,co,str,operator2==;privatebooleanoperateValidFlag=true;//***********************************************************************//科学性计算器控件及按钮staticJFramef2=newJFrame(科学计算器);privateJTextFieldresult2;privateString[]KON={sta,F-E,(,),MC,Ave,dms,Exp,In,MR,Sum,sin,x^y,log,MS,s,cos,x^3,n!,M+,Dat,tan,x^2,1/x,pi};privateString[]KO={7,8,9,/,Mod,And,4,5,6,*,Or,Xor,1,2,3,-,Lsh,Not,0,+/-,.,+,=,Int,A,B,C,D,E,F};privateStringITM[]={标准型,科学型,十六进制数(H)F5,十进制(D)F6,八进制(O)F7,二进制(B)F8,四字(Q)F12,DWORD值(R)F2,单字(W)F3,字节(Y)F4,数字分组(I)};privatefinalString[]COMM={Backspace,CE,C};privateJButtoncomm[]=newJButton[COMM.length];privateJButtonkon[]=newJButton[KON.length];privateJButtonko[]=newJButton[KO.length];privateMenuItemitm[]=newMenuItem[ITM.length];privateJPanelpanel0,panel1,panel2,panel3,panel4,panel5,panel6;Checkboxcheckbox1,checkbox2,checkbox3,checkbox4;MenuBarmenubar2=newMenuBar();//添加菜单条Menuedit2=newMenu(编辑(E)),view2=newMenu(查看(V)),help2=newMenu(帮助(H));MenuItemcopy2=newMenuItem(复制(C)Ctrl+C),past2=newMenuItem(粘贴(P)Ctrl+V),standar2=newMenuItem(标准型(T)),science2=newMenuItem(科学型(S)),numgroup2=newMenuItem(数字分组(I)),menuitem1=newMenuItem(十六进制数(H)F5),menuitem2=newMenuItem(十进制(D)F6),menuitem3=newMenuItem(八进制(O)F7),menuitem4=newMenuItem(二进制(B)F8),menuitem5=newMenuItem(四字(Q)F12),menuitem6=newMenuItem(DWORD值(R)F2),menuitem7=newMenuItem(单字(W)F3),menuitem8=newMenuItem(字节(Y)F4),helpt2=newMenuItem(帮助主题(H)),about2=newMenuItem(关于计算器(A));//***********************************************************************//构造函数publicCa(){//标准型开始初始化********************result=newJTextField(0.);result.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐result.setEditable(false);//设置为不可编辑result.setBackground(Color.WHITE);//设置颜色为白色JPanelk0=newJPanel();k0.setLayout(newGridLayout(4,5,5,5));for(inti=0;iKEYS.length;i++){keys[i]=newJButton(KEYS[i]);k0.add(keys[i]);keys[i].setForeground(Color.blue);}keys[3].setForeground(Color.red);keys[8].setForeground(Color.red);keys[13].setForeground(Color.red);keys[18].setForeground(Color.red);keys[19].setForeground(Color.red);JPanelc0=newJPanel();c0.setLayout(newGridLayout(1,3,5,5));for(inti=0;iCOM.length;i++){com[i]=newJButton(COM[i]);c0.add(com[i]);com[i].setForeground(Color.red);}JPanelm0=newJPanel();m0.setLayout(newGridLayout(5,1,5,5));for(inti=0;iM.length;i++){m[i]=newJButton(M[i]);m0.add(m[i]);m[i].setForeground(Color.red);}m[0].setForeground(Color.black);//f1的监视器for(inti=0;iKEYS.length;i++){keys[i].addActionListener(this);}for(inti=0;iCOM.length;i++){com[i].addActionListener(this);}for(inti=0;iM.length;i++){m[i].addActionListener(this);}//复制事件copy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){intcount;count=str.length();if(count!=0)co=result.getText();elseSystem.out.println(没有可复制的对象);}});//粘贴事件paste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){str=co;result.setText(str);}});//复制事件copy2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){intcount;count=str.length();if(count!=0)co=result2.getText();elseSystem.out.println(没有可复制的对象);}});//粘贴事件past2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){str=co;result2.setText(str);}});edit.add(copy);edit.add(paste);view.add(standar);view.add(science);view.addSeparator();view.add(numgroup);help.add(helpt);help.addSeparator();help.add(about);menubar1.add(edit);menubar1.add(view);menubar1.add(help);//整体布局JPanelp1=newJPanel();p1.setLayout(newBorderLayout());p1.add(North,c0);p1.add(Center,k0);JPanelp2=newJPanel();p2.setLayout(newBorderLayout());p2.add(Center,result);f1.setLayout(newBorderLayout(3,5));f1.add(North,p2);f1.add(Center,p1);f1.add(West,m0);f1.setBackground(Color.LIGHT_GRAY);f1.setLocation(200,200);f1.setMenuBar(menubar1);f1.setVisible(true);f1.setResizable(false);//不可最大化f1.pack();//紧凑f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//标准型初始化结束//****************************************************standar2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){f1.setVisible(true);f2.setVisible(false);//按钮不可操作}});science.addActionLi
本文标题:JAVA模拟科学计算器
链接地址:https://www.777doc.com/doc-5144766 .html