童鞋们,是不是有使用计算器的时候,还要进入运行,输入calc,太麻烦了,有时候甚至还忘记单词怎么拼写,呵呵
程序员自己写代码实现,又简单,又方便啊

以下为代码(想要生成可执行工具可参考:http://www.cnblogs.com/holdon521/p/4483966.html

  1. package com;
  2.  
  3. import java.awt.BorderLayout;
  4.  
  5. import java.awt.EventQueue;
  6.  
  7. import java.awt.Font;
  8.  
  9. import java.awt.GridLayout;
  10.  
  11. import java.awt.event.ActionEvent;
  12.  
  13. import java.awt.event.ActionListener;
  14.  
  15. import javax.swing.JButton;
  16.  
  17. import javax.swing.JFrame;
  18.  
  19. import javax.swing.JPanel;
  20.  
  21. import javax.swing.JTextField;
  22.  
  23. import javax.swing.SwingConstants;
  24.  
  25. import javax.swing.UIManager;
  26.  
  27. import javax.swing.border.EmptyBorder;
  28.  
  29. public class Test extends JFrame{
  30.  
  31. /**
  32.  
  33. * humphrey
  34.  
  35. */
  36.  
  37. private static final long serialVersionUID = -9075562467166618473L;
  38.  
  39. private JPanel contentPane;
  40.  
  41. private JTextField display;
  42.  
  43. private ActionListener insert = new InsertAction();
  44.  
  45. private ActionListener command = new CommandAction();
  46.  
  47. private double result = 0;
  48.  
  49. private String lastCommand = "=";
  50.  
  51. private boolean start = true;
  52.  
  53. /**
  54.  
  55. * Launch the application.
  56.  
  57. */
  58.  
  59. public static void main(String[] args) {
  60.  
  61. try {
  62.  
  63. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  64.  
  65. } catch (Throwable e) {
  66.  
  67. e.printStackTrace();
  68.  
  69. }
  70.  
  71. EventQueue.invokeLater(new Runnable() {
  72.  
  73. public void run() {
  74.  
  75. try {
  76.  
  77. Test frame = new Test();
  78.  
  79. frame.setVisible(true);
  80.  
  81. } catch (Exception e) {
  82.  
  83. e.printStackTrace();
  84.  
  85. }
  86.  
  87. }
  88.  
  89. });
  90.  
  91. }
  92.  
  93. /**
  94.  
  95. * Create the frame.
  96.  
  97. */
  98.  
  99. public Test() {
  100.  
  101. setTitle("\u8BA1\u7B97\u5668");
  102.  
  103. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  104.  
  105. setLocationByPlatform(true);
  106.  
  107. contentPane = new JPanel();
  108.  
  109. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  110.  
  111. contentPane.setLayout(new BorderLayout(0, 0));
  112.  
  113. setContentPane(contentPane);
  114.  
  115. JPanel displayPanel = new JPanel();
  116.  
  117. contentPane.add(displayPanel, BorderLayout.NORTH);
  118.  
  119. display = new JTextField();
  120.  
  121. display.setText("0");
  122.  
  123. display.setHorizontalAlignment(SwingConstants.RIGHT);
  124.  
  125. display.setEditable(false);
  126.  
  127. display.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  128.  
  129. displayPanel.add(display);
  130.  
  131. display.setColumns(13);
  132.  
  133. JPanel buttonPanel = new JPanel();
  134.  
  135. contentPane.add(buttonPanel, BorderLayout.CENTER);
  136.  
  137. buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
  138.  
  139. JButton number7Button = new JButton("7");
  140.  
  141. number7Button.addActionListener(insert);
  142.  
  143. number7Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  144.  
  145. buttonPanel.add(number7Button);
  146.  
  147. JButton number8Button = new JButton("8");
  148.  
  149. number8Button.addActionListener(insert);
  150.  
  151. number8Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  152.  
  153. buttonPanel.add(number8Button);
  154.  
  155. JButton number9Button = new JButton("9");
  156.  
  157. number9Button.addActionListener(insert);
  158.  
  159. number9Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  160.  
  161. buttonPanel.add(number9Button);
  162.  
  163. JButton divideButton = new JButton("/");
  164.  
  165. divideButton.addActionListener(command);
  166.  
  167. divideButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  168.  
  169. buttonPanel.add(divideButton);
  170.  
  171. JButton number4Button = new JButton("4");
  172.  
  173. number4Button.addActionListener(insert);
  174.  
  175. number4Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  176.  
  177. buttonPanel.add(number4Button);
  178.  
  179. JButton number5Button = new JButton("5");
  180.  
  181. number5Button.addActionListener(insert);
  182.  
  183. number5Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  184.  
  185. buttonPanel.add(number5Button);
  186.  
  187. JButton number6Button = new JButton("6");
  188.  
  189. number6Button.addActionListener(insert);
  190.  
  191. number6Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  192.  
  193. buttonPanel.add(number6Button);
  194.  
  195. JButton multiplyButton = new JButton("*");
  196.  
  197. multiplyButton.addActionListener(command);
  198.  
  199. multiplyButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  200.  
  201. buttonPanel.add(multiplyButton);
  202.  
  203. JButton number3Button = new JButton("1");
  204.  
  205. number3Button.addActionListener(insert);
  206.  
  207. number3Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  208.  
  209. buttonPanel.add(number3Button);
  210.  
  211. JButton number2Button = new JButton("2");
  212.  
  213. number2Button.addActionListener(insert);
  214.  
  215. number2Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  216.  
  217. buttonPanel.add(number2Button);
  218.  
  219. JButton number1Button = new JButton("3");
  220.  
  221. number1Button.addActionListener(insert);
  222.  
  223. number1Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  224.  
  225. buttonPanel.add(number1Button);
  226.  
  227. JButton subtractButton = new JButton("-");
  228.  
  229. subtractButton.addActionListener(command);
  230.  
  231. subtractButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  232.  
  233. buttonPanel.add(subtractButton);
  234.  
  235. JButton number0Button = new JButton("0");
  236.  
  237. number0Button.addActionListener(insert);
  238.  
  239. number0Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  240.  
  241. buttonPanel.add(number0Button);
  242.  
  243. JButton dotButton = new JButton(".");
  244.  
  245. dotButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  246.  
  247. buttonPanel.add(dotButton);
  248.  
  249. JButton equalButton = new JButton("=");
  250.  
  251. equalButton.addActionListener(command);
  252.  
  253. equalButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  254.  
  255. buttonPanel.add(equalButton);
  256.  
  257. JButton addButton = new JButton("+");
  258.  
  259. addButton.addActionListener(command);
  260.  
  261. addButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
  262.  
  263. buttonPanel.add(addButton);
  264.  
  265. pack();
  266.  
  267. }
  268.  
  269. private class InsertAction implements ActionListener {
  270.  
  271. public void actionPerformed(ActionEvent e) {
  272.  
  273. String input = e.getActionCommand();
  274.  
  275. String text = display.getText();
  276.  
  277. if (start) {
  278.  
  279. display.setText("");
  280.  
  281. start = false;
  282.  
  283. }
  284.  
  285. if (text.startsWith(".")) {
  286.  
  287. display.setText("0" + display.getText() + input);
  288.  
  289. } else if (text.startsWith("-0.") || text.startsWith("0.")) {
  290.  
  291. display.setText(display.getText() + input);
  292.  
  293. } else if (text.startsWith("-0")) {
  294.  
  295. display.setText("-" + input);
  296.  
  297. } else if (text.startsWith("0")) {
  298.  
  299. display.setText(input);
  300.  
  301. } else {
  302.  
  303. display.setText(display.getText() + input);
  304.  
  305. }
  306.  
  307. }
  308.  
  309. }
  310.  
  311. private class CommandAction implements ActionListener {
  312.  
  313. public void actionPerformed(ActionEvent e) {
  314.  
  315. String command = e.getActionCommand();
  316.  
  317. if (start) {
  318.  
  319. if (command.equals("-")) {
  320.  
  321. display.setText(command);
  322.  
  323. start = false;
  324.  
  325. } else {
  326.  
  327. lastCommand = command;
  328.  
  329. }
  330.  
  331. } else {
  332.  
  333. calculate(Double.parseDouble(display.getText()));
  334.  
  335. lastCommand = command;
  336.  
  337. start = true;
  338.  
  339. }
  340.  
  341. }
  342.  
  343. }
  344.  
  345. public void calculate(double x) {
  346.  
  347. char operator = lastCommand.charAt(0);
  348.  
  349. switch (operator) {
  350.  
  351. case '+':
  352.  
  353. result += x;
  354.  
  355. break;
  356.  
  357. case '-':
  358.  
  359. result -= x;
  360.  
  361. break;
  362.  
  363. case '*':
  364.  
  365. result *= x;
  366.  
  367. break;
  368.  
  369. case '/':
  370.  
  371. result /= x;
  372.  
  373. break;
  374.  
  375. case '=':
  376.  
  377. result = x;
  378.  
  379. break;
  380.  
  381. }
  382.  
  383. display.setText("" + result);
  384.  
  385. }
  386.  
  387. }

java实现简单计算器功能的更多相关文章

  1. [Java.web]简单计算器

    项目的  WebRoot 目录下的 calculator.jsp <%@ page language="java" import="java.util.*" ...

  2. Java实现简单计算器、抽票程序

    计算器: import java.awt.BorderLayout; import java.awt.Container; import java.awt.Font; import java.awt. ...

  3. java实现简单计算器

    首先利用字符串数组保存计算器上的按钮的标签名 private final String[] str = {"7","8","9"," ...

  4. Android-Kotlin简单计算器功能

    上一篇博客 Android-Kotlin-配置/入门 配置好了 AndroidStudio Kotlin 的环境: 选择包名,然后右键: 选择Class类型,会有class: 创建CounterCla ...

  5. s12-day04-work01 简单计算器功能实现

    代码: #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo. ...

  6. Python之实现简单计算器功能

    一,需求分析 要求计算一串包含数字+-*/()的类似于3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4)表达式的数值 二,知识点 正 ...

  7. 简单计算器 安卓 Android 作业

    Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com ...

  8. 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能

    #!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...

  9. Java语言编写计算器(简单的计算器)

    Java编写的一个简单计算器,本人还比较菜,只能这样了,有点代码冗余,不能连续计算. import javax.swing.*; import java.awt.*; import java.awt. ...

随机推荐

  1. python的返回值

    1.返回值的作用 函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值.函数返回的值被称为返回值.在函数中,可使用return语句将值返回到调用函数的代码行.返回值让你能够将程序的大 ...

  2. 服务器word权限添加

    我们的程序部署到IIS上会出现上图所示的错误. 这个是由于IIS没有权限访问word所致.   解决的方法: 在运行对话框中输入: mmc -32 出来如下图的界面:   然后点击"文件&q ...

  3. scss-比较运算符

    与JavaScript类似,scss中也有比较运算符,下面就分别做一下介绍. 一.==相等和!=不相等运算符: 此运算符用来判断两个操作数是否相等. 特别说明: 上面两个运算符,支持类似于JavaSc ...

  4. Ubuntu16.04安装Hadoop2.6+Spark1.6+开发实例

    Ubuntu16.04安装Hadoop2.6+Spark1.6,并安装python开发工具Jupyter notebook,通过pyspark测试一个实例,調通整个Spark+hadoop伪分布式开发 ...

  5. Git 基本知识与常用指令

    一.Git代码状态转换图 其中: 未被Git跟踪的状态为unstage状态: 已被Git跟踪的状态为stage状态(stage:阶段),因此包括staging状态和staged状态. untrack ...

  6. python 读写Oracle10g数据简介

    1.测试环境: Centos6 X86_64python 2.6 Oracle 10g 2.安装cx_Oracle 和 Oracle InstantClient: http://www.rpmfind ...

  7. 随学笔记 partAdded

    随学笔记: RectangularDropShadow为矩形对象添加阴影,DropShadowFilter可以为任意形状对象添加阴影. BorderContainer和Panel等容器使用的就是Rec ...

  8. linux搭建nginx图片服务器

    1:参考http://blog.csdn.net/u012401711/article/details/53525908

  9. ExecutorService的invokeAny方法

    一.此方法获得最先完成任务的结果,即Callable<T>接口中的call的返回值,在获得结果时,会中断其他正在执行的任务 示例代码: import java.util.ArrayList ...

  10. SQL转Linq工具的使用——Linqer

    官方下载网站:http://www.sqltolinq.com/ 本文介绍版本为Linqer 4.5.7 第一步:下载下来,解压,双击安装.exe文件,运行界面如下. 第二步:建立与数据库的连接 点击 ...