`
kyke
  • 浏览: 53036 次
  • 来自: 广州
社区版块
存档分类
最新评论

A Simple Calculator

    博客分类:
  • Java
阅读更多
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

/*
*A frame with calculator panel
*/
class CalculatorFrame extends JFrame{
public CalculatorFrame(){
setTitle("Calculator");
//setSize(100, 100);
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}

/*
*A panel with calulator buttons and a result display
*/
class CalculatorPanel extends JPanel{
public CalculatorPanel(){
setLayout(new BorderLayout());

result = 0;
lastCommand = "=";
start = true;

//add the display
display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

//add the buttons in a 4 * 4 grid

panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));

addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);

addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);

addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);

addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);

add(panel, BorderLayout.CENTER);
}

/*
*Adds a button to the center panel.
*@param label the button label
*@param listener the button listener
*/
public void addButton(String label, ActionListener listener){
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}

/*
*Carries out the pending calculation.
*@param x the value to be accumulated with the prior result.
*/
public void calculate(double x){
if(lastCommand.equals("+")) result += x;
else if(lastCommand.equals("-")) result -= x;
else if(lastCommand.equals("*")) result *= x;
else if(lastCommand.equals("/")) result /= x;
else if(lastCommand.equals("=")) result = x;
display.setText("" + result);
}

/*
*This action inserts the button action string to the end of the display text.
*/
private class InsertAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String input = event.getActionCommand();
if(start){
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}

/*
*This action executes the command that the button action string denotes.
*/
private class CommandAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String command = event.getActionCommand();
if(start){
if(command.equals("-")){
display.setText(command);
start = false;
}
else lastCommand = command;
}
else{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}

private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
分享到:
评论

相关推荐

    A simple calculator program.

    【标题】"A Simple Calculator Program" 是一个基础的计算器应用程序,旨在实现基本的数学运算功能。这个程序可能是用某种编程语言(如Visual Basic或Delphi)编写,通过图形用户界面(GUI)提供用户友好的计算体验...

    微信小程度简易计算器(A simple calculator)

    A simple calculator,一个简易计算器 纯HTML、CSS、JS编写 该计算器布局使用CSS3 FlexBox布局 移动APP使用HBuild构建 在APP上,当输入手机号码后长按等于号可以拨打手机号码 左滑右滑可以切换单手模式 内置两套主题...

    simple calculator

    在这个项目中,"simple calculator"利用了这两个强大的开源库来创建一个直观且易于使用的计算器界面。 首先,让我们深入了解GTK+。GTK+是由GNOME项目开发的,最初用于GNU Image Manipulation Program (GIMP),后来...

    jisuanqi.rar_calculator

    该计算器的描述"A simple calculator which can calculate simply"表明,它的功能相对基础,主要用于进行基本的数学运算,例如加、减、乘、除。这样的计算器通常包含以下核心组件: 1. **用户界面(UI)**:计算器...

    本科毕业设计--基于单片机控制的简单计算器设计与仿真.doc

    This paper presents the design and simulation of a simple calculator based on single-chip microcomputer control. The system can perform basic arithmetic operations and LED display. The design uses 80S...

    VB编程资源大全(英文源码 函数)

    A detailed Word 6.0 "ReadMe" file is included.<END><br>2,calc2.zip A very Simple Calculator, i made this to learn VB. Nothing special, though worth giving a look.<END><br>3,ComplexNumber.zip ...

    CheckStyle结果分析

    * This class represents a simple calculator. It provides basic arithmetic operations. */ public class Calculator { // ... } ``` --- ##### 26. 冗余的 throws: 'NameNotFoundException' 是 '...

    简单计算器源码[C++]

    *This is a simple calculator program resolves a expression * *input and prints the value of the expression. The code shows * *how a stack ADT works in a calculator. To simplify the probl-* *em, we...

    prolog manual.pdf

    8.1 Action specification for a simple calculator 8.2 Animating the 8-puzzle (§5.2) using character graphics 8.3 Animating the blocks mover (§2.19) using character graphics 8.4 Java Tic-Tac-Toe ...

    CS193P IOS APPLICATION DEVELOPMENT Assignment 1 Walkthrough.pdf

    The main objective of this assignment is to reproduce the demonstration given in class, which involves building a simple calculator application for the iPhone. This assignment aims to help students ...

    A-simple-Calculator-master_html_javascript_calculator_css_

    标题 "A-simple-Calculator-master" 暗示我们正在探讨一个简单的计算器项目,它基于HTML、JavaScript和CSS技术。这个项目可能是一个基本的前端实现,允许用户进行基本的数学运算。接下来,我们将深入讨论这三个核心...

    大学毕业论文-—基于mcs51单片机的简易计算器的设计与实现.doc

    本文 Designs and implements a simple calculator based on MCS-51 single-chip microcomputer. The calculator uses MCS-51 single-chip microcomputer as the core, and uses MM74C922 chip to realize the key ...

    Calculadora_Final_line9yb_Calcukator_

    【描述】"A simple calculator programmed with QT" 表明这个计算器软件设计得简洁易用,主要功能可能包括基本的数学运算,如加减乘除,可能还包含一些进阶功能,如求平方根、百分比计算等。QT框架的使用意味着其...

    Java编程规范+文档注释+程序调试

    public class SimpleCalculator { /** * Adds two numbers together. * @param num1 The first number. * @param num2 The second number. * @return The sum of the two numbers. */ public int add(int num...

    完美Javadoc全接触

    public class SimpleCalculator { // ... } ``` - **显示效果**: - 作者:Androidyue - 自 1.0 版本开始 - 版本:1.1, 01/01/10 2. **方法注释** - **代码示例**: ```java /** * Adds two numbers ...

    Visual Studio的简单控制应用.docx

    Console.WriteLine("A simple calculator application.\r\n"); Console.WriteLine("Please enter first integer value: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Please enter ...

    VHDL.rar_The Test

    It s a simple calculator of addition and multiplication using a simple stack, with multiple test benches. The files test-button and debounce are for the use on a board for the correct functionality of...

    A_Simple_Calculator_Program.zip_visual studio_计算器

    标题"A_Simple_Calculator_Program.zip_visual studio_计算器"表明这是一个使用Visual Studio开发的简易计算器程序。"Visual Studio"是微软出品的一款强大的集成开发环境(IDE),广泛用于编写各种类型的软件,包括...

    SimpleCalculator

    "SimpleCalculator"是一个基于Java开发的简易计算器项目。这个项目的目的是提供一个基本的计算功能,让用户能够进行简单的数学运算,如加、减、乘、除。在这个项目中,我们可以学习到如何使用Java语言来实现一个...

    Calc 小计算器

    * A simple calculator program. * <p>I saw this program in a QQ group, and help a friend correct it. * * @author Singyuen Yip * @version 1.00 12/29/2009 * @see JFrame * @see ...

Global site tag (gtag.js) - Google Analytics