java实现简单计算器功能

童鞋们,是不是有使用计算器的时候,还要进入运行,输入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);

  }

}
时间: 2024-07-31 04:06:04

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

Shell 实现简单计算器功能

Shell 实现简单计算器功能,脚本如下: [[email protected] scripts]# cat jisuan.sh #!/bin/bash print_usage(){     printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"     exit 1 } #判断传入的参数是不是3个 if [ $# -ne 3 ]   then     print_usage fi firstnum=$1 secondnum=$3 op=$2 #对传入的参数进

[Java.web]简单计算器

项目的  WebRoot 目录下的 calculator.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>计算结果</title> </head> <body> <jsp:us

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

计算器: 1 import java.awt.BorderLayout; 2 import java.awt.Container; 3 import java.awt.Font; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.JButton; 9 import javax.swing.

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

代码: 1 #!/usr/local/env python3 2 ''' 3 Author:@南非波波 4 Blog:http://www.cnblogs.com/songqingbo/ 5 E-mail:[email protected] 6 ''' 7 8 import time,sys 9 import module 10 11 if __name__ == "__main__": 12 while True: 13 count = 0 14 if count < 3: 1

Android-Kotlin简单计算器功能

上一篇博客 Android-Kotlin-配置/入门 配置好了 AndroidStudio Kotlin 的环境: 选择包名,然后右键: 选择Class类型,会有class: 创建CounterClass: package cn.kotlin.kotlin_oop02 /** * 加减乘除计算的calss * var number1:Double 此Double是kotlin的 * var operapor:Char 此Char是kotlin的 */ class CounterClass(var

shell脚本结合函数实现简单计算器功能

#!/bin/bashfunction tool_menu(){    echo "****************************************************"    echo "*                                                  *"    echo "*              calculation  tools                  *"   

Java对象简单实用(计算器案例)

Java对象简单实用(计算器案例) 对 Java中的对象与属性,方法的使用,简单写了个案例 1 import java.util.Scanner; 2 class Calculste 3 { 4 int a; //定义两个整数 5 int b; 6 String option; //定义接收操作符的字符串 7 public void count(){ 8 9 //对操作符进行判断 10 switch(option){ 11 case "+": 12 System.out.println

JAVA编写的简单计算器

package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; impo

简单计算器 java实现hdu1237

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15190    Accepted Submission(s): 5184 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,