版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、目录一、运行环境············································5二、问题描述及要求···
2、···································5三、需求分析··············
3、;······························5四、设计思路··················
4、3;·························54.工作原理图·······················
5、183;···············64.功能规划·································&
6、#183;·······6五、程序的界面设计及代码实现····························75.声明的类··········
7、83;·····························75.引用的包···················
8、183;····················75.界面设计····························&
9、#183;···········75.4计算功能实现····································15六
10、、参考文献···········································21七、总结·····&
11、#183;·········································21八、源代码·······
12、······································22一、运行环境(1)操作系统:Solaris、Windows xp、Windows 7等(2). 应用软件:jdk1.5、Eclipse二、问
13、题描述及要求制作一个计算器,要求仿Windows里的计算器,设计一个图形界面,其中基本组件包括09、+、-、*、/、.、=、+/-、Back、CE、C、sqrt、%、1/x、。其基本功能完成加减乘除、开方、求模、求倒,十进制与八进制、二进制、十六进制的转换等,退格、清零等按钮的实现。 在我的计算器上实现了以上功能。在菜单栏有查看、编辑、帮助菜单,在“查看”菜单中有“标准型”菜单项,实现加减乘除等基本功能,“科学型”菜单项,实现进制间的转换。在“编辑”菜单中有“复制”、“粘贴”菜单项。“帮助”菜单中有“关于计算器”、“帮助主题”菜单项,并实现相关功能。三、需求分析日常生活中经常需要用到计算器,比
14、如科学计算、数值计算、会计业务等,但简单的计算器已不能满足日常需要,因此有必要开发一些进制转换、开方等多种运算的计算器。创建一个简单计算器,具有简单的人机交互界面,便于数据计算。我的计算器具有的功能如下:1、实现基本的加、减、乘、除四则运算及开方、求倒、求模。2、Back退格、CE返回上层运算符、C清零功能。3、十进制与二进制、八进制、十六进制之间的转换。4、菜单中包含的一些功能,如帮助文档。四、设计思路4.1 工作原理图开始按键判断进制转换数字按键运用相应处理按键判断运算符按键进制转换显示结果是否继续操作结束4.2功能规划 本程序继承父类Frame,运用了布局管理器GridLayout和Bo
15、rderlayout,将界面分为三块,顶上为单行文本框,中间为进制单选钮和退格、清空按钮,下面是数字、符号按钮。各种按钮采用行列的网格布局,并注册按钮事件监听器。事件监听器中的事件处理方法void actionPerformed(ActionEvent event)完成主要的按钮事件的处理。事件分为以下几种情况:数字按钮事件()、运算符按钮事件(、×、)、正负号按钮事件()、小数点按钮事件()、等号按钮事件()、求倒按钮事件()、三角函数按钮事件(cos,sin,tan)、开方按钮事件(sqrt)、对数按钮事件(lgX,lnX)、进制转换按钮事件。在此声明的是每次输入的数据都要进行类
16、型转换。五、程序的界面设计及代码实现5.1声明的类* 类名: calculator * 作用: 主类。* 继承的父类: JFrame类 * 实现的接口:ActionListener类 * 类名: WindowDestroyer * 作用: 退出窗口动作。 * 继承的父类: WindowAdapter类 * 实现的接口:无 * 类名: objConversion * 作用: 各个进制之间的转换。* 继承的父类: 无 * 实现的接口:无 *5.2引用的包import java.awt.*;import javax.swing.*;import java.lang.Math;import java.
17、awt.event.*;5.3界面设计、调试后的界面:标准型JScrollPane scrollHelp;private objConversion convert = new objConversion();JMenuItem fileMenu,exitItemOfFile,s,t, about, me;JRadioButton sixteen,ten,eight,two;/单选按扭Jbutton Back,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;Jbutton a,b,cc,dd,ee,ff,jia,jian,cheng
18、,chu,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX;Container cp;/容器,便于集体操作 JTextField text; String copycontent="" boolean clickable=true,clear=true; int all=0; double qian; String fuhao,copy; int jin=10,first=1; public Calculator() super("计算器"); setSize(400,400); setLocation(400,
19、400); text=new JTextField(25); text.setHorizontalAlignment(JTextField.LEFT);/从左到右 JPanel cp1=new JPanel(); JPanel cp2=new JPanel(); JPanel cp3=new JPanel(); cp=getContentPane(); cp.add(cp1,"North"); cp.add(cp2,"Center"); cp.add(cp3,"South"); cp1.setLayout(new GridLayout
20、(1,1); cp2.setLayout(new GridLayout(2,4); cp3.setLayout(new GridLayout(7,4); sixteen=new JRadioButton("十六进制"); sixteen.setVisible(false); ten=new JRadioButton("进制",true); ten.setVisible(false); eight=new JRadioButton("八进制"); eight.setVisible(false); two=new JRadioButton
21、("二进制"); two.setVisible(false); a = new JButton("A"); a.setVisible(false); b = new JButton("B"); b.setVisible(false); cc = new JButton("C"); cc.setVisible(false); dd = new JButton("D"); dd.setVisible(false); ee = new JButton("E"); ee.setVis
22、ible(false); ff = new JButton("F"); ff.setVisible(false); jia = new JButton("+"); jian = new JButton("-"); cheng = new JButton("×"); chu = new JButton("÷"); quyu = new JButton("%"); deng = new JButton("="); fu = new JBut
23、ton("+/-"); dian = new JButton("."); kai = new JButton("sqrt"); dao = new JButton("1/x"); num0=new JButton("0"); num1=new JButton("1"); num2=new JButton("2"); num3=new JButton("3"); num4=new JButton("4"); num5=
24、new JButton("5"); num6=new JButton("6"); num7=new JButton("7"); num8=new JButton("8"); cos=new JButton("cos"); sin=new JButton("sin"); tan=new JButton("tan"); lgX=new JButton("lgX"); lnX=new JButton("lnX"); num
25、9=new JButton("9"); sixteen.addActionListener(this); ten.addActionListener(this); eight.addActionListener(this); two.addActionListener(this); ButtonGroup btg=new ButtonGroup();/创建一个多斥作用域 btg.add(sixteen); btg.add(ten); btg.add(eight); btg.add(two); cp1.add(text); text.setEditable(false); t
26、ext.setBackground(Color.white); Back=new JButton("Back");/Back Back.setForeground(Color.red); Back.addActionListener(this); ce=new JButton("CE");/CE ce.setForeground(Color.red); ce.addActionListener(this); c=new JButton("C");/C c.setForeground(Color.red); c.addActionLis
27、tener(this); cp2.add(sixteen); cp2.add(ten); cp2.add(eight); cp2.add(two); cp2.add(Back); cp2.add(ce); cp2.add(c); cp3.add(num7); num7.addActionListener(this); cp3.add(num8); num8.addActionListener(this); cp3.add(num9); num9.addActionListener(this); cp3.add(chu); chu.addActionListener(this); cp3.add
28、(kai); kai.addActionListener(this); cp3.add(num4); num4.addActionListener(this); cp3.add(num5); num5.addActionListener(this); cp3.add(num6); num6.addActionListener(this); cp3.add(cheng); cheng.addActionListener(this); cp3.add(quyu); quyu.addActionListener(this); cp3.add(num1); num1.addActionListener
29、(this); cp3.add(num2); num2.addActionListener(this); cp3.add(num3); num3.addActionListener(this); cp3.add(jian); jian.addActionListener(this); cp3.add(dao); dao.addActionListener(this); cp3.add(num0); num0.addActionListener(this); cp3.add(fu); fu.addActionListener(this); cp3.add(dian); dian.addActio
30、nListener(this); cp3.add(jia); jia.addActionListener(this); cp3.add(deng); deng.addActionListener(this); cp3.add(cos); cos.addActionListener(this); cp3.add(sin); sin.addActionListener(this); cp3.add(tan); tan.addActionListener(this); cp3.add(lnX); lnX.addActionListener(this); cp3.add(lgX); lgX.addAc
31、tionListener(this); cp3.add(a); a.setForeground(Color.magenta); a.setBackground(Color.pink); a.addActionListener(this); cp3.add(b); b.setForeground(Color.magenta); b.setBackground(Color.pink); b.addActionListener(this); cp3.add(cc); cc.setForeground(Color.magenta); cc.setBackground(Color.pink); cc.a
32、ddActionListener(this); cp3.add(dd); dd.setForeground(Color.magenta); dd.setBackground(Color.pink); dd.addActionListener(this); cp3.add(ee); ee.setForeground(Color.magenta); ee.setBackground(Color.pink); ee.addActionListener(this); cp3.add(ff); ff.setForeground(Color.magenta); ff.setBackground(Color
33、.pink); ff.addActionListener(this); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu("编辑"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("帮助"); mainMenu.add(viewMenu); mainMenu.add(editMenu); mainMenu.add(helpMenu); f
34、ileMenu = new JMenu("复制C Ctrl+C"); exitItemOfFile = new JMenuItem("粘贴V Ctrl+V"); fileMenu.addActionListener(this); exitItemOfFile.addActionListener(this); editMenu.add(fileMenu); editMenu.add(exitItemOfFile); t = new JMenuItem("标准型"); s = new JMenuItem(" 科学型")
35、; viewMenu.add(t); viewMenu.add(s); t.addActionListener(this); s.addActionListener(this); about = new JMenuItem(" 关于计算器"); me = new JMenuItem(" 帮助主题");helpMenu.add(about); helpMenu.add(me); about.addActionListener(this); me.addActionListener(this); addWindowListener(new WindowDes
36、troyer();、科学型:、这是“帮助”菜单里的“帮助主题”菜单项:用了JtextArea将内容显示出来me = new JMenuItem(" 帮助主题"); JTextArea help = new JTextArea(10, 30); scrollHelp = new JScrollPane(help); help.setEditable(false); help.append("执行简单计算 "+"n"); help.append("1. 键入计算的第一个数字。"+"n"); help
37、.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除"+"n"); help.append("3. 键入计算的下一个数字。"+"n"); help.append("4. 输入所有剩余的运算符和数字。"+"n"); help.append("5. 单击“=“ ");、这是“帮助”菜单里的“关于计算器”菜单项:用了JOptionPane.showMessageDialog(null, "计算机开发者:魏晓力")
38、;5.4计算功能实现、四则运算 if (temp = jia)/加法 qian = Double.parseDouble(text.getText(); fuhao = "+" clear = false; if (temp = jian) qian = Double.parseDouble(text.getText(); fuhao = "-" clear = false; if (temp = cheng) qian = Double.parseDouble(text.getText(); fuhao = "×" cle
39、ar = false; if(temp=chu) qian = Double.parseDouble(text.getText(); fuhao = "÷" clear = false; if (temp = quyu) qian = Double.parseDouble(text.getText(); fuhao="%" clear = false; if (temp = deng) double ss = Double.parseDouble(text.getText(); text.setText(""); if (f
40、uhao = "+") text.setText(qian + ss + ""); if (fuhao = "-") text.setText(qian - ss + ""); if (fuhao = "×") text.setText(qian * ss + ""); if (fuhao = "÷") text.setText(qian / ss + ""); if(fuhao="%") tex
41、t.setText(qian%ss + ""); clear = false;/要清空前一次的数据 、三角函数运算if(temp=cos) text.setText(Double.toString(Math.cos(Double.parseDouble(text.getText()*Math.PI/180); if(temp=sin)text.setText(Double.toString(Math.sin(Double.parseDouble(text.getText()*Math.PI/180); if(temp=tan) text.setText(Double.toS
42、tring(Math.tan(Double.parseDouble(text.getText()*Math.PI/180); 、对数运算if(temp=lgX) text.setText(Double.toString(Math.log10(Double.parseDouble(text.getText(); if(temp=lnX) text.setText(Double.toString(Math.log(Double.parseDouble(text.getText(); 、开方、求倒运算if (temp = kai) String s = text.getText(); if (s.c
43、harAt(0) = '-') text.setText("负数不能开根号"); else text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText(); clear = false; if (temp = dao) if (text.getText().charAt(0) = '0' && text.getText().length() = 1) text.setText("除数不能为零"); else
44、 boolean isDec = true; int i, j, k; String s = Double.toString(1 / Double.parseDouble(text.getText(); for (i = 0; i < s.length(); i+) if (s.charAt(i) = '.') break; for (j = i + 1; j < s.length(); j+) if (s.charAt(j) != '0') isDec = false; break; if (isDec = true) String stemp =
45、 "" for (k = 0; k < i; k+) stemp += s.charAt(k); text.setText(stemp); else text.setText(s); clear = false; 、正负号运算if (temp = fu) boolean isNumber = true; String s = text.getText(); for (int i = 0; i < s.length(); i+) if(!(s.charAt(i)>='0'&&s.charAt(i)<='9'
46、;|s.charAt(i)='.'|s.charAt(i)='-') isNumber = false; break; if (isNumber = true) if (s.charAt(0) = '-') text.setText(""); for (int i = 1; i < s.length(); i+) char a = s.charAt(i); text.setText(text.getText() + a); else text.setText('-' + s); 、退出窗口动作class
47、WindowDestroyer extends WindowAdapter/ public void windowClosing(WindowEvent e) System.exit(0); 、进制转换class objConversion public String decDec(int decNum) String strDecNum = Integer.toString(decNum); for (int i = strDecNum.length(); i < 3; i+) strDecNum = "0" + strDecNum; return invert (
48、strDecNum, 5); public String decHex (int decNum)/10 to 16 String strHexNum = "" int currentNum = 0; while(decNum != 0) if (decNum > 15) currentNum=decNum%16; decNum /= 16; else currentNum = decNum; decNum = 0; switch (currentNum) case 15: strHexNum += "F" break; case 14: strHe
49、xNum += "E" break; case 13: strHexNum += "D" break; case 12: strHexNum += "C" break; case 11: strHexNum += "B" break; case 10: strHexNum += "A" break; default: strHexNum +=Integer.toString(currentNum);break; return invert(strHexNum,2); public String
50、decOct (int decNum)/10 to 8 String strOctNum = "" while (decNum != 0) if (decNum > 7) strOctNum += Integer.toString(decNum % 8); decNum /= 8; else strOctNum += Integer.toString(decNum); decNum = 0; return invert (strOctNum, 3); public String decBin (int decNum) /10 to 2 String strBinNum
51、 = "" while (decNum != 0) if (decNum > 1) strBinNum += Integer.toString(decNum % 2); decNum /= 2; else strBinNum += Integer.toString(decNum); decNum = 0; return invert (strBinNum, 8); private String invert(String strNum,int minLength) String answer = "" int length = strNum.len
52、gth(); if (length < minLength) for (int padding =(minLength -length);padding>0;padding-) answer += "0" for(int i=length;i>0;i-) answer+=strNum.charAt(i-1); return answer; 六、参考文献01施霞萍,张欢.Java课程设计(第二版)M机械工业出版社.2006年8月02 耿祥义,张跃平. Java2实用教程(第三版).清华大学出版社。七、总结设计给人以创作的冲动,但是也要为这次冲动承担一定的痛苦
53、,却事后会发现,这一切都是值得的。本次的Java课程设计让我对Java的理论知识又有了更深一步的了解,温故而知新,开始设计时完全没头绪,感觉很混乱,对书本知识不够扎实的我深感“书到用时方恨少”,只好又一遍浏览全书,让我对其有了大概的复习,对知识系统全面进行了了解,让我里出了我设计的框架,遇到困难时先是苦思冥想在向同学请教。这次课程设计使我感到收获不小,让我对Java的设计过程有了更深的了解,促进了对理论知识的消化与吸收,也巩固和完善了本门课程的知识体系结构。设计过程中遇到了不少麻烦:如进制间的转换,也遇到了平时学习中老师强调与教过的疑难点,不懂的通过翻阅资料和与同学间的讨论都一一解决了。通过实
54、践让我发现了我的不足,并加深了自身学习能力,提高了综合能力。因此在以后的学习中我会通过实践来检验自己的不足和加深、巩固自身。以此来完善自己的知识系统。这次课程设计的主要目的是学会Java程序开发的环境搭建与配置,并在实际运用中学习和掌握Java程序开发的全过程,以及进一步熟悉掌握Java程序设计语言的基础内容,提高Java编程技术以及分析解决问题的综合能力。通过这次课程设计,我基本掌握了以上要求。由于专业知识有限,以及动手能力的欠缺,所以开发的系统不是很完善,有一些功能未实现,但是简易计算器的基本功能均已实现。以前对Java语言的很多知识认识都不深刻,做过这次课程设计之后,我对Java语言的开
55、发有了一个比较系统的了解;比如:用户图形界面设计等的运用已经比较熟练。八、源代码import java.awt.*;import javax.swing.*;import java.lang.Math;import java.awt.event.*;public class Calculator extends JFrame implements ActionListenerJScrollPane scrollHelp;private objConversion convert = new objConversion();/各个进制之间的转化JMenuItem fileMenu,exitIte
56、mOfFile,s,t, about, me;JRadioButton sixteen,ten,eight,two;/单选按扭JButton Back,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;JButton a,b,cc,dd,ee,ff,jia,jian,cheng,chu,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX;Container cp;/容器,便于集体操作 JTextField text; String copycontent="" boolean
57、 clickable=true,clear=true; int all=0; double qian; String fuhao,copy; int jin=10,first=1; public Calculator() super("计算器"); setSize(400,400); setLocation(400,400); text=new JTextField(25); text.setHorizontalAlignment(JTextField.LEFT);/从左到右 JPanel cp1=new JPanel(); JPanel cp2=new JPanel(); JPanel cp3=new JPanel(); cp=getConten
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广东省东莞虎门汇英学校2026年八上数学期末调研试题含解析
- 武汉东湖学院《商务统计》2026-2027学年第一学期期末试卷含解析
- 北京大兴区北臧村中学2027届物理八年级第一学期期末统考模拟试题含解析
- 西安音乐学院《中级写作》2026-2027学年第一学期期末试卷含解析
- 护理实践评估
- 护理美学中的审美体验设计
- 吉林省松原市名校2027届物理八上期末达标测试试题含解析
- S-Bleximenib-S-JNJ-75276617-生命科学试剂-MCE
- AI在电子技术中的应用
- 安防监控笔试题及答案
- 2026年四川省拟任县处级领导干部理论(任职资格考试)练习题及答案
- (2026年)护理文书书写规范与质量控制课件
- 班组安全生产检查培训课件
- 2026年贵州省专业技术人员继续教育公需科目试题及参考答案详解(模拟题)
- 信誉楼老带新客户裂变
- 营养配餐工作室创新创业
- 2026年国家能源集团河南公司校园招聘笔试参考题库及答案解析
- 肝病与凝血教学课件
- GB/Z 43592.2-2025纳米技术磁性纳米材料第2部分:核酸提取用磁珠的特性和测量规范
- 肿瘤学概论课件
- 手法排痰课件
评论
0/150
提交评论