Calc计算器

一段简单的Java计算器代码 

2009-12-29 20:52:47|  分类: 学海泛舟 |  标签: |字号大中小订阅

今天在一个QQ群上看到一位朋友发了一个编译未通过的Java计算器源代码,遂收藏下来并将其更正。Mark之,以供日后参考。

程序比较简单,可以说并不是很好的设计,但对于Java中swing及awt的使用,可以作为一个简单有效的例子。运行起来如下图所示:

一段简单的Java计算器代码 - 小叶 - 一叶扁舟

下面,将此Java程序分享一下(详细描述见注释):

/*

* @(#)JCalculator.java 1.00 12/29/2009

*

* Email: light_warm@163.com

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* A simple calculator program.

* <p>I saw this program in a QQ group, and help a friend correct it.</p>

*

* @author Singyuen Yip

* @version 1.00 12/29/2009

* @see JFrame

* @see ActionListener

*/

publicclass JCalculator extends JFrame implements ActionListener {

/**

* Serial Version UID

*/

privatestaticfinallongserialVersionUID = -169068472193786457L;

/**

* This class help close the Window.

* @author Singyuen Yip

*

*/

privateclass WindowCloser extends WindowAdapter {

publicvoid windowClosing(WindowEvent we) {

System.exit(0);

}

}

inti;

// Strings for Digit & Operator buttons.

privatefinal String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1",

"2", "3", "-", ".", "0", "=", "+" };

// Build buttons.

JButton[] buttons = new JButton[str.length];

// For cancel or reset.

JButton reset = new JButton("CE");

// Build the text field to show the result.

JTextField display = new JTextField("0");

/**

* Constructor without parameters.

*/

public JCalculator() {

super("Calculator");

// Add a panel.

JPanel panel1 = new JPanel(new GridLayout(4, 4));

// panel1.setLayout(new GridLayout(4,4));

for (i = 0; i < str.length; i++) {

buttons[i] = new JButton(str[i]);

panel1.add(buttons[i]);

}

JPanel panel2 = new JPanel(new BorderLayout());

// panel2.setLayout(new BorderLayout());

panel2.add("Center", display);

panel2.add("East", reset);

// JPanel panel3 = new Panel();

getContentPane().setLayout(new BorderLayout());

getContentPane().add("North", panel2);

getContentPane().add("Center", panel1);

// Add action listener for each digit & operator button.

for (i = 0; i < str.length; i++)

buttons[i].addActionListener(this);

// Add listener for "reset" button.

reset.addActionListener(this);

// Add listener for "display" button.

display.addActionListener(this);

// The "close" button "X".

addWindowListener(new WindowCloser());

// Initialize the window size.

setSize(800, 800);

// Show the window.

// show(); Using show() while JDK version is below 1.5.

setVisible(true);

// Fit the certain size.

pack();

}  

publicvoid actionPerformed(ActionEvent e) {

Object target = e.getSource();

String label = e.getActionCommand();

if (target == reset)

handleReset();

elseif ("0123456789.".indexOf(label) > 0)

handleNumber(label);

else

handleOperator(label);

}

// Is the first digit pressed?

booleanisFirstDigit = true;

/**

* Number handling.

* @param key the key of the button.

*/

publicvoid handleNumber(String key) {

if (isFirstDigit)

display.setText(key);

elseif ((key.equals(".")) && (display.getText().indexOf(".") < 0))

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

elseif (!key.equals("."))

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

isFirstDigit = false;

}

/**

* Reset the calculator.

*/

publicvoid handleReset() {

display.setText("0");

isFirstDigit = true;

operator = "=";

}

doublenumber = 0.0;

String operator = "=";

/**

* Handling the operation.

* @param key pressed operator's key.

*/

publicvoid handleOperator(String key) {

if (operator.equals("+"))

number += Double.valueOf(display.getText());

elseif (operator.equals("-"))

number -= Double.valueOf(display.getText());

elseif (operator.equals("*"))

number *= Double.valueOf(display.getText());

elseif (operator.equals("/"))

number /= Double.valueOf(display.getText());

elseif (operator.equals("="))

number = Double.valueOf(display.getText());

display.setText(String.valueOf(number));

operator = key;

isFirstDigit = true;

}

publicstaticvoid main(String[] args) {

new JCalculator();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值