java实现简单的计算器(共19页).doc
精选优质文档-倾情为你奉上目 录专心-专注-专业1 需求分析 该计算器程序除了具备加减乘除基本功能外,还有清零键C、CE和退格键Backspace,和一些部分的科学计算方法,包括开方、求倒、百分比,由于时间问题,之后会完善键盘事件的监听功能。1.1 计算器的基本功能:1.1.1 加法运算:用数字按钮和“+”按钮进行运算;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 角度换算成弧度:用数字按钮和“ ”按钮进行运算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) 由于该组件按钮较多,设计一个方法简化按钮的设置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 implements 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.CENTER);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 它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科学运算方法.类中包含了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 myBar;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("科学计算器");/设置标题栏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);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 = 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)");seeItem1.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 ButtonGroup();/选项组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);/* * 文本域,即为计算器的屏幕显示区域 */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(), Color.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(panel2, "÷", 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(), Color.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(panel2, "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(), 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);addButton(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);this.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.setForeground(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);/* * 运算符号的事件监听 */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)/* * 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(i>0)tf.setText(String.valueOf(df.format(Math.log(i);elsetf.setText("负数不能求对数");/* %求百分比 */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求正弦函数 */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.parseDouble(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<=j;k+)result *= k;tf.setText(String.valueOf(result);elsetf.setText("无法进行阶乘");/* x2求平方 */else if(str.equals("x2")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(i*i);/* x3求立方 */else if(str.equals("x3")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(i*i*i);/* 角度转换 */* * 将角度值转换成弧度值,方便三角函数的计算 */else if(str.equals("")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(i/180*Math.PI);elseif(flag)IfResult = false;if(IfResult)oper = str;else getResult(Double.parseDouble(tf.getText();oper = str;IfResult = true;/* * 清除按钮的事件监听 */class Clear implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent对象的getActionCommand()方法取得与引发事件对象的字符串 */String str = e.getActionCommand();if(str = "C")tf.setText("0");IfResult = true;result = 0;else if(str = "-/+")double i = 0 - Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(i);else if(str = "Backspace")if(Double.parseDouble(tf.getText() > 0)if(tf.getText().length() > 1)tf.setText(tf.getText().substring(0, tf.getText().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("");IfResult = false;if(str="")tf.setText(String.valueOf(Math.PI);else if(str="e")tf.setText(String.valueOf(Math.E);elsetf.setText(tf.getText()+ str);if(tf.getText().equals("0")/为了能够输入绝对值小于1的小数并不会重复显示多个0tf.setText("0");IfResult = true;flag = true;/* * 小数点的事件监听 */class Dot implements ActionListenerpublic void actionPerformed(ActionEvent e) IfResult = false;/在已显示的文本中查找是否有"."出现过,没有则加上"."if(tf.getText().indexOf(".") = -1)tf.setText(tf.getText() + ".");/* * 储存器的事件监听 */class Memory implements ActionListenerpublic void actionPerformed(ActionEvent e)String str = e.getActionCommand();if(str.equals("MC")memory = 0;else if(str.equals("MS")memory = Double.parseDouble(tf.getText();else if(str.equals("MR")tf.setText(String.valueOf(memory);else if(str.equals("M-")memory -= Double.parseDouble(tf.getText();else if(str.equals("M+")memory += Double.parseDouble(tf.getText();IfResult = true;/* * main方法 */public static void main(String args) Calculator ca = new Calculator(); 4 测试 4.1 实现加法运算:4+12=164.2 实现乘法运算:3*9=274.3 用”C实现清零功能:4.4 用”Backspace”实现退格功能:4.5 求倒数:1/4=0.25