您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > Java科学计算器程序代码
09计61_013刘晨旭SyJ_4计算器布局显示:功能举例:①13+20=33②-7*14=-98:先输入7,再单击-/+将其变成负数,然后依次单击×号、14与=③7!=5040④sin30°=0.5:先输入30,然后按下′″按钮转换成角度值,再按下sin即可显示结果⑤sqrt(4)=2:利用鼠标点击4后再点击sqrt按键即可显示结果⑥2*π=6.2832……源程序如下: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(JFr
本文标题:Java科学计算器程序代码
链接地址:https://www.777doc.com/doc-4222185 .html