《Java科学计算器程序代码.docx》由会员分享,可在线阅读,更多相关《Java科学计算器程序代码.docx(11页珍藏版)》请在第一文库网上搜索。
1、计算器布局显示:功能举例:13+20=33-7*14=-98:先输入7,再单击-/+将其变成负数,然后依次单击X号、14与=7!=5040sin30o=0.5:先输入30,然后按下按钮转换成角度值,再按下Sin即可显示结果Sqrt(4)=2:利用鼠标点击4后再点击sqrt按键即可显示结果2*=6.2832源程序如下:importjava,awt.Bordcr1ayout;importjava.awt.Co1or;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Grid1ayout;importjava.awt.event.
2、ActionEvent;importjava.awt.event.Action1istener;importjava.text.Decima1Format;importjavax.swing.BorderFactorjf;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JCheckBoxMenuItem;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMe
3、nuItem;importjavax.swing.JPane1;importjavax.swing.JRadioButtonMcnuItem;importjavax.swing.JTextFie1d;pub1icc1assCa1ucatorextendsJFrameprivateJTextFie1dtf;privateJPane1pane11,pane12,pane13,pane14;privateJMenuBarmyBar;privateJMenumenu1,menu2,menu3;privateJMenuItemec1it1tem1,edit1tem2,he1p1,he1p2,he1p3;
4、privateJRadioBu11onMenuItemsee1tcm1,SeCItCm2;单选框privateJCheckBoxMenuItcmSeCItC1n3;复选框privateButtonGroupbgb;privateStringback;privateb1eanIfResu1t=true,f1ag=fa1se;privateStringoper=;privatedoub1eresu1t=O;privateNumnumAction1istener;privateDecima1Formatdf;pub1icCa1ucatorOSUPer(科学计算器”);设置标题栏df=newDCein
5、IaIFOr1nat(#.#);保留四位小数this.Set1ayout(newBorder1ayout(10,5);pane11=newJPane1(newGrid1ayout(1,3,10,10);pane12=newJPane1(newGrid1ayout(5,6,5,5);5行6列pane13=newJPane1(newGrid1ayout(5,1,5,5);pane14=newJPane1(newBorder1ayout(5,5);*菜单栏*/myBar=newJMenuBar();mcnu1=newJMenU(编辑(E)”);menu2=newJMCnU(查看(V);mcnu3=n
6、ewJMenU(帮助(H)”);menu1.sctFont(newFont(宋体,Font.P1AIN,12);menu2.setFont(newFont(宋体,Font.P1AIN112);menu3.setFont(newFOnt(宋体,Font.P1AIN,12);*编辑栏ec1it1tem1=newJMenU1tem(复制(C)Ctr1+C);edit1tcm2=newJMCnUIteIn(粘贴(P)Ctr1+V*);edit1tem1.setFont(newFont(宋体”,Font.P1AIN,12);edit1tem2.setFont(newFOnt(宋体,Font.P1AIN,
7、12);*查看栏*/see1tcm1=newJRadiOBUttOnMCnU1tCm(科学型(T)”);see1tcm2=newJRadiOBUttonMCnUItCm(标准型(S)”);seeItcm3=newJCheCkBoXMenUItein(数字分组(I)”);see1tcm1.setFont(newFOnt(宋体”,Font.P1AIN,12);see1tem2.setFont(newFont(宋体”,Font.P1AIN,12);see1tem3.setFont(newFont(宋体,Font.P1AIN,12);*帮助栏*/he1p1=newJMenUItem(帮助主题(H)”)
8、;he1p2=newJMCnUIten(关于计算器(A);hc1p1.setFont(newFOnt(宋体,Font.P1AINt12);he1p2.setFont(newFont(宋体,Font.P1AINt12);bgb=newBUttOnGroUP();选项组menu1.add(edit1tem1);menu1.add(edit1tem2);menu2.add(see1tem1);monu2.add(see1tem2);mcnu2.addScparator();添加一条分割线menu2.add(see1tem3);menu3.add(he1p1);mcnu3.addScparator()
9、;添加一条分割线menu3.add(he1p2);myBar.add(menu1);myBar.add(menu2);myBar.add(menu3);this.SetJMcnuBar(myBar);numAction1istener=newNU1n();实现数字监听*文本域,即为计算器的屏幕显示区域*/tf=newJTextFie1dO;tf.SetEditab1e(fa1se);文本区域不可编辑tf.SetBackground(Co1or,white);文本区域的背景色tf.SetHorizonta1A1ignmcnt(JTextFie1d.RIGHT);文字右对齐tf.SetTeXt(0
10、);tf.SetBordcr(BorderFactory.Create1owcrcdBevcIBordeiQ);init();对计算器进行初始化)*初始化操作*添加按钮*/privatevoidinit()addButton(pane11,addButton(pane11,addButton(pane11,“Backspace”,newC1ear(),Co1or,iocb;“CE”,newC1ear(),Co1or,red);C,newC1ear(),Co1or,red);addButton(pane12,addButton(pane12,addButton(panc12,addButton(
11、panc12,addButton(pane12,addButton(pane12,“1x”,newSigns(),Co1or,magenta);“1og”,newSigns(),Co1or,magenta);7”,numAction1istener,Co1or,b1ue);8”,numAction1istoner,Co1or,b1ue);9”,numAction1istener,Co1or,b1ue);“,newSigns(),Co1or,red);addButton(pane12,addButton(pane12,addButton(pane12,addButton(panc12,addBu
12、tton(panc12,addButton(pane12,n!newSigns(),Co1or,magenta);sqrt”,newSigns(),Co1or,magenta);4”,numAction1istencr,Co1or,b1ue);5”,numAction1istener,Co1or,b1ue);6”,numAction1istoner,Co1or,b1ue);X,newSigns(),Co1or,red);addButton(pane12,addButton(pane12,addButton(pane12,addButton(pane12,addButton(panc12,add
13、Button(panc12,newnewSigns()tCo1or.Inagenta);Signs(),Co1or.magenta);1,numAction1istencr,Co1or,b1ue);2”,numAction1istencr,Co1or,b1ue);3”,numAction1istener,Co1or,b1ue);一,newSigns(),Co1or,icd);addButton(pane12,addButton(panc12,addButton(panc12,addButton(pane12,addButton(pane12,addButton(pane12,“cos”,new
14、Signs(),Co1or,magenta);x3”,newSigns()tCo1or,magenta);0”,numAction1istoner,Co1or,b1ue);newC1ear(),Co1or,b1ue);newDot()tCo1or,b1ue)newSigns(),Co1or,ied);addButton(pane12,addButton(pane12,addButton(panc12,addButton(panc12,addButton(pane12,addButton(pane12,“tan”,newSigns(),Co1or,magenta);newSigns(),Co1o
15、r,magenta);“冗,numActiOn1istener,Co1or,orange);e,numAction1istoner,Co1or,orange);”,newSigns(),Co1or,orange);newSigns(),Co1or,red);JButtonbtns=newJBUttOn(“计算器”);btns.SetBorder(BorderFactory.Creatc1owcrcdBcvc!Border);btns.SCtEnabICd(fa1se);按钮不可操作btns.SetPreferredSize(newDimension(20,20);pane13.add(btns);加入按钮addButton(pane13,addBu