ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:106.85KB ,
资源ID:23439526      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/23439526.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java课程设计简易计算器.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java课程设计简易计算器.docx

1、Java课程设计简易计算器 面向对象课程设计(Java) 题 目:简易计算器 专 业:电子商务 班 级:1110025 学生姓名: 指导老师: 2013 年 1月 6日 简易计算器设计一、设计内容 设计一个图形界面(GUI)的应用程序,完成简单的运算。通过计算器的编辑来检测和提升自己的java能力,并最终完成课程设计报告。 二、设计要求 1、应用自己所学课程知识完成对计算器的基本任务。 2、查阅相关资料,学习和掌握项目中涉及的新知识,提高自学能力。 3、通过应用java程序编写计算器来提升对简单的图形界面的了解和掌握。三、总体设计 该计算器基本运算没有问题,清零、正负号、求倒数、退格功能都能很

2、好的实现,总体能完成一个计算器的基本功能,但仍有许多地方需要改进,比如小数点的实现所存在的一些问题,虽然在基本的运算过程当中不会造成太大影响,但这依然不能认为是一个很好的计算器,同时,在另一方面,该计算器还没能很好的实现连续计算的功能,必须每次按下等号按钮计算出结果后才能用产生的结果接着进行下一次的计算,改进的方法是在运算符上同时注册Result类,让运算符同时拥有计算结果的功能。 四、具体设计 1、程序流程图:2、主要代码展示及说明:总体代码的设计:程序主窗体继承JFrame类,使用24个JBotton按钮实现数字和基本功能,并且分别注册监听,采用6行4列网格布局,完成计算器界面的基本设置,

3、最后对按钮进行计算分析,计算并显示结果。这段代码主要是对程序的主框架进行相应的设置super(简易计算器); setSize(300,260); this.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout(1,1); /一行一列的布局 jPanel2=new JPanel(); jPanel2.setLayout(new GridLayout(6,4); /六行四列的布局 resultField=new JTextField(0); jPanel1.add(resultFie

4、ld); this.add(jPanel1,BorderLayout.NORTH); this.add(jPanel2,BorderLayout.CENTER);设置数字按钮1到9并添加监控以及符号等的添加监控 s1=new JButton( 1 ); s1.addActionListener(this); s2=new JButton( 2 ); s2.addActionListener(this); s3=new JButton( 3 ); s3.addActionListener(this); s4=new JButton( 4 ); s4.addActionListener(this)

5、; s5=new JButton( 5 ); s5.addActionListener(this); s6=new JButton( 6 ); s6.addActionListener(this); s7=new JButton( 7 ); s7.addActionListener(this); s8=new JButton( 8 ); s8.addActionListener(this); s9=new JButton( 9 ); s9.addActionListener(this); s0=new JButton( 0 ); s0.addActionListener(this); b1=n

6、ew JButton( + ); b1.addActionListener(this); b2=new JButton( - ); b2.addActionListener(this); b3=new JButton( * ); b3.addActionListener(this); b4=new JButton( / ); b4.addActionListener(this); b5=new JButton( ); b5.addActionListener(this); b6=new JButton( 开方 ); b6.addActionListener(this); b7=new JBut

7、ton( % ); b7.addActionListener(this); b8=new JButton( x! ); b8.addActionListener(this); b9=new JButton( 1/x ); b9.addActionListener(this); f1=new JButton( . ); f1.addActionListener(this); f2=new JButton( = ); f2.addActionListener(this); f3=new JButton( C ); f3.addActionListener(this); f4=new JButton

8、( 负 ); f4.addActionListener(this); f5=new JButton( 退格 ); f5.addActionListener(this); jPanel2.add(f3); jPanel2.add(b4); jPanel2.add(b3); jPanel2.add(f5); jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b1); jPanel2.add(s4); jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add

9、(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b5); jPanel2.add(s0); jPanel2.add(f1); jPanel2.add(f4); jPanel2.add(b6); jPanel2.add(b7); jPanel2.add(b8); jPanel2.add(b9); jPanel2.add(f2);(3)数据输入用方法num(int i)设计如下:public void num(int i) String s = null; s=String.valueOf(i); /返回整数i的字符串表示形式 if(end)

10、 /如果数字输入结束,则将文本框置零,重新输入 resultField.setText(0); end=false; if(resultField.getText().equals(0) /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s); else /如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(str); 以及用JFrame的方法actionPerformed(ActionEvent e)来获取数据(4)符号运算及清零、退格的设计如下: if(ad

11、d) / 加法 num1=num1 +num2; else if(sub) /减法 num1=num1 - num2; else if(mul) /乘法 num1=num1 * num2; else if(div) /除法 if (num2=0) throw new ArithmeticException(除数不能为零); else num1=num1 / num2; else if(chf) /乘方 if(num2=0) num1=1; else num1=Math.pow(num1, num2); else if(evo) /开方 if(num20) throw new Arithmeti

