简单图形界面计算器的设计_java课程设计报告

上传人:1666****666 文档编号:37466908 上传时间:2021-11-03 格式:DOC 页数:32 大小:345.50KB
收藏 版权申诉 举报 下载
简单图形界面计算器的设计_java课程设计报告_第1页
第1页 / 共32页
简单图形界面计算器的设计_java课程设计报告_第2页
第2页 / 共32页
简单图形界面计算器的设计_java课程设计报告_第3页
第3页 / 共32页
资源描述:

《简单图形界面计算器的设计_java课程设计报告》由会员分享,可在线阅读,更多相关《简单图形界面计算器的设计_java课程设计报告(32页珍藏版)》请在装配图网上搜索。

1、Java 课程设计简单图形界面计算器的设计课程名称 Java程序设计 选题名称 简单图形界面计算器的设计 专 业 班 级 姓 名 学 号 指导教师 简单图形界面计算器的设计一、设计任务与目标本次java程序设计我的设计任务是设计一个图形界面(GUI)的计算器应用程序并且能够完成简单的算术运算。本次任务的基本要求是这个计算器应用程序可以完成十进制的加、减、乘、除、求倒、取余、开方运算,且有小数点、正负号、退格和清零功能。而我要在此基础上添加一项千位符分隔符的功能,即以三位为一级,在输入的一串数字中每三位加入一个逗号,这项功能国际通用,并已经成为惯例,会计记账都用这种方法便于账目核算与管理。GUI

2、计算器设计的具体目标:1. 完成十进制的加、减、乘、除、求倒、取余和开方运算;2. 有小数点和正负号加入运算;3. 有退格、复位和清零的功能;4. 有千位符分隔符的功能,即在输入的一串数字中每三位加入一个逗号。二、方案设计与论证1.设计目标的总体分析(1)设计目标的需求分析:计算器是现在一个普遍应用的工具,能够解决许多人工所无法计算的数据,节省大量宝贵的时间。(2)设计目标的功能分析:实现计算器系统的功能,主要有两个功能模块:输入 和 输出。(3)设计原则:基于计算器系统要具有适用性广、操作简便等特点,本系统预计要达到以下几个目标:满足以上的基本功能要求;能够在常见的计算机及其操作系统上运行。

3、2.设计的基本思路利用GUI的界面设计,将整个大设计分为三块,分别是数据的输入,运算符功能符的控制和数据的输入输出显示。利用Swing控件,数据的输入由09这10个按钮来表示,用“+”、“-”、“*”、“/”、“1/x”、“%”、“sqrt”这7个按钮来表示加、减、乘、除、求倒、取余、开方运算,用“.”和“”这2个按钮来表示小数点和正负号,用“Back”、“CE”和“C”这3个按钮来表示退格、复位和清零的功能,数据的输入输出显示由文本字段来表示。将计算器的总体界面设计好后,再将代码分别写入不同的按钮的源程序中。我要完成的一项改进,即添加一个拥有千位符分隔符功能的按钮,按下这个按钮能够在输入的一

