简单的java计算器项目

 

题目:java计算器项目

一、 题目简介: 一个能进行加减乘除四则运算的小程序

Github链接:https://github.com/lizhenbin/test/tree/master/Calculator

 
 

代码如下:

import java.awt.; import java.awt.event.; import javax.swing.*;

public class JCalculator extends JFrame implements ActionListener {

  1. private static final long serialVersionUID = -169068472193786457L;
  2. //关闭计算器
  3. private class WindowCloser extends WindowAdapter {
  4. public void windowClosing(WindowEvent we) {
  5. System.exit(0);
  6. }
  7. }
  8. int i;
  9. //添加数字按钮
  10. private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1",
  11. "2", "3", "-", ".", "0", "=", "+" };
  12. JButton[] buttons = new JButton[str.length];
  13. JButton reset = new JButton("CE");
  14. // 编译
  15. JTextField display = new JTextField("0");
  16. public JCalculator() {
  17. super("Calculator");
  18. JPanel panel1 = new JPanel(new GridLayout(4, 4));
  19. for (i = 0; i < str.length; i++) {
  20. buttons[i] = new JButton(str[i]);
  21. panel1.add(buttons[i]);
  22. }
  23. JPanel panel2 = new JPanel(new BorderLayout());
  24. panel2.add("Center", display);
  25. panel2.add("East", reset);
  26. getContentPane().setLayout(new BorderLayout());
  27. getContentPane().add("North", panel2);
  28. getContentPane().add("Center", panel1);
  29. // 添加监听器
  30. for (i = 0; i < str.length; i++)
  31. buttons[i].addActionListener(this);
  32. reset.addActionListener(this);
  33. // 添加一个显示按钮
  34. display.addActionListener(this);
  35. // 关闭按钮“X”
  36. addWindowListener(new WindowCloser());
  37. // 改变窗口大小
  38. setSize(800, 800);
  39. setVisible(true);
  40. pack();
  41. }
  42. public void actionPerformed(ActionEvent e) {
  43. Object target = e.getSource();
  44. String label = e.getActionCommand();
  45. if (target == reset)
  46. handleReset();
  47. else if ("0123456789.".indexOf(label) > 0)
  48. handleNumber(label);
  49. else
  50. handleOperator(label);
  51. }
  52. boolean isFirstDigit = true;
  53. public void handleNumber(String key) {
  54. if (isFirstDigit)
  55. display.setText(key);
  56. else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))
  57. display.setText(display.getText() + ".");
  58. else if (!key.equals("."))
  59. display.setText(display.getText() + key);
  60. isFirstDigit = false;
  61. }
  62. public void handleReset() {
  63. display.setText("0");
  64. isFirstDigit = true;
  65. operator = "=";
  66. }
  67. double number = 0.0;
  68. String operator = "=";
  69. public void handleOperator(String key) {
  70. if (operator.equals("+"))
  71. number += Double.valueOf(display.getText());
  72. else if (operator.equals("-"))
  73. number -= Double.valueOf(display.getText());
  74. else if (operator.equals("*"))
  75. number *= Double.valueOf(display.getText());
  76. else if (operator.equals("/"))
  77. number /= Double.valueOf(display.getText());
  78. else if (operator.equals("="))
  79. number = Double.valueOf(display.getText());
  80. display.setText(String.valueOf(number));
  81. operator = key;
  82. isFirstDigit = true;
  83. }
  84. public static void main(String[] args) {
  85. new JCalculator();
  86. }

}

三、 结对分工情况 代码编写:李振斌 测试,校对:张义军

四、 所设计的模块测试用例、测试结果截图

五、问题及解决方案、心得体会 本次实验设计了一个简单的java加速器程序,进一步学习了Github的使用方法, 进一步学习了Junit4的相关知识,增进了对Java的理解,对合作学习有了更清晰的认识。 两人协作编程更容易找到错误,对编程很有帮助。

java计算器项目的更多相关文章

  1. 结对项目3-功能增强型带基本函数计算java计算器

    -----------------------------------------------------实验报告------------------------------------------- ...

  2. JAVA WEB项目中各种路径的获取

    JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在s ...

  3. 怎么将java web 项目导入idea 中

    1.将 java web 项目导 入idea 中, 显示 然后进行 Configure 配置. 2. 点击 open module settings. 3. 4. 选择jar包. 5. 6. 配置to ...

  4. 在cmd命令行使用Maven Archetype插件 generate命令创建简单的java web项目

    前提: 1.下载apache-maven:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache ...

  5. Java Web项目报错java.lang.NullPointerException at org.apache.jsp.front.index_jsp._jspInit(index_jsp.java:30)

    环境:myeclipse+tomcat6+jdk6 今天搭建了一个Java Web项目,访问index.jsp时报如下错误: 严重: Servlet.service() for servlet jsp ...

  6. 使用Spring Boot来加速Java web项目的开发

    我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用 ...

  7. Linux(Centos)之安装tomcat并且部署Java Web项目

    1.准备工作 a.下载tomcat linux的包,地址:http://tomcat.apache.org/download-80.cgi,我们下载的版本是8.0,下载方式如图:          b ...

  8. 阿里云部署Java web项目初体验(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在 ...

  9. 【转】Java Web 项目获取运行时路径 classpath

    Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...

随机推荐

  1. January 29th, 2018 Week 05th Monday

    Losing all hope was freedom. 彻底绝望就是真正的自由. Losing all the hopes, and we are free to challenge everyth ...

  2. 推荐5款简洁美观的Hexo主题

    2018-11-17 17:15:46 原文地址:http://www.izhongxia.com 以下是 <hexo 主题列表> 中挑选出来一些比较简洁美观的主题(存在个人主观意识,请勿 ...

  3. Handler实现线程间的通信2

    与Handler实现线程间的通信1反过来MainThread中向WorkerThread中发送消息

  4. CNAME记录和A记录

    主机名:host.abcd.com 别名:一台主机可以提供多种服务,比如http服务和mail服务. 访问http服务就可以使用域名:www.abcd.com 访问mail服务就可以使用域名:mail ...

  5. day15 Python风湿理论之函数即变量

    eg1.定义foo门牌号,调用foo函数,打印,再找bar门牌号,找不到,报错 def foo(): print('from foo') bar() foo() 结果:报错 from foo Trac ...

  6. python开发技巧---列表、字典、集合值的过滤

    主要学习列表,字典,集合表达式的应用: 列表的解析式: 生成一个随机列表: In [4]: datalist = [randint(-10,10) for _ in range(10)] In [5] ...

  7. P1004 方格取数-洛谷luogu-dp动态规划

    题目描述 设有N \times NN×N的方格图(N \le 9)(N≤9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字00.如下图所示(见样例): A 0 0 0 0 0 0 0 0 ...

  8. 【Codeforces Round 1137】Codeforces #545 (Div. 1)

    Codeforces Round 1137 这场比赛做了\(A\).\(B\),排名\(376\). 主要是\(A\)题做的时间又长又交了两次\(wa4\)的. 这两次错误的提交是因为我第一开始想的求 ...

  9. ubuntu (14.04) 卸载 gnome 系统桌面

    1.将ubuntu 的图形界面切到命令行界面. 2.卸掉 gnome-shell 的主程序 sudo apt-get remove gnome-shell 3.卸载 gnome sudo apt-ge ...

  10. 利用 ProtoThreads实现Arduino多线程处理(2)

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 感谢小V分享给大家的博文. 我在做产品设计的课题的时候,小五义推荐我使用Protothrea ...