12、cException(被开方数不能小于零); else num1=Math.sqrt(num2); else if(rec) /倒数 if(num20) throw new ArithmeticException(分母不能小于零); else num1=1/num2; else if(per) /百分 num1=num1/100.0; else if(pur) /阶乘 if(num20) throw new ArithmeticException(负数不能求阶乘); else for(int i=1;inum2;i+) num1=i*num1; resultField.setText(Stri

13、ng.valueOf(num1); end=true; else if(e.getSource()=f3) /实现清零 resultField.setText(0); else if(e.getSource()=f4) /实现负号 if(resultField.getText()!=0) resultField.setText(-+resultField.getText(); else if(e.getSource()=f5) /实现退格 int i; i=(resultField.getText().length(); if(i=1) resultField.setText(0); else

14、 str=resultField.getText(); resultField.setText(str.substring(0,i-1); 五、程序设计import java.awt.*; import java.awt.event.*; import javax.swing.*; public class test1 extends JFrame implements ActionListener / JFrame jframe1; JPanel jPanel1,jPanel2; JTextField resultField; JButton s1,s2,s3,s4,s5,s6,s7,s8,

15、s9,s0,b1,b2,b3,b4,b5,b6,b7,b8,b9,f1,f2,f3,f4,f5; private boolean end,add,sub,mul,div,evo,chf,per,rec,pur; private String str; private double num1,num2; public test1() super(简易计算器); setSize(300,260); this.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout(1,1); /一行一列

16、的布局 jPanel2=new JPanel(); jPanel2.setLayout(new GridLayout(6,4); /四行五列的布局 resultField=new JTextField(0); jPanel1.add(resultField); this.add(jPanel1,BorderLayout.NORTH); s1=new JButton( 1 ); s1.addActionListener(this); s2=new JButton( 2 ); s2.addActionListener(this); s3=new JButton( 3 ); s3.addAction

17、Listener(this); s4=new JButton( 4 ); s4.addActionListener(this); s5=new JButton( 5 ); s5.addActionListener(this); s6=new JButton( 6 ); s6.addActionListener(this); s7=new JButton( 7 ); s7.addActionListener(this); s8=new JButton( 8 ); s8.addActionListener(this); s9=new JButton( 9 ); s9.addActionListen

18、er(this); s0=new JButton( 0 ); s0.addActionListener(this); b1=new JButton( + ); b1.addActionListener(this); b2=new JButton( - ); b2.addActionListener(this); b3=new JButton( * ); b3.addActionListener(this); b4=new JButton( / ); b4.addActionListener(this); b5=new JButton( ); b5.addActionListener(this)

19、; b6=new JButton( 开方 ); b6.addActionListener(this); b7=new JButton( % ); b7.addActionListener(this); b8=new JButton( x! ); b8.addActionListener(this); b9=new JButton( 1/x ); b9.addActionListener(this); f1=new JButton( . ); f1.addActionListener(this); f2=new JButton( = ); f2.addActionListener(this);

20、f3=new JButton( C ); f3.addActionListener(this); f4=new JButton( 负 ); f4.addActionListener(this); f5=new JButton( 退格 ); f5.addActionListener(this); jPanel2.add(f3); jPanel2.add(b4); jPanel2.add(b3); jPanel2.add(f5); jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b1); jPanel2.add(s4);

21、 jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b5); jPanel2.add(s0); jPanel2.add(f1); jPanel2.add(f4); jPanel2.add(b6); jPanel2.add(b7); jPanel2.add(b8); jPanel2.add(b9); jPanel2.add(f2); this.add(jPanel2,BorderLayout.CENTER); publi

22、c void num(int i) String s = null; s=String.valueOf(i); /返回整数i的字符串表示形式 if(end) /如果数字输入结束,则将文本框置零,重新输入 resultField.setText(0); end=false; if(resultField.getText().equals(0) /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s); else /如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(

23、str); public void actionPerformed(ActionEvent e)throws ArithmeticException /数字事件 if(e.getSource()=s1) num(1); else if(e.getSource()=s2) num(2); else if(e.getSource()=s3) num(3); else if(e.getSource()=s4) num(4); else if(e.getSource()=s5) num(5); else if(e.getSource()=s6) num(6); else if(e.getSource(

24、)=s7) num(7); else if(e.getSource()=s8) num(8); else if(e.getSource()=s9) num(9); else if(e.getSource()=s0) num(0); /符号事件 else if(e.getSource()=b1) sign(1); else if(e.getSource()=b2) sign(2); else if(e.getSource()=b3) sign(3); else if(e.getSource()=b4) sign(4); else if(e.getSource()=b5) sign(5); els

25、e if(e.getSource()=b6) sign(6); else if(e.getSource()=b7) sign(7); else if(e.getSource()=b8) sign(8); else if(e.getSource()=b9) sign(9); else if(e.getSource()=f1) /实现. str=resultField.getText(); if(str.indexOf(.)=1) str+=.; resultField.setText(str); else if(e.getSource()=f2) /实现= num2=Double.parseDo

26、uble(resultField.getText(); if(add) / 加法 num1=num1 +num2; else if(sub) /减法 num1=num1 - num2; else if(mul) /乘法 num1=num1 * num2; else if(div) /除法 if (num2=0) throw new ArithmeticException(除数不能为零); else num1=num1 / num2; else if(chf) /乘方 if(num2=0) num1=1; else num1=Math.pow(num1, num2); else if(evo)

27、/开方 if(num20) throw new ArithmeticException(被开方数不能小于零); else num1=Math.sqrt(num2); else if(rec) /倒数 if(num20) throw new ArithmeticException(分母不能小于零); else num1=1/num2; else if(per) /百分 num1=num1/100.0; else if(pur) /阶乘 if(num20) throw new ArithmeticException(负数不能求阶乘); else for(int i=1;inum2;i+) num1

28、=i*num1; resultField.setText(String.valueOf(num1); end=true; else if(e.getSource()=f3) /实现清零 resultField.setText(0); else if(e.getSource()=f4) /实现负号 if(resultField.getText()!=0) resultField.setText(-+resultField.getText(); else if(e.getSource()=f5) /实现退格 int i; i=(resultField.getText().length(); if(i=1) resultField.setText(0); else str=resultField.getText(); resultField.setText(str.substring(0,i-1); p

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1