




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、目 录目 录I1需求分析11.1计算器的基本功能:11.1.1加法运算:用数字按钮和“+”按钮进行运算;11.1.2减法运算:用数字按钮和“-”按钮进行运算;11.1.3乘法运算:用数字按钮和“*”按钮进行运算;11.1.4除法运算:用数字按钮和“/”按钮进行运算;11.2退格键和清零键: 用”Backspace”、”C”和”CE”按钮实现;11.3计算器的科学计算方法:11.3.1开方:用数字按钮和“Sqrt”按钮进行运算;11.3.2百分比:用数字按钮和“%”按钮进行运算;11.3.3求倒数:用数字按钮和“1/x”按钮进行运算;11.3.4求自然对数:用数字按钮和“ln”按钮进行运算;11
2、.3.5三角函数:用数字按钮和“tan”“cos”“sin”按钮进行运算11.3.6角度换算成弧度:用数字按钮和“ ”按钮进行运算11.3.7求反:用数字按钮和“-/+”按钮进行运算11.3.8平方和三次方:用数字按钮和“x2”“x3”按钮进行运算11.4常数:21.4.1:用Math类中的PI来实现21.4.2自然对数e:用Math类中的E来实现22设计22.1用户界面设计22.1.1该计算器程序的设计:用户界面包括Swing组件,不过程序中大都使用的是AWT组件.22.1.2在AWT组件,22.1.3这个界面设计中包含了五个接口,分别控制运算符,数字,清除,存储功能和小数点的输入22.1.
3、4程序设计中,使用了布局管理:32.2概要设计32.2.1它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科学运算方法.33实现34测试144.1实现加法运算:4+12=16144.2实现乘法运算:3*9=27154.3用”C实现清零功能:164.4用”Backspace”实现退格功能:174.5求倒数:1/4=0.25181 需求分析 该计算器程序除了具备加减乘除基本功能外,还有清零键C、CE和退格键Backspace,和一些部分的科学计算方法,包括开方、求倒、百分比,由于时间问题,之后会完善键盘事件的监听功能。1.1 计算器的基本功能:1.1.1 加法运算:用数字按钮和“+”
4、按钮进行运算;1.1.2 减法运算:用数字按钮和“-”按钮进行运算;1.1.3 乘法运算:用数字按钮和“*”按钮进行运算;1.1.4 除法运算:用数字按钮和“/”按钮进行运算;1.2 退格键和清零键: 用”Backspace”、”C”和”CE”按钮实现;1.3 计算器的科学计算方法:1.3.1 开方:用数字按钮和“Sqrt”按钮进行运算;1.3.2 百分比:用数字按钮和“%”按钮进行运算;1.3.3 求倒数:用数字按钮和“1/x”按钮进行运算;1.3.4 求自然对数:用数字按钮和“ln”按钮进行运算;1.3.5 三角函数:用数字按钮和“tan”“cos”“sin”按钮进行运算1.3.6 角度换
5、算成弧度:用数字按钮和“ ”按钮进行运算1.3.7 求反:用数字按钮和“-/+”按钮进行运算1.3.8 平方和三次方:用数字按钮和“x2”“x3”按钮进行运算1.4 常数:1.4.1 :用Math类中的PI来实现1.4.2 自然对数e:用Math类中的E来实现2 设计2.1 用户界面设计2.1.1 该计算器程序的设计:用户界面包括Swing组件,不过程序中大都使用的是AWT组件. 2.1.2 在AWT组件,(1) 使用了面板和按钮组:JPanel panel1, panel2, panel3, panel4;ButtonGroup bgb;(2) 由于该组件按钮较多,设计一个方法简化按钮的设置
6、void addButton(JPanel panel, String name, ActionListener action, Color color)JButton bt = new JButton(name);panel.add(bt);/在面板上增加按钮bt.setForeground(color);/设置字体颜色bt.addActionListener(action);/增加监听事件2.1.3 这个界面设计中包含了五个接口,分别控制运算符,数字,清除,存储功能和小数点的输入class Signs implements ActionListenerclass Num implement
7、s ActionListenerclass Clear implements ActionListenerclass Memory implements ActionListenerclass Dot implements ActionListener2.1.4 程序设计中,使用了布局管理:(1) 用边布局管理器(BorderLayout)设置计算器容器各方位组件:panel4 = new JPanel(new BorderLayout(5, 5)panel4.add(panel1, BorderLayout.NORTH);panel4.add(panel2, BorderLayout.CEN
8、TER);this.add(tf, BorderLayout.NORTH);this.add(panel3, BorderLayout.WEST);(2) 用网格布局管理器(GridLayout)设置面板panel1 = new JPanel(new GridLayout(1, 3, 10, 10);panel2 = new JPanel(new GridLayout(5, 6, 5, 5);panel3 = new JPanel(new GridLayout(5, 1, 5, 5);2.2 概要设计计算器的整个程序包括:一个Calculator类2.2.1 它的功能是使用图形用户来实现计算器
9、的界面设计和运算功能以及一些科学运算方法.类中包含了Calculator()构造方法,和其他重要的成员方法,最终在main方法中实例化一个ca对象实现计算器的功能。3 实现package calculator;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.text.DecimalFormat;public class Calculator extends JFrame JTextField tf;JPanel panel1, panel2, panel3, panel4;JMenuBar m
10、yBar;JMenu menu1, menu2, menu3;JMenuItem editItem1, editItem2, help1, help2, help3;JRadioButtonMenuItem seeItem1, seeItem2;/单选框JCheckBoxMenuItem seeItem3;/复选框ButtonGroup bgb;boolean IfResult = true, flag = false;String oper = =;double result = 0,memory = 0;DecimalFormat df;Calculator()super(科学计算器);/
11、设置标题栏df = new DecimalFormat(#.#);/保留四位小数this.setLayout(new BorderLayout(10, 5);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);/设置每个JPanel的参数panel1 = new JPanel(new GridLayout(1, 3, 10, 10);panel2 = new JPanel(new GridLayout(5, 6, 5, 5);panel3 = new JPanel(new GridLayout(5, 1, 5, 5);
12、panel4 = new JPanel(new BorderLayout(5, 5);/* * 菜单栏 */myBar = new JMenuBar();menu1 = new JMenu(编辑(E);menu2 = new JMenu(查看(V);menu3 = new JMenu(帮助(H);menu1.setFont(new Font(宋体, Font.PLAIN, 12);menu2.setFont(new Font(宋体, Font.PLAIN, 12);menu3.setFont(new Font(宋体, Font.PLAIN, 12);/* * 编辑栏 */editItem1 =
13、 new JMenuItem(复制(C) Ctrl+C);editItem2 = new JMenuItem(粘贴(P) Ctrl+V);editItem1.setFont(new Font(宋体,Font.PLAIN,12);editItem2.setFont(new Font(宋体,Font.PLAIN,12);/* * 查看栏 */seeItem1 = new JRadioButtonMenuItem(科学型(T);seeItem2 = new JRadioButtonMenuItem(标准型(S);seeItem3 = new JCheckBoxMenuItem(数字分组(I);see
14、Item1.setFont(new Font(宋体,Font.PLAIN,12);seeItem2.setFont(new Font(宋体,Font.PLAIN,12);seeItem3.setFont(new Font(宋体,Font.PLAIN,12);/* * 帮助栏 */help1 = new JMenuItem(帮助主题(H);help2 = new JMenuItem(关于计算器(A);help1.setFont(new Font(宋体,Font.PLAIN,12);help2.setFont(new Font(宋体,Font.PLAIN,12);bgb = new ButtonG
15、roup();/选项组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);/* * 文本域,即为计算器的屏幕显示区
16、域 */tf = new JTextField();tf.setEditable(false);/文本区域不可编辑tf.setBackground(Color.white);/文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐tf.setText(0);init();/对计算器进行初始化/* * 初始化操作 * 添加按钮 */void init()addButton(panel1, Backspace, new Clear(), Color.red);addButton(panel1, CE, new Clear(), Colo
17、r.red);addButton(panel1, C, new Clear(), Color.red);addButton(panel2, 1/x, new Signs(), Color.magenta);addButton(panel2, ln, new Signs(), Color.magenta);addButton(panel2, 7, new Num(), Color.blue);addButton(panel2, 8, new Num(), Color.blue);addButton(panel2, 9, new Num(), Color.blue);addButton(panel
18、2, , new Signs(), Color.red);addButton(panel2, n!, new Signs(), Color.magenta);addButton(panel2, sqrt, new Signs(), Color.magenta);addButton(panel2, 4, new Num(), Color.blue);addButton(panel2, 5, new Num(), Color.blue);addButton(panel2, 6, new Num(), Color.blue);addButton(panel2, , new Signs(), Colo
19、r.red);addButton(panel2, sin, new Signs(), Color.magenta);addButton(panel2, x2, new Signs(), Color.magenta);addButton(panel2, 1, new Num(), Color.blue);addButton(panel2, 2, new Num(), Color.blue);addButton(panel2, 3, new Num(), Color.blue);addButton(panel2, -, new Signs(), Color.red);addButton(panel
20、2, cos, new Signs(), Color.magenta);addButton(panel2, x3, new Signs(), Color.magenta);addButton(panel2, 0, new Num(), Color.blue);addButton(panel2, -/+, new Clear(), Color.blue);addButton(panel2, ., new Dot(), Color.blue);addButton(panel2, +, new Signs(), Color.red);addButton(panel2, tan, new Signs(
21、), Color.magenta);addButton(panel2, %, new Signs(), Color.magenta);addButton(panel2, , new Num(), Color.orange);addButton(panel2, e, new Num(), Color.orange);addButton(panel2, , new Signs(), Color.orange);addButton(panel2, =, new Signs(), Color.red);addButton(panel3, MC, new Memory(), Color.red);add
22、Button(panel3, MS, new Memory(), Color.red);addButton(panel3, MR, new Memory(), Color.red);addButton(panel3, M-, new Memory(), Color.red);addButton(panel3, M+, new Memory(), Color.red);panel4.add(panel1, BorderLayout.NORTH);panel4.add(panel2, BorderLayout.CENTER);this.add(tf, BorderLayout.NORTH);thi
23、s.add(panel3, BorderLayout.WEST);this.add(panel4);pack();/调整此窗口的大小,以适合其子组件的首选大小和布局this.setResizable(false);/窗口不可改变大小this.setLocation(300, 200);/* * 统一设置按钮的的使用方式 */void addButton(JPanel panel, String name, ActionListener action, Color color)JButton bt = new JButton(name);panel.add(bt);/在面板上增加按钮bt.set
24、Foreground(color);/设置字体颜色bt.addActionListener(action);/增加监听事件/* * 计算器的基础操作(+ - ) */void getResult (double x)if(oper.equals(+)result += x;else if(oper.equals(-)result -= x;else if(oper.equals()result *= x;else if(oper.equals()result /= x;else if(oper.equals(=)result = x;tf.setText(df.format(result);/
25、* * 运算符号的事件监听 */class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent对象的getActionCommand()方法取得与引发事件对象的字符串 */String str = e.getActionCommand();/* sqrt求平方根 */if(str.equals(sqrt)double i = Double.parseDouble(tf.getText();/将tf文本框中已经输入的数字字符串转化为double类型的变量if(i=0
26、)/* * String.valueOf() 转换为字符串 * df.format() 按要求保留四位小数 * Math.sqrt() 求算数平方根 */tf.setText(String.valueOf(df.format(Math.sqrt(i);elsetf.setText(负数不能开平方根);/* *用log函数求自然对数 */else if(str.equals(ln)double i = Double.parseDouble(tf.getText();if(i0)tf.setText(String.valueOf(df.format(Math.log(i);elsetf.setTe
27、xt(负数不能求对数);/* %求百分比 */else if(str.equals(%)tf.setText(String.valueOf(df.format(Double.parseDouble(tf.getText() / 100);/* 1/x求倒数 */else if(str.equals(1/x)if(Double.parseDouble(tf.getText() = 0)tf.setText(除数不能为零);elsetf.setText(String.valueOf(df.format(1 / Double.parseDouble(tf.getText();/* sin求正弦函数
28、*/else if(str.equals(sin)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.sin(i);/* cos求余弦函数 */else if(str.equals(cos)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.cos(i);/* tan求正切函数 */else if(str.equals(tan)double i = Double.p
29、arseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.tan(i);/* n!求阶乘 */else if(str.equals(n!)double i = Double.parseDouble(tf.getText();if(i%2=0)|(i%2=1)/判断为整数放进行阶乘操作int j = (int)i;/强制类型转换int result=1;for(int k=1;k 0)if(tf.getText().length() 1)tf.setText(tf.getText().substring(0, tf.getT
30、ext().length() - 1);/使用退格删除最后一位字符/* * 函数:substring(int beginIndex, int endIndex) * 它的返回值为一个新的字符串,为原字符串的子串 * beginIndex为子字符串开始的地方,endIndex为子字符串结束的地方 */elsetf.setText(0);IfResult = true;elseif(tf.getText().length() 1)tf.setText(tf.getText().substring(0, tf.getText().length() - 1);elsetf.setText(0);IfResult = true;else if(str = CE)tf.setText(0);IfResult = true;/* * 数字输入的事件监听 */class Num implements ActionListenerpublic void actionPerformed(ActionEvent e) String str = e.getActionCommand();if(IfResult)tf.setText();IfResul
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023国家能源投资集团有限责任公司第一批社会招聘笔试备考试题及完整答案详解一套
- 2025年黑龙江省五常市辅警招聘考试试题题库完整答案详解
- 2025年Z世代消费趋势预测:新消费品牌发展前景深度报告
- 2025年K2学校STEM课程实施效果评估与教育评价体系创新实践报告
- 慢性阻塞性肺疾病诊治指南变迁2025
- 《模拟导游教程》中职旅游管理专业全套教学课件
- 宪法学试题及参考答案
- 广东省清远市佛冈县高中联考2024-2025学年高一下学期6月月考英语试卷
- 2025年重庆市西南大学附属中学中考三模道德与法治试卷(含答案)
- 2024-2025福建省厦门市集美中学高一下6月英语月考英语试卷
- 风力发电设备维修合同
- 跨文化交际智慧树知到期末考试答案章节答案2024年苏州大学
- 湖南省怀化市2022-2023学年五年级下学期语文期末试卷(含答案)
- DZ∕T 0004-2015 重力调查技术规范(150 000)(正式版)
- 《酒店消防安全培训》课件完整版
- 二手人防车位使用权转让协议书
- 地质行业安全培训课件
- 中医培训课件:《中药热奄包技术》
- 小班健康:不乱扔垃圾
- 河南省城市生命线安全工程建设指引V1
- 电力电气 纺织厂供配电系统设计
评论
0/150
提交评论