4、串数字中每三位加入一个逗号并且显示出来。我要在之前的界面设计的基础上多添加一个按钮“$”来表示千位符分隔符,并且将功能代码写入这个按钮的源程序中。三、程序流程图,程序清单与调用关系1. 程序流程图:2. 程序清单09数据的输入private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: if(flag = false&!jTextField1.getText().equals(0) jTextField1.setText(jTextField1.

5、getText()+1); else jTextField1.setText(1); flag = false; “+”号的控制private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /加号 d1 = Double.parseDouble(jTextField1.getText(); flag = true; op = +; “-”号的控制private void jButton18ActionPerformed(java.awt.even

6、t.ActionEvent evt) / TODO add your handling code here: /减号 d1 = Double.parseDouble(jTextField1.getText(); flag = true; op = -; “*”号的控制private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /乘号 d1 = Double.parseDouble(jTextField1.getText(); flag = tr

7、ue; op = *; “/”号的控制 private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /除号 d1 = Double.parseDouble(jTextField1.getText(); flag = true; op = /; “%”取余运算private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling

8、code here: /求余 d1 = Double.parseDouble(jTextField1.getText(); flag = true; op = %; “.”号的加入private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /点号 if(jTextField1.getText().equals()|flag = true) jTextField1.setText(0.); else jTextField1.setText(jTe

9、xtField1.getText()+.); flag = false; “”号的加入private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /正负号 double d = Double.parseDouble(jTextField1.getText(); d = 0 - d; jTextField1.setText(+d); “sqrt”开方运算private void jButton21ActionPerformed(java.awt.

10、event.ActionEvent evt) / TODO add your handling code here: /开方 double d = Double.parseDouble(jTextField1.getText(); jTextField1.setText(+Math.sqrt(d); “1/x”求倒数运算private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /求倒数 double d = Double.parseDoubl

11、e(jTextField1.getText(); if(d=0) jTextField1.setText(0); else jTextField1.setText(+(1/d); “=”等号运算private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: if(op.equals() return; d2 = Double.parseDouble(jTextField1.getText(); double result = 0; if(op.eq

12、uals(+) result = d1 + d2; else if(op.equals(-) result = d1 - d2; else if(op.equals(*) result = d1 * d2; else if(op.equals(/) result = d1 / d2; if(d2 = 0) result = 0; else if(op.equals(%) result = d1 % d2; if(d2 = 0) result = 0; jTextField1.setText(+result); flag = true; “Back”退格运算private void jButto

13、n1ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /if语句判定当前字符串是否为空,若不为空,则将字符串长度减1之后,再赋值给原字符串;否则,将0复制给原字符串。 String s = jTextField1.getText(); if(!s.equals() s = s.substring(0,s.length()-1); if(s.equals() s = 0; jTextField1.setText(s); “CE”复位运算private void jButton2A

14、ctionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /复位操作 jTextField1.setText(0); d1 = 0; d2 = 0; op = ; flag = false; “C”清零运算private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /清零操作 jTextField1.setText(0); “$”千位符分隔

15、符运算private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here: /千位符分隔符 d1 = Double.parseDouble(jTextField1.getText(); flag = true; op = $; DecimalFormat f = new DecimalFormat(,#); jTextField1.setText(+f.format(d1); 3. 程序的调用关系图NewJFrameBackCEC=$09+-*/%1/x

16、sqrt.四、全部源程序清单/* Calculator.java GUI简单计算器 */package javacalculator;/* * * author */import java.awt.datatransfer.*;import java.text.DecimalFormat;public class NewJFrame extends javax.swing.JFrame /* * Creates new form NewJFrame */ public NewJFrame() initComponents(); flag = false; d1 = 0; d2 = 0; op

17、= ; jTextField1.setText(0); clipboard = getToolkit().getSystemClipboard(); private Clipboard clipboard; private boolean flag; private double d1; private double d2; private String op; SuppressWarnings(unchecked) / private void initComponents() jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.s

18、wing.JPanel(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); jButton10 = new javax.swing.JButton(); jButton11 = new ja

19、vax.swing.JButton(); jButton12 = new javax.swing.JButton(); jButton13 = new javax.swing.JButton(); jButton14 = new javax.swing.JButton(); jButton15 = new javax.swing.JButton(); jButton16 = new javax.swing.JButton(); jButton17 = new javax.swing.JButton(); jButton18 = new javax.swing.JButton(); jButto

20、n19 = new javax.swing.JButton(); jButton20 = new javax.swing.JButton(); jButton21 = new javax.swing.JButton(); jButton22 = new javax.swing.JButton(); jButton23 = new javax.swing.JButton(); jButton24 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.J

21、Button(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton4.setText(7); jButton4.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) j

22、Button4ActionPerformed(evt); ); jButton5.setText(8); jButton5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton5ActionPerformed(evt); ); jButton6.setText(9); jButton6.addActionListener(new java.awt.event.ActionListener() public

23、void actionPerformed(java.awt.event.ActionEvent evt) jButton6ActionPerformed(evt); ); jButton7.setText(4); jButton7.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton7ActionPerformed(evt); ); jButton8.setText(5); jButton8.addActi

24、onListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton8ActionPerformed(evt); ); jButton9.setText(6); jButton9.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton9ActionPe

25、rformed(evt); ); jButton10.setText(1); jButton10.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton10ActionPerformed(evt); ); jButton11.setText(2); jButton11.addActionListener(new java.awt.event.ActionListener() public void actio

26、nPerformed(java.awt.event.ActionEvent evt) jButton11ActionPerformed(evt); ); jButton12.setText(3); jButton12.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton12ActionPerformed(evt); ); jButton13.setText(0); jButton13.addActionLi

27、stener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton13ActionPerformed(evt); ); jButton14.setText(.); jButton14.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton14ActionPe

28、rformed(evt); ); jButton15.setText(); jButton15.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton15ActionPerformed(evt); ); jButton16.setText(/); jButton16.addActionListener(new java.awt.event.ActionListener() public void action

29、Performed(java.awt.event.ActionEvent evt) jButton16ActionPerformed(evt); ); jButton17.setText(*); jButton17.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton17ActionPerformed(evt); ); jButton18.setText(-); jButton18.addActionLis

30、tener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton18ActionPerformed(evt); ); jButton19.setText(+); jButton19.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton19ActionPer

31、formed(evt); ); jButton20.setText($); jButton20.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton20ActionPerformed(evt); ); jButton21.setText(sqrt); jButton21.addActionListener(new java.awt.event.ActionListener() public void act

32、ionPerformed(java.awt.event.ActionEvent evt) jButton21ActionPerformed(evt); ); jButton22.setText(1/x); jButton22.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton22ActionPerformed(evt); ); jButton23.setText(=); jButton23.addActi

33、onListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton23ActionPerformed(evt); ); jButton24.setText(%); jButton24.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton24Acti

34、onPerformed(evt); ); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(

35、53, 53, 53) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

36、t.TRAILING, false) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton15, javax.swing.GroupLayout.PREFER

37、RED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATE

38、D) .addComponent(jButton19) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton23, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGr

39、oup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)

40、.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

41、 .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addC

42、omponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE) .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParal

43、lelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone

44、ntPlacement.RELATED) .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing

45、.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButton17) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton21, javax.swing.GroupLayout.PREFERRED_SIZE, 70, java

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!