《Java实现的简易计算器-附代码(共12页).doc》由会员分享,可在线阅读,更多相关《Java实现的简易计算器-附代码(共12页).doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上 Java实验报告题目: 简易计算器 学 院 数学与计算机学院 学科门类 工 学 专 业 软件工程 学 号 姓 名 指导教师 2012年 12月 25 日一、实验目的通过Java程序开发实训使学生掌握面向对象程序设计(Java)课堂讲授的内容,灵活运用到Java程序的开发与设计中。1. 了解Java中的类与对象,类及成员的定义 2. 掌握运算符与基本的表达式,和异常的处理 3. 数组与字符串的应用 4. 掌握常用的Java系统类,核心语言包(java.lang),工具包(java.util),数学包(java.math) 5. 接口的实现,输入输出的处理和可视化程序设
2、计二、实验要求应用Java语言,设计一个简易计算器,要求界面简洁,能完成基本运算功能。三、实验内容1. 进行图形界面设计 应用JFrame类、JPanel类、5种布局管理器、文本框、标签及按钮等。2. 实现计算器的基本功能包括加减乘除四则运算、倒数、取余、平方根、三角函数及清除结果等功能。四、设计方案1、设计流程图2、界面的设计:Swing布局。界面设计中有用到swing组件的TextField和Button,用到awt中的BorderLayout和GridLayout布局管理方式。设计主界面框架布局为默认的BorderLayout布局,申请三个面板分别是:(1)输入输出面板,布局设置为两行一
3、列的网格GridLayout布局,包括两个JTextField文本框,一个负责显示当前输入信息,一个负责显示计算式。面板设置为面板的上部,即f.add(inputPanel,BorderLayout.NORTH);(2)数字按钮及运算按钮面板,设置为4行5列的网格布局,包括09的10个数字和加、减、乘、除、乘方、开方、取倒、取余、等于按钮。面板位置在主界面的下部,即f.add(buttonsPanel,BorderLayout.SOUTH);(3)功能键面板, 包括三角函数,清除,退格等按钮。面板位置在主界面框架的中部。即f.add(operationPanel,BorderLayout.CE
4、NTER)。其图形界面如下:3、数据输入:鼠标事件响应。分别定义三个类,一个是NumberMouseListener类来响应数字点击事件,另外一个是CalcMouseListener 类来响应包括开方、乘方在内的运算按钮的点击事件,最后是OperationMouseListener类用来响应退格、清除等功能键的点击事件,三个类都分别实现鼠标监听MouseListener接口。对每个按钮设置对应监听。4、数据处理:整型和浮点数处理,计算器的核心功能实现。在CalcMouseListener 类中对根据事件源的分析,完成用户要求的运算,将结果输出到文本框中。五、实验结果1、四则运算功能2、扩张的其
5、他运算功能六、程序代码public class Calculator /计算器面板private JFrame f = new JFrame(Calculator); private JPanel inputPanel = new JPanel();/输入面扳private JPanel operationPanel = new JPanel();/加减乘除面板private JPanel buttonsPanel = new JPanel();/数字面板private JTextField input = new JTextField(20); /输入数据文本框private JTextFi
6、eld show=new JTextField(20);private JButton backspace = new JButton(退格);/退格键private JButton CE = new JButton(CE );/清空private JButton C = new JButton(C);/删除private JButton sqrt=new JButton();/开方private JButton fang=new JButton();/乘方private JButton mod=new JButton(%);/取余private JButton dao=new JButton
7、(1/x);/取倒数private JButton sin=new JButton(sin);private JButton cos=new JButton(cos);private JButton lg=new JButton(ln);/四则运算符号键private JButton add = new JButton(+);private JButton sub = new JButton(-);private JButton mul = new JButton(*);private JButton div = new JButton(/);/小数点private JButton decim
8、al = new JButton(.);/等号private JButton equal = new JButton(=);/数字键private JButton zero = new JButton(0);private JButton one = new JButton(1);private JButton two = new JButton(2);private JButton three = new JButton(3);private JButton four = new JButton(4);private JButton five = new JButton(5);private
9、 JButton six = new JButton(6);private JButton seven = new JButton(7);private JButton eight = new JButton(8);private JButton nine = new JButton(9);private String num1 = ;/保存第一个运算数字private String operator = ;/保存运算符号public static void main(String args) new Calculator();/new计算器实例public Calculator() /添加组
10、件,布局show.setText(0);show.setEditable(false); /不可编辑show.setHorizontalAlignment(JTextField.RIGHT);input.setText(0);input .setEditable(false);input.setHorizontalAlignment(JTextField.RIGHT);inputPanel.setLayout(new GridLayout(2,1);inputPanel.add(show);inputPanel.add(input);/添加输入文本框f.add(inputPanel, Bord
11、erLayout.NORTH);operationPanel.add(sin);sin.setBackground(new Color(100, 255, 10);operationPanel.add(cos);cos.setBackground(new Color(100, 255, 10);operationPanel.add(CE);CE.setBackground(new Color(100, 255, 10);operationPanel.add(C);C.setBackground(new Color(100, 255, 10);operationPanel.add(backspa
12、ce);backspace.setBackground(new Color(100, 255, 10);f.add(operationPanel, BorderLayout.CENTER);buttonsPanel.setLayout(new GridLayout(4, 5); buttonsPanel.add(seven);buttonsPanel.add(eight);buttonsPanel.add(nine);buttonsPanel.add(div);buttonsPanel.add(mod);buttonsPanel.add(four);buttonsPanel.add(five)
13、;buttonsPanel.add(six);buttonsPanel.add(mul);buttonsPanel.add(fang);buttonsPanel.add(one);buttonsPanel.add(two);buttonsPanel.add(three);buttonsPanel.add(sub);buttonsPanel.add(sqrt);buttonsPanel.add(zero);buttonsPanel.add(decimal);buttonsPanel.add(equal);buttonsPanel.add(add);buttonsPanel.add(dao);f.
14、add(buttonsPanel, BorderLayout.SOUTH);/注册各个组件监听事件backspace.addMouseListener(new OperationMouseListener();CE.addMouseListener(new OperationMouseListener();C.addMouseListener(new OperationMouseListener();decimal.addMouseListener(new OperationMouseListener();equal.addMouseListener(new OperationMouseLis
15、tener();/注册运算监听add.addMouseListener(new CalcMouseListener();sub.addMouseListener(new CalcMouseListener();mul.addMouseListener(new CalcMouseListener();div.addMouseListener(new CalcMouseListener();sqrt.addMouseListener(new CalcMouseListener();fang.addMouseListener(new CalcMouseListener();mod.addMouseL
16、istener(new CalcMouseListener();dao.addMouseListener(new CalcMouseListener();sin.addMouseListener(new CalcMouseListener();cos.addMouseListener(new CalcMouseListener();/注册数字监听事件zero.addMouseListener(new NumberMouseListener();one.addMouseListener(new NumberMouseListener();two.addMouseListener(new Numb
17、erMouseListener();three.addMouseListener(new NumberMouseListener();four.addMouseListener(new NumberMouseListener();five.addMouseListener(new NumberMouseListener();six.addMouseListener(new NumberMouseListener();seven.addMouseListener(new NumberMouseListener();eight.addMouseListener(new NumberMouseLis
18、tener();nine.addMouseListener(new NumberMouseListener();f.setVisible(true);f.pack();/窗口自适应大小f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);private class NumberMouseListener implements MouseListenerpublic void mouseClicked(MouseEvent e) /if(operator=)if(input.getText().trim().equals(0)|input.getTex
19、t().trim().equals()/如果文本框已经是0,结果还是0 input.setText(JButton)e.getSource().getText().trim();else/否则的话,把新输入的数添加到原有数的后边input.setText(input.getText().concat(JButton)e.getSource().getText().trim();if(operator=)show.setText();public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) publi
20、c void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) private class CalcMouseListener implements MouseListener/如果输入的是运算符号,保存第一个结果和运算符号public void mouseClicked(MouseEvent e) double s=0;if(JButton)e.getSource().getText().trim()=- )if(input.getText().trim().equals(0)|input.getText()
21、.trim().equals()input.setText(-);return;if(e.getSource()=sqrt)double t=Math.sqrt(Double.parseDouble(input.getText().trim();input.setText(new DecimalFormat(0.00).format(t);else if(e.getSource()=dao)double t=1/Double.parseDouble(input.getText().trim();input.setText(new DecimalFormat(0.00).format(t);el
22、se if(e.getSource()=sin)double t=Math.sin(Double.parseDouble(input.getText().trim();input.setText(new DecimalFormat(0.00).format(t);else if(e.getSource()=cos)double t=Math.cos(Double.parseDouble(input.getText().trim();input.setText(new DecimalFormat(0.00).format(t);elseif(operator=)num1 = input.getT
23、ext().trim();operator = (JButton)e.getSource().getText().trim();show.setText(input.getText().concat(JButton)e.getSource().getText().trim();input.setText(0);else /实现连续运算if(operator.equals(+)/执行加法运算s = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim();else if(operator.equals(-)/减法运算
24、s = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim();else if(operator.equals(*)/乘法运算s = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim();else if(operator.equals(/)/除法运算s = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim();else if(operator.eq
25、uals()/乘方运算s=Math.pow(Double.parseDouble(num1), Double.parseDouble(input.getText().trim();else if(operator.equals(%)/取余运算s=getMod(Double.parseDouble(num1), Double.parseDouble(input.getText().trim();num1 =new DecimalFormat(0.00).format(s);show.setText(num1.concat(JButton)e.getSource().getText().trim(
26、);input.setText(0);operator=(JButton)e.getSource().getText().trim();private double getMod(double a, double b) / TODO Auto-generated method stubdouble s=0;if(a 0)input.setText(inputText.substring(0, inputText.length() - 1);else if(inputText.length()=0 & showText.length()0) input.setText(showText.subs
27、tring(0,showText.length()-1); show.setText(0);else if(e.getSource() = C)input.setText(0);/C,清空所有运算数字和符号show.setText(0);num1 = ;operator=;else if(e.getSource() = CE)input.setText(0);/CE-将文本框置为0else if(e.getSource() = decimal)String text = input.getText().trim();/如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数
28、点if(text.indexOf(.) = -1)input.setText(text.concat(.);else if(e.getSource() = equal)/输入操作符后 点击的等于号 两个数做正常计算if(!operator.trim().equals()if(!input.getText().trim().equals()show.setText(show.getText().concat(input.getText().concat(=);double result = 0;if(operator.equals(+)/执行加法运算result = Double.parseDo
29、uble(num1) + Double.parseDouble(input.getText().trim();else if(operator.equals(-)/减法运算result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim();else if(operator.equals(*)/乘法运算result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim();else if(operator.equals(/)/
30、除法运算result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim();else if(operator.equals()result=Math.pow(Double.parseDouble(num1), Double.parseDouble(input.getText().trim();else if(operator.equals(%)/取余运算result=getMod(Double.parseDouble(num1), Double.parseDouble(input.getText().tri
31、m(); /格式化最终结果,保留两位小数点input.setText(new DecimalFormat(0.00).format(result);operator=;num1=;private double getMod(double a, double b) / TODO Auto-generated method stubdouble s=0;if(ab)s=a;else if(a=b)s=0;elses=a%b;return s;public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) 专心-专注-专业