【Java精品源码栏目提醒】:网学会员Java精品源码为您提供【精品】Egpqmgg课程设计报告(java版的计算器源代码) - 市场营销参考,解决您在【精品】Egpqmgg课程设计报告(java版的计算器源代码) - 市场营销学习中工作中的难题,参考学习。
生命中,不断地有人离开或进入。
于是,看见的,看不见的;记住的,遗忘了。
生命中,不断地有得到和失落。
于是,看不见的,看见了;遗忘的,记住了。
然而,看不见的,是不是就等于不存在?记住的,是不是永远不会消失?程序功能简介:从最基本的加、减、乘、除,到基本数学函数处理,再到数的进制转换处理。
代码如下/文件名:Calculator.
java 说明:简易科学计算器 /import
java.awt.import
java.awt.event.import javax.swing.public class Calculator extends Frame implements ActionListener WindowListener private Container container private GridBagLayout layout private GridBagConstraints constraints private JTextField displayField //计算结果显示区 private String lastCommand //保存-/命令0 private double result //保存计算结果 private boolean start //判断是否为数字的开始 private JMenuBar menubar private JMenuItem m_exitm2_ejzm2_bjz private Dialog dialog private Label label_dialog private JButton button_sqrtbutton_plusminusbutton_CEbutton_cancelbutton_1button_2 button_3button_4button_5button_6button_7button_8button_9button_0 button_plusbutton_minusbutton_multiplybutton_dividebutton_point button_equalbutton_logbutton_tanbutton_cosbutton_sinbutton_exp public Calculator //构造方法设置布局、为按钮注册事件监听器 superCalculator this.setLocation240200 this.setSize350300 this.setResizabletrue this.setLayoutnew GridLayout71 this.addmyMenu //调用成员方法添加菜单 displayFieldnew JTextField30 this.adddisplayField displayField.setEditabletrue starttrue result0lastCommand JPanel panel0new JPanelpanel0.setLayoutnew GridLayout1444JPanel panel1new JPanelpanel1.setLayoutnew GridLayout1544this.addpanel1button_sqrtnew JButtonsqrtbutton_plusminusnew JButton/-button_expnew JButtonexpbutton_CEnew JButton退格button_cancelnew JButtonCJPanel panel2new JPanelpanel2.setLayoutnew GridLayout1544this.addpanel2button_7new JButton7button_8new JButton8button_9new JButton9button_lognew JButtonlogbutton_dividenew JButton/JPanel panel3new JPanelpanel3.setLayoutnew GridLayout1544this.addpanel3button_4new JButton4button_5new JButton5button_6new JButton6button_tannew JButtontanbutton_multiplynew JButtonJPanel panel4new JPanelpanel4.setLayoutnew GridLayout1544this.addpanel4button_1new JButton1button_2new JButton2button_3new JButton3button_cosnew JButtoncosbutton_minusnew JButton-JPanel panel5new JPanelpanel5.setLayoutnew GridLayout1544this.addpanel5button_0new JButton0button_pointnew JButton.button_equalnew JButtonbutton_sinnew JButtonsinbutton_plusnew JButtonpanel1.addbutton_sqrtpanel1.addbutton_plusminuspanel1.addbutton_exppanel1.addbutton_CEpanel1.addbutton_cancelpanel2.addbutton_7panel2.addbutton_8panel2.addbutton_9panel2.addbutton_logpanel2.addbutton_dividepanel3.addbutton_4panel3.addbutton_5panel3.addbutton_6panel3.addbutton_tanpanel3.addbutton_multiplypanel4.addbutton_1panel4.addbutton_2panel4.addbutton_3panel4.addbutton_cospanel4.addbutton_minuspanel5.addbutton_0panel5.addbutton_pointpanel5.addbutton_equalpanel5.addbutton_sinpanel5.addbutton_plusbutton_sqrt.addActionListenerthisbutton_plusminus.addActionListenerthisbutton_exp.addActionListenerthisbutton_CE.addActionListenerthisbutton_cancel.addActionListenerthisbutton_7.addActionListenerthisbutton_8.addActionListenerthisbutton_9.addActionListenerthisbutton_log.addActionListenerthisbutton_divide.addActionListenerthisbutton_4.addActionListenerthisbutton_5.addActionListenerthisbutton_6.addActionListenerthisbutton_tan.addActionListenerthisbutton_multiply.addActionListenerthisbutton_1.addActionListenerthisbutton_2.addActionListenerthisbutton_3.addActionListenerthisbutton_cos.addActionListenerthisbutton_minus.addActionListenerthisbutton_0.addActionListenerthisbutton_point.addActionListenerthisbutton_equal.addActionListenerthis button_sin.addActionListenerthis button_plus.addActionListenerthis this.addWindowListenernew WinClose //注册窗口监听器 this.setVisibletrueprivate void addmyMenu //菜单的添加 JMenuBar menubarnew JMenuBar this.addmenubar JMenu m1new JMenu选项 JMenu m2new JMenu进制转换 JMenuItem m1_exitnew JMenuItem退出 m1_exit.addActionListenerthis JMenuItem m2_ejznew JMenuItem二进制 m2_ejz.addActionListenerthis JMenuItem m2_bjznew JMenuItem八进制 m2_bjz.addActionListenerthis JMenu m3 new JMenu 帮助 JMenuItem m3_Help new JMenuItem用法 m3_Help.addActionListenerthis dialog new Dialogthis提示true //模式窗口 dialog.setSize24080 label_dialog new LabelLabel.CENTER //标签的字符串为空,居中对齐 dialog.addlabel_dialog dialog.addWindowListenerthis //为对话框注册窗口事件监听器 m1.addm1_exit menubar.addm1 m2.addm2_ejz m2.addm2_bjz menubar.addm2 m3.addm3_Help menubar.addm3 public void actionPerformedActionEvent e //按钮的单击事件处理方法 ife.getSource.equalsbutton_1e.getSource.equalsbutton_2 e.getSource.equalsbutton_3e.getSource.equalsbutton_4 e.getSource.equalsbutton_5 e.getSource.equalsbutton_6 e.getSource.equalsbutton_7 e.getSource.equalsbutton_8 e.getSource.equalsbutton_9 e.getSource.equalsbutton_0 e.getSource.equalsbutton_pointe.getSource.equalsbutton_plusminus e.getSource.equalsbutton_cancele.getSource.equalsbutton_CE //非运算符的处理方法 String inpute.getActionCommand if start displayField.setText startfalse ifinput.equals/- displayField.setTextdisplayField.getText- ifinput.equals/- String strdisplayField.getText ifinput.equals退格 //退格键的实现方法 ifstr.length0 displayField.setTextstr.substring0str.length-1 else ifinput.equalsC //清零键的实现方法 displayField.setText0 starttrue else displayField.setTextdisplayField.getTextinput else if e.getActionCommand二进制 //二进制的转换 int nInteger.parseIntdisplayField.getText displayField.setTextInteger.toBinaryStringnelse if e.getActionCommand八进制 //八进制的转换 int nInteger.parseIntdisplayField.getText displayField.setTextInteger.toOctalStringnelse if e.getActionCommand退出 //选项中退出的处理方法 System.exit0else if e.getActionCommand用法 //按下帮助菜单栏中用法的处理方法 label_dialog.setTextsqrtexp等键是先输运算符再输数字n dialog.setLocation400250 dialog.setVisibletrueelse //各运算符的识别 String commande.getActionCommand ifstart lastCommandcommand else calculateDouble.parseDoubledisplayField.getText lastCommandcommand starttrue public void calculatedouble x //各运算符的具体运算方法 double d0 if lastCommand.equals result x else if lastCommand.equals- result-x else if lastCommand.equals resultx else if lastCommand.equals/ result/x else if lastCommand.equals resultx else if lastCommand.equalssqrt dMath.sqrtx resultd else if lastCommand.equalsexp dMath.expx resultd else if lastCommand.equalslog dMath.logx resultd else if lastCommand.equalstan dMath.tanx resultd else if lastCommand.equalscos dMath.cosx resultd else if lastCommand.equalssin dMath.sinx resultd displayField.setText result public void windowClosingWindowEvent e ife.getSourcedialog dialog.setVisiblefalse //隐藏对话框 else System.exit0 public void windowOpenedWindowEvent e public void windowActivatedWindowEvent e public void windowDeactivatedWindowEvent e public void windowClosedWindowEvent e public void windowIconifiedWindowEvent e public void windowDeiconifiedWindowEvent e public static void mainString args Calculator calculatornew Calculator class WinClose implements WindowListener public void windowClosingWindowEvent e //单击窗口关闭按钮时触发并执行实现窗口监听器接口 中的方法 System.exit0 //结束程序运行 public void windowOpenedWindowEvent e public void windowActivatedWindowEvent e public void windowDeactivatedWindowEvent e public void windowClosedWindowEvent e public void windowIconifiedWindowEvent e public void windowDeiconifiedWindowEvent e运行结果截图:程序功能实现: 1. 运用两个面板的叠加做出界面。
2. 通 过 在 按 钮 的 单 击 事 件 处 理 方 法 中 调 用 类 的 成 员 方 法 calculate()来进行简易计算器的各种运算,并正确实现运 算功能。
3. 调用Math包中的方法实现各函数功能。
4. 添加菜单条,列出‘选项’、 、 ‘进制转换’ ‘帮助’等菜单选 项,并分别实现‘选项’中‘退出’的功能,通过调用Integer 包中的方法实现二进制、 八进制的转换, ‘帮助菜单栏中 ‘用 法’的提示对话框。
5. 整个程序对话框可实现最小化、最大化、关闭。
设计体会: 在此次的课程设计实践中,我学到了教科书上没有的东西。
这次课程设计,我按照课程设计的要求,分块实现程序的功能。
在实践中我遇到许多的问题,但我都克服,最终我成功的完成了这次课程设计。
从中我充分理解了
Java面对对象的特性,并体会到
Java与C语言的不同之处 在实践的同时,我又从中成长了许多,特别是在对程序的编译和调试中掌握了许多的技巧和方法,为今后的其他计算机语言的学习奠定了一定的基础。
总结:(1)通过编写这个相对复杂的程序,让我认识解决实际问题前应现建立模型,再通过建立的模型使问题简单化,从而解决问题。
(2)这次编写的程序大部分是通过调用各包中的方法来实现的,这也让我充分理解了包和类调用的关系。
(3)由于这次编写的程序相对比较复杂,所以不能一次性编写、调试,也由此让我学会把程序分块,逐个编写、调试。