




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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和Bor
15、derlayout,将界面分为三块,顶上为单行文本框,中间为进制单选钮和退格、清空按钮,下面是数字、符号按钮。各种按钮采用行列的网格布局,并注册按钮事件监听器。事件监听器中的事件处理方法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.a
17、wt.event.*;5.3界面设计、调试后的界面:标准型JScrollPane scrollHelp;private objConversion convert = new objConversion();JMenuItem fileMenu,exitItemOfFile,s,t, about, me;JRadioButton sixteen,ten,eight,two;/单选按扭JbuttonBack,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;Jbuttona,b,cc,dd,ee,ff,jia,jian,cheng,ch
18、u,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX;Container cp;/容器,便于集体操作 JTextField text; String copycontent=""booleanclickable=true,clear=true;intall=0;doubleqian; String fuhao,copy;intjin=10,first=1;public Calculator()super("计算器"); setSize(400,400); setLocation(400,400);text=new
19、 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(1,1); cp2.setLayou
20、t(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("二进制");two.setV
21、isible(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.setVisible(false);ff = new JButton("F&
22、quot;);ff.setVisible(false);jia = new JButton("+");jian = new JButton("-");cheng = new JButton("×");chu = new JButton("÷");quyu = new JButton("%");deng = new JButton("=");fu = new JButton("+/-");dian = new JButton("
23、.");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=new JButton("5");num6=new JButton("6&quo
24、t;);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");num9=new JButton("9");sixteen.addActionListener(this);ten
25、.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);text.setBackground(Color.white);Back=new JButton("Back");/Bac
26、kBack.setForeground(Color.red);Back.addActionListener(this);ce=new JButton("CE");/CEce.setForeground(Color.red);ce.addActionListener(this);c=new JButton("C");/Cc.setForeground(Color.red);c.addActionListener(this); cp2.add(sixteen); cp2.add(ten); cp2.add(eight); cp2.add(two); cp2.
27、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(kai);kai.addActionListener(this); cp3.add(num4);num4.addActionListener(this); cp3.
28、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(this); cp3.add(num2);num2.addActionListener(this); cp3.add(num3);num3.addActionListener(t
29、his); 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.addActionListener(this); cp3.add(jia);jia.addActionListener(this); cp3.add(deng);deng.addActionListener(t
30、his); 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.addActionListener(this); cp3.add(a);a.setForeground(Color.magenta);a.setBackground(Color.pink);a.addActionLis
31、tener(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.addActionListener(this); cp3.add(dd);dd.setForeground(Color.magenta);dd.setBackground(Color.pink);dd.addActionList
32、ener(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.pink);ff.addActionListener(this); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu(
33、"编辑"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("帮助"); mainMenu.add(viewMenu); mainMenu.add(editMenu); mainMenu.add(helpMenu);fileMenu = new JMenu("复制C Ctrl+C");exitItemOfFile = new JMenuItem("粘贴V Ctrl+V");fileMenu.addActionListen
34、er(this);exitItemOfFile.addActionListener(this); editMenu.add(fileMenu); editMenu.add(exitItemOfFile);t = new JMenuItem("标准型");s = new JMenuItem(" 科学型"); viewMenu.add(t); viewMenu.add(s);t.addActionListener(this);s.addActionListener(this);about = new JMenuItem(" 关于计算器")
35、;me = new JMenuItem(" 帮助主题");helpMenu.add(about); helpMenu.add(me);about.addActionListener(this);me.addActionListener(this); addWindowListener(new WindowDestroyer();、科学型:、这是“帮助”菜单里的“帮助主题”菜单项:用了JtextArea将内容显示出来me = new JMenuItem(" 帮助主题");JTextArea help = new JTextArea(10, 30); scr
36、ollHelp = new JScrollPane(help); help.setEditable(false); help.append("执行简单计算 "+"n"); help.append("1. 键入计算的第一个数字。"+"n"); help.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除"+"n"); help.append("3. 键入计算的下一个数字。"+"n"); help.append
37、("4. 输入所有剩余的运算符和数字。"+"n"); help.append("5. 单击“=“ ");、这是“帮助”菜单里的“关于计算器”菜单项:用了JOptionPane.showMessageDialog(null, "计算机开发者:魏晓力");5.4计算功能实现、四则运算if (temp = jia)/加法qian = Double.parseDouble(text.getText();fuhao = "+"clear = false; if (temp = jian)qian = Do
38、uble.parseDouble(text.getText();fuhao = "-"clear = false; if (temp = cheng)qian = Double.parseDouble(text.getText();fuhao = "×"clear = false; if(temp=chu)qian = Double.parseDouble(text.getText();fuhao = "÷"clear = false; if (temp = quyu)qian = Double.parseDoub
39、le(text.getText();fuhao="%"clear = false; if (temp = deng)double ss = Double.parseDouble(text.getText();text.setText("");if (fuhao = "+")text.setText(qian + ss + "");if (fuhao = "-")text.setText(qian - ss + "");if (fuhao = "×"
40、;)text.setText(qian * ss + "");if (fuhao = "÷")text.setText(qian / ss + "");if(fuhao="%")text.setText(qian%ss + "");clear = false;/要清空前一次的数据 、三角函数运算if(temp=cos) text.setText(Double.toString(Math.cos(Double.parseDouble(text.getText()*Math.PI/180)
41、; if(temp=sin)text.setText(Double.toString(Math.sin(Double.parseDouble(text.getText()*Math.PI/180); if(temp=tan) text.setText(Double.toString(Math.tan(Double.parseDouble(text.getText()*Math.PI/180); 、对数运算if(temp=lgX) text.setText(Double.toString(Math.log10(Double.parseDouble(text.getText();if(temp=l
42、nX) text.setText(Double.toString(Math.log(Double.parseDouble(text.getText(); 、开方、求倒运算if (temp = kai)String s = text.getText();if (s.charAt(0) = '-')text.setText("负数不能开根号");elsetext.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText();clear = false; if (temp
43、 = dao)if (text.getText().charAt(0) = '0' && text.getText().length() = 1)text.setText("除数不能为零");elseboolean 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
44、 = i + 1; j < s.length(); j+)if (s.charAt(j) != '0')isDec = false; break; if (isDec = true) String stemp = ""for (k = 0; k < i; k+) stemp += s.charAt(k);text.setText(stemp); elsetext.setText(s); clear = false; 、正负号运算if (temp = fu)boolean isNumber = true; String s = text.getTe
45、xt();for (int i = 0; i < s.length(); i+) if(!(s.charAt(i)>='0'&&s.charAt(i)<='9'|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();
46、i+)char a = s.charAt(i);text.setText(text.getText() + a); elsetext.setText('-' + s); 、退出窗口动作class WindowDestroyer extends WindowAdapter/publicvoid windowClosing(WindowEvent e) System.exit(0); 、进制转换class objConversionpublic String decDec(int decNum) String strDecNum = Integer.toString(decNum)
47、;for (int i = strDecNum.length(); i < 3; i+) strDecNum = "0" + strDecNum;return invert (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 =
48、 decNum; decNum = 0; switch (currentNum)case 15: strHexNum += "F" break;case 14: strHexNum += "E" break;case 13: strHexNum += "D" break;case 12: strHexNum += "C" break;case 11: strHexNum += "B" break;case 10: strHexNum += "A" break;default:
49、 strHexNum +=Integer.toString(currentNum);break; return invert(strHexNum,2); public String 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;
50、return invert (strOctNum, 3); public String decBin (int decNum) /10 to 2 String strBinNum = ""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
51、invert(String strNum,int minLength) String answer = ""int length = strNum.length();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课程设计(第二
52、版)M机械工业出版社.2006年8月02 耿祥义,张跃平. Java2实用教程(第三版).清华大学出版社。七、总结设计给人以创作的冲动,但是也要为这次冲动承担一定的痛苦,却事后会发现,这一切都是值得的。本次的Java课程设计让我对Java的理论知识又有了更深一步的了解,温故而知新,开始设计时完全没头绪,感觉很混乱,对书本知识不够扎实的我深感“书到用时方恨少”,只好又一遍浏览全书,让我对其有了大概的复习,对知识系统全面进行了了解,让我里出了我设计的框架,遇到困难时先是苦思冥想在向同学请教。这次课程设计使我感到收获不小,让我对Java的设计过程有了更深的了解,促进了对理论知识的消化与吸收,也巩固和
53、完善了本门课程的知识体系结构。设计过程中遇到了不少麻烦:如进制间的转换,也遇到了平时学习中老师强调与教过的疑难点,不懂的通过翻阅资料和与同学间的讨论都一一解决了。通过实践让我发现了我的不足,并加深了自身学习能力,提高了综合能力。因此在以后的学习中我会通过实践来检验自己的不足和加深、巩固自身。以此来完善自己的知识系统。这次课程设计的主要目的是学会Java程序开发的环境搭建与配置,并在实际运用中学习和掌握Java程序开发的全过程,以及进一步熟悉掌握Java程序设计语言的基础内容,提高Java编程技术以及分析解决问题的综合能力。通过这次课程设计,我基本掌握了以上要求。由于专业知识有限,以及动手能力的
54、欠缺,所以开发的系统不是很完善,有一些功能未实现,但是简易计算器的基本功能均已实现。以前对Java语言的很多知识认识都不深刻,做过这次课程设计之后,我对Java语言的开发有了一个比较系统的了解;比如:用户图形界面设计等的运用已经比较熟练。八、源代码import java.awt.*;import javax.swing.*;import java.lang.Math;import java.awt.event.*;publicclassCalculatorextends JFrame implements ActionListenerJScrollPane scrollHelp;private
55、 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,chu,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX
56、;Container cp;/容器,便于集体操作 JTextField text; String copycontent=""booleanclickable=true,clear=true;intall=0;doubleqian; String fuhao,copy;intjin=10,first=1;public Calculator()super("计算器"); setSize(400,400); setLocation(400,400);text=new JTextField(25);text.setHorizontalAlignment(JTe
57、xtField.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(1,1); cp2.setLayout(new GridLayout(2,4); cp3.setLayout(new GridLa
58、yout(7,4);sixteen=new JRadioButton("十六进制");sixteen.setVisible(false);ten=new JRadioButton("进制",true);ten.setVisible(false);eight=new JRadioButton("八进制");eight.setVisible(false);two=new JRadioButton("二进制");two.setVisible(false);a = new JButton("A");a.
59、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.setVisible(false);ff = new JButton("F");ff.setVisible(false);jia = new JButton(&
60、quot;+");jian = new JButton("-");cheng = new JButton("×");chu = new JButton("÷");quyu = new JButton("%");deng = new JButton("=");fu = new JButton("+/-");dian = new JButton(".");kai = new JButton("sqrt");da
61、o = 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=new JButton("5");num6=new JButton("6");num7=new JButton("7");num8=new JB
62、utton("8");cos=new JButton("cos");sin=new JButton("sin");tan=new JButton("tan");lgX=new JButton("lgX");lnX=new JButton("lnX");num9=new JButton("9");sixteen.addActionListener(this);ten.addActionListener(this);eight.addActionListene
63、r(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);text.setBackground(Color.white);Back=new JButton("Back");/BackBack.setForeground(Color.red);Back.addActionListener(this);ce=new JButton("CE");/CEce.setForeground(Color.red);ce.addActionListener(this);c=new JButton("C");/Cc.setForeground(Color.red);c.addActionListener(this); cp2.add(six
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度抵押车辆欠款债权处置合同
- 二零二五年度商铺投资合作协议
- 二零二五年度上海美业产品加盟店知识产权保护合同范本
- 2025年度绿色环保装修服务合同
- 二零二五年度就业市场调研方就业协议
- 二零二五年度个人艺术品拍卖贷款私人现金借款合同
- 二零二五年度特色小吃店租赁服务协议
- 二氧化碳除杂化学式
- 2024年思政理论对社会进步的影响试题及答案
- 办公家具合同采购合同范本
- 幼儿园大班音乐《建筑之歌》
- 2022区域新能源集控中心建设指导意见
- 神笔马良-中国故事英文版课件
- 发票审批核准事前查验单
- 人工智能:现代方法
- 特种作业人员安全技术培训考核管理规定
- 初中英语- I'd love to sail across the Pacific.教学课件设计
- 第讲 发达资本主义国家经济与政治
- 城市热力网设计规范标准
- 陶瓷装饰工(四级)理论考试复习题库(浓缩300题)
- 国家电网公司电力安全工作规程(变电部分)2013年6月修订
评论
0/150
提交评论