JAVA实训报告--简易计算器.doc_第1页
JAVA实训报告--简易计算器.doc_第2页
JAVA实训报告--简易计算器.doc_第3页
JAVA实训报告--简易计算器.doc_第4页
JAVA实训报告--简易计算器.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java实训设计报告Java实训设计报告电子信息工程学院专业:软件技术班级:1班 实训:简单计算器项目简介项目名称简单计算器项目功能1:使用图形用户界面:一个文本框;0-9数字按钮;加、减、乘、除运算符;“等于”符号;复位按钮;2:完成整数、实数的四则运算(加、减、乘、除);:3:通过按钮点击实现数据的复位(清零);:4:实数运算中小数位的精度一致。项目目的掌握计算器的各项基本功能掌握编写计算器程序的所用方法掌握Swing组件的应用掌握事件的应用主要技术Java编程语言、javaSwing组件开发环境jdk1.6.0_10项目特点使用组件构建的C/S应用程序各项功能的组织合理搭配应用图标控制功能实现技术重点构建基于C/S架构的应用程序Swing组件应用事件监听的应用方法的编写技术难点Swing组件的应用事件监听器的使用异常处理方法简便的选择项目名称一、项目需求 本程序的主要功能:(1)单击“简单计算器”上的数字按钮(09)可以设置参与计算的运算数。(2)单击“简单计算器”上的运算符按钮(+、-、*、/、1/x、sqrt、%)可以选择运算符号。(3)单击“简单计算器”上的“=”按钮显示计算结果。(4)单击“简单计算器”上的“C”按钮清空计算结果。(5)单击“简单计算器”上的“Backspace”按钮删除输入的最后一个数字。二、项目设计在设计计算器时,编写1个Java源文件:SimpleCalculator.java,计算器除了上述Java源文件所给出的类外,还需要Java系统提供的一些重要类,如JButton,JTextField等。下面是Java源文件的总体设计。SimpleCalculator(主类)SimpleCalculator类负责创建计算器的主窗口,该类含有main方法,计算器从该类开始执行。SimpleCalculator类有3种类型的对象,分别是:JPanel、JButton、JTextField。还有boolean canClick;是否单击 double memd;int memi; double tempResult,result;显示的结果 short op=0; 1、SimpleCalculator类(主类)(1)数据和方法SimpleCalculator类是javax.swing包中JFrame的一个子类,并实现了ActionListener接口。类中有关数据和方法的详细说明。1)成员变量btnNumber是数组,该数组长度为10。btnNumber数组中的“数字按钮”含有的数字依次为09。每个“数字按钮”都注册有ActionEvent事件监听器。2)方法main(String args)方法是计算器程序运行的入口方法。SimpleCalculator()是构造方法,负责完成窗口的初始化。(2)代码:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SimpleCalculator extends JFrame implements ActionListenerJPanel p,p1,p2;JTextField tResult;JButton btnBK,btnC;JButton btnNumber=new JButton10; JButton btnAdd,btnSub,btnMul,btnDiv,btnEqual,btnDot,btnSign;JButton btnSqrt,btnMod,btnReciprocal;boolean canClick;double memd;int memi;double tempResult,result;short op=0;public SimpleCalculator()canClick=true;result=0;tResult=new JTextField(15);tResult.setEditable(false);tResult.setBackground(Color.WHITE);btnBK=new JButton(Backspace);btnC=new JButton(C);for( int i=0;i10;i+)btnNumberi=new JButton(Integer.toString(i);btnAdd=new JButton(+);btnSub=new JButton(-);btnMul=new JButton(*);btnDiv=new JButton(/);btnMod=new JButton(%);btnSqrt=new JButton(Sqrt);btnReciprocal=new JButton(1/x);btnEqual=new JButton(=);btnDot=new JButton(.);btnSign=new JButton(+/-);Container c=this.getContentPane();p1=new JPanel(new FlowLayout(FlowLayout.RIGHT);p2=new JPanel(new GridLayout(4,5,3,3);c.add(tResult,BorderLayout.NORTH);c.add(p1,BorderLayout.CENTER);c.add(p2,BorderLayout.SOUTH);p1.setBackground(new Color(44,00,180);p2.setBackground(new Color(44,00,180);p1.add(btnBK);p1.add(btnC);p2.add(btnNumber7);p2.add(btnNumber8);p2.add(btnNumber9);p2.add(btnDiv);p2.add(btnSqrt);p2.add(btnNumber4);p2.add(btnNumber5);p2.add(btnNumber6);p2.add(btnMul);p2.add(btnMod);p2.add(btnNumber1);p2.add(btnNumber2);p2.add(btnNumber3);p2.add(btnSub);p2.add(btnReciprocal);p2.add(btnNumber0);p2.add(btnSign);p2.add(btnDot);p2.add(btnAdd);p2.add(btnEqual);for( int i=0;i10;i+)btnNumberi.addActionListener(this);btnBK.addActionListener(this);btnC.addActionListener(this);btnAdd.addActionListener(this);btnSub.addActionListener(this);btnMul.addActionListener(this);btnDiv.addActionListener(this);btnMod.addActionListener(this);btnSqrt.addActionListener(this);btnReciprocal.addActionListener(this);btnEqual.addActionListener(this);btnDot.addActionListener(this);btnSign.addActionListener(this);this.setTitle(简单计算器);this.setLocation(200, 200);this.pack();this.setVisible(true);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent ae)Object eSrc=ae.getSource();for( int i=0;i10;i+)if(eSrc=btnNumberi&canClick=true)tResult.setText(tResult.getText()+Integer.toString(i);if(eSrc=btnDot&canClick=true)boolean hasDot=false;String s=tResult.getText();if(s.length()=0)hasDot=true;for(int i=0;is.length();i+)if(s.charAt(i)=.)hasDot=true;break;if(hasDot=false)tResult.setText(s+.);if(eSrc=btnAdd|eSrc=btnSub|eSrc=btnMul|eSrc=btnDiv)&canClick=true)if(eSrc=btnAdd)tempResult=Double.parseDouble(tResult.getText();tResult.setText();op=1;if(eSrc=btnSub)tempResult=Double.parseDouble(tResult.getText();tResult.setText();op=2;if(eSrc=btnMul)tempResult=Double.parseDouble(tResult.getText();tResult.setText();op=3;if(eSrc=btnDiv)if(Double.parseDouble(tResult.getText()=0)tResult.setText(注意:除数不可为0);canClick=false;elsetempResult=Double.parseDouble(tResult.getText();tResult.setText();op=4;if(eSrc=btnEqual&canClick=true)if(op=1)result=tempResult+Double.parseDouble(tResult.getText();tResult.setText(Double.toString(result);if(op=2)result=tempResult-Double.parseDouble(tResult.getText();tResult.setText(Double.toString(result);if(op=3)result=tempResult*Double.parseDouble(tResult.getText();tResult.setText(Double.toString(result);if(op=4)if(Double.parseDouble(tResult.getText()=0)tResult.setText(除数不能为零);canClick=false;elseresult=tempResult/Double.parseDouble(tResult.getText();tResult.setText(Double.toString(result);tempResult=0;if(eSrc=btnMod&canClick=true)String s=tResult.getText();boolean hasDot=false;for(int i=0;is.length();i+)if(s.charAt(i)=.)hasDot=true;break;if(hasDot=true)double temp=Double.parseDouble(s);temp=temp/100.0;tResult.setText(Double.toString(temp);elseif(Integer.parseInt(s)%100=0)int itemp=Integer.parseInt(s);itemp=itemp/100;tResult.setText(Integer.toString(itemp);elsedouble dtemp=Double.parseDouble(s);dtemp=dtemp/100.0;tResult.setText(Double.toString(dtemp);if(eSrc=btnSqrt&canClick=true)String s=tResult.getText();if(s.charAt(0)=-)tResult.setText(注意:负数不可开根号);canClick=false;elsetResult.setText(Double.toString(Math.sqrt(Double.parseDouble(s);if(eSrc=btnReciprocal&canClick=true) String s=tResult.getText();if(s.charAt(0)=0&s.length()=1)tResult.setText(注意:0不能求倒数);canClick=false;elseboolean hasDot=false;for(int i=0;is.length();i+)if(s.charAt(i)=.)hasDot=true;break;if(hasDot=true)double dtemp=Double.parseDouble(s);dtemp=1.0/dtemp;tResult.setText(Double.toString(dtemp);elseint itemp=Integer.parseInt(s);double dtemp=1.0/(double)itemp;tResult.setText(Double.toString(dtemp);if(eSrc=btnSign&canClick=true)boolean isNum=true;String s=tResult.getText();for(int i=0;i=0&s.charAt(i)=9|s.charAt(i)=.|s.charAt(i)=-)isNum=false;break;if(isNum=true)if(s.charAt(0)=-)tResult.setText();for(int i=1;is.length();i+)char ch=s.charAt(i)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论