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

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

package com;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

import javax.swing.border.EmptyBorder;

public class Test  extends JFrame{

/**

   *  humphrey

   */

  private static final long serialVersionUID = -9075562467166618473L;

  private JPanel contentPane;

  private JTextField display;

  private ActionListener insert = new InsertAction();

  private ActionListener command = new CommandAction();

  private double result = 0;

  private String lastCommand = "=";

  private boolean start = true;

  /**

   * Launch the application.

   */

  public static void main(String[] args) {

      try {

          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

      } catch (Throwable e) {

          e.printStackTrace();

      }

      EventQueue.invokeLater(new Runnable() {

          public void run() {

              try {

                  Test frame = new Test();

                  frame.setVisible(true);

              } catch (Exception e) {

                  e.printStackTrace();

              }

          }

      });

  }

  /**

   * Create the frame.

   */

  public Test() {

      setTitle("\u8BA1\u7B97\u5668");

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setLocationByPlatform(true);

      contentPane = new JPanel();

      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

      contentPane.setLayout(new BorderLayout(0, 0));

      setContentPane(contentPane);

      JPanel displayPanel = new JPanel();

      contentPane.add(displayPanel, BorderLayout.NORTH);

      display = new JTextField();

      display.setText("0");

      display.setHorizontalAlignment(SwingConstants.RIGHT);

      display.setEditable(false);

      display.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      displayPanel.add(display);

      display.setColumns(13);

      JPanel buttonPanel = new JPanel();

      contentPane.add(buttonPanel, BorderLayout.CENTER);

      buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));

      JButton number7Button = new JButton("7");

      number7Button.addActionListener(insert);

      number7Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number7Button);

      JButton number8Button = new JButton("8");

      number8Button.addActionListener(insert);

      number8Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number8Button);

      JButton number9Button = new JButton("9");

      number9Button.addActionListener(insert);

      number9Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number9Button);

      JButton divideButton = new JButton("/");

      divideButton.addActionListener(command);

      divideButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(divideButton);

      JButton number4Button = new JButton("4");

      number4Button.addActionListener(insert);

      number4Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number4Button);

      JButton number5Button = new JButton("5");

      number5Button.addActionListener(insert);

      number5Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number5Button);

      JButton number6Button = new JButton("6");

      number6Button.addActionListener(insert);

      number6Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number6Button);

      JButton multiplyButton = new JButton("*");

      multiplyButton.addActionListener(command);

      multiplyButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(multiplyButton);

      JButton number3Button = new JButton("1");

      number3Button.addActionListener(insert);

      number3Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number3Button);

      JButton number2Button = new JButton("2");

      number2Button.addActionListener(insert);

      number2Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number2Button);

      JButton number1Button = new JButton("3");

      number1Button.addActionListener(insert);

      number1Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number1Button);

      JButton subtractButton = new JButton("-");

      subtractButton.addActionListener(command);

      subtractButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(subtractButton);

      JButton number0Button = new JButton("0");

      number0Button.addActionListener(insert);

      number0Button.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(number0Button);

      JButton dotButton = new JButton(".");

      dotButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(dotButton);

      JButton equalButton = new JButton("=");

      equalButton.addActionListener(command);

      equalButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(equalButton);

      JButton addButton = new JButton("+");

      addButton.addActionListener(command);

      addButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));

      buttonPanel.add(addButton);

      pack();

  }

  private class InsertAction implements ActionListener {

      public void actionPerformed(ActionEvent e) {

          String input = e.getActionCommand();

          String text = display.getText();

          if (start) {

              display.setText("");

              start = false;

          }

          if (text.startsWith(".")) {

              display.setText("0" + display.getText() + input);

          } else if (text.startsWith("-0.") || text.startsWith("0.")) {

              display.setText(display.getText() + input);

          } else if (text.startsWith("-0")) {

              display.setText("-" + input);

          } else if (text.startsWith("0")) {

              display.setText(input);

          } else {

              display.setText(display.getText() + input);

          }

      }

  }

  private class CommandAction implements ActionListener {

      public void actionPerformed(ActionEvent e) {

          String command = e.getActionCommand();

          if (start) {

              if (command.equals("-")) {

                  display.setText(command);

                  start = false;

              } else {

                  lastCommand = command;

              }

          } else {

              calculate(Double.parseDouble(display.getText()));

              lastCommand = command;

              start = true;

          }

      }

  }

  public void calculate(double x) {

      char operator = lastCommand.charAt(0);

      switch (operator) {

          case '+':

              result += x;

              break;

          case '-':

              result -= x;

              break;

          case '*':

              result *= x;

              break;

          case '/':

              result /= x;

              break;

          case '=':

              result = x;

              break;

      }

      display.setText("" + result);

  }

}

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. sql查询结果多对多转为一对多返回前端

    企业表 ent_EnterpriseArchives  有id,企业名称 entName veh_Vehicle 车辆表,有所属企业id  companyId,车辆id,车牌号licPlate 目的是 ...

  2. SolidWorks二次开发的研究

    三维机械设计软件SolidWorks是一套基于Windows的CAD/CAE/CAM/PDM桌面集成系统,是由美国SolidWorks公司在总结和继承大型机械CAD软件的基础上,在Windows环境下 ...

  3. ASP.NET中引用dll“找不到指定模块"的完美解决办法 z

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息.DllImport属性应用于方法,要求最少要 ...

  4. harbor使用aws s3存储

    参考:http://www.vmtocloud.com/how-to-configure-harbor-registry-to-use-amazon-s3-storage/ s3 bucket权限:更 ...

  5. 2.GlusterFS 安装配置

    2.1 GlusterFS 安装前的准备 服务器规划:(vmware 实验) 操作系统 IP 主机名 数据盘(2 块) CentOS 6.8 x86_64 10.1.0.151 mystorage1 ...

  6. CentOS 7.3 下 Mysql(mariadb)的安装

    LNMP的安装中 Nginx的安装很简单,我一般去Nginx官方网站上下载对应版本的rpm包后,上传到终端rpm安装.再此不多赘述. 但是在CentOS7中安装最新的mysql(mariadb)却经常 ...

  7. June 17th 2017 Week 24th Saturday

    Absence sharpens love, presence strengthens it. 相聚爱益切,离别情更深. There is almost no such love that can i ...

  8. OpenGL中的数据——Buffer

    OpenGL中主要包括了两种数据——Buffer和Texture. Buffer用于储存线性数无类型据块,可以看成普通的内存块,而Texture则用于储存多维数据,一般储存图像或者其他数据. Buff ...

  9. 在同一网关下ping不通其他电脑

    最近在做数据库相关的程序,所以想先用ping试下和主机连没连着,但发现虽然两台电脑在同一网关下,但却互相ping不通,后来才发现,原来是防火墙在作怪. Omg!感慨一下!默认的情况下,保守的windo ...

  10. 如何将BSP应用配置成Fiori Launchpad上的一个tile

    当我们通过WebIDE或者Eclipse的插件Team Provider把一个本地开发好的UI5应用部署到了ABAP Netweaver服务器上之后,我们可以将该UI5应用配置成Fiori launc ...