科学计算器程序Java代码

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

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

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

资源描述

MC:清除独立内存里的数字MR:读出独立内存数字MS:将显示数字保存到独立内存,并取代内存中原有数字M+:将显示数字向独立内存累加(独立内存是一个临时寄放数字的存储器,方便计算)CE:纠错键(CLEARERROR).输入数字有错,按它可以重新输入C:表示清零,将计算器恢复到开机状态功能举例:源程序如下:importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.text.DecimalFormat;importjavax.swing.BorderFactory;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JCheckBoxMenuItem;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JPanel;importjavax.swing.JRadioButtonMenuItem;importjavax.swing.JTextField;publicclassCalucatorextendsJFrame{privateJTextFieldtf;privateJPanelpanel1,panel2,panel3,panel4;privateJMenuBarmyBar;privateJMenumenu1,menu2,menu3;privateJMenuItemeditItem1,editItem2,help1,help2,help3;privateJRadioButtonMenuItemseeItem1,seeItem2;//单选框privateJCheckBoxMenuItemseeItem3;//复选框privateButtonGroupbgb;privateStringback;privatebooleanIfResult=true,flag=false;privateStringoper==;privatedoubleresult=0;privateNumnumActionListener;privateDecimalFormatdf;publicCalucator(){super(科学计算器);//设置标题栏df=newDecimalFormat(#.####);//保留四位小数this.setLayout(newBorderLayout(10,5));panel1=newJPanel(newGridLayout(1,3,10,10));panel2=newJPanel(newGridLayout(5,6,5,5));//5行6列panel3=newJPanel(newGridLayout(5,1,5,5));panel4=newJPanel(newBorderLayout(5,5));/***菜单栏*/myBar=newJMenuBar();menu1=newJMenu(编辑(E));menu2=newJMenu(查看(V));menu3=newJMenu(帮助(H));menu1.setFont(newFont(宋体,Font.PLAIN,12));menu2.setFont(newFont(宋体,Font.PLAIN,12));menu3.setFont(newFont(宋体,Font.PLAIN,12));/***编辑栏*/editItem1=newJMenuItem(复制(C)Ctrl+C);editItem2=newJMenuItem(粘贴(P)Ctrl+V);editItem1.setFont(newFont(宋体,Font.PLAIN,12));editItem2.setFont(newFont(宋体,Font.PLAIN,12));/***查看栏*/seeItem1=newJRadioButtonMenuItem(科学型(T));seeItem2=newJRadioButtonMenuItem(标准型(S));seeItem3=newJCheckBoxMenuItem(数字分组(I));seeItem1.setFont(newFont(宋体,Font.PLAIN,12));seeItem2.setFont(newFont(宋体,Font.PLAIN,12));seeItem3.setFont(newFont(宋体,Font.PLAIN,12));/***帮助栏*/help1=newJMenuItem(帮助主题(H));help2=newJMenuItem(关于计算器(A));help1.setFont(newFont(宋体,Font.PLAIN,12));help2.setFont(newFont(宋体,Font.PLAIN,12));bgb=newButtonGroup();//选项组menu1.add(editItem1);menu1.add(editItem2);menu2.add(seeItem1);menu2.add(seeItem2);menu2.addSeparator();//添加一条分割线menu2.add(seeItem3);menu3.add(help1);menu3.addSeparator();//添加一条分割线menu3.add(help2);myBar.add(menu1);myBar.add(menu2);myBar.add(menu3);this.setJMenuBar(myBar);numActionListener=newNum();//实现数字监听/***文本域,即为计算器的屏幕显示区域*/tf=newJTextField();tf.setEditable(false);//文本区域不可编辑tf.setBackground(Color.white);//文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐tf.setText(0);tf.setBorder(BorderFactory.createLoweredBevelBorder());init();//对计算器进行初始化}/***初始化操作*添加按钮*/privatevoidinit(){addButton(panel1,Backspace,newClear(),Color.red);addButton(panel1,CE,newClear(),Color.red);addButton(panel1,C,newClear(),Color.red);addButton(panel2,1/x,newSigns(),Color.magenta);addButton(panel2,log,newSigns(),Color.magenta);addButton(panel2,7,numActionListener,Color.blue);addButton(panel2,8,numActionListener,Color.blue);addButton(panel2,9,numActionListener,Color.blue);addButton(panel2,÷,newSigns(),Color.red);addButton(panel2,n!,newSigns(),Color.magenta);addButton(panel2,sqrt,newSigns(),Color.magenta);addButton(panel2,4,numActionListener,Color.blue);addButton(panel2,5,numActionListener,Color.blue);addButton(panel2,6,numActionListener,Color.blue);addButton(panel2,×,newSigns(),Color.red);addButton(panel2,sin,newSigns(),Color.magenta);addButton(panel2,x^2,newSigns(),Color.magenta);addButton(panel2,1,numActionListener,Color.blue);addButton(panel2,2,numActionListener,Color.blue);addButton(panel2,3,numActionListener,Color.blue);addButton(panel2,-,newSigns(),Color.red);addButton(panel2,cos,newSigns(),Color.magenta);addButton(panel2,x^3,newSigns(),Color.magenta);addButton(panel2,0,numActionListener,Color.blue);addButton(panel2,-/+,newClear(),Color.blue);addButton(panel2,.,newDot(),Color.blue);addButton(panel2,+,newSigns(),Color.red);addButton(panel2,tan,newSigns(),Color.magenta);addButton(panel2,%,newSigns(),Color.magenta);addButton(panel2,π,numActionListener,Color.orange);addButton(panel2,e,numActionListener,Color.orange);addButton(panel2,′″,newSigns(),Color.orange);addButton(panel2,=,newSigns(),Color.red);JButtonbtns=newJButton(计算器);btns.setBorder(BorderFactory.createLoweredBevelBorder());btns.setEnabled(false);//按钮不可操作btns.setPreferredSize(newDimension(20,20));panel3.add(btns);//加入按钮addButton(panel3,MC,null,Color.red);addButton(panel3,MR,null,Color.red);addButton(panel3,MS,null,Color.red);addButton(panel3,M+,null,Color.red);panel4.add(panel1,BorderLayout.NORTH);panel4.add(panel2,BorderLayout.CENTER);this.add(tf,BorderLayout.NORTH);this.add(panel3,BorderLayout.WEST);this.add(panel4);pack();this.setResizable(false);//窗口不可改变大小this.setLocation(300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/***统一设置按钮的的使用方

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

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

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

×
保存成功