发现网上现存的JAVA计算器的代码基本上都是不完美的,差不多都有一定的错误所以把我写的这个发出来,供有兴趣的朋友参考。我的这个实现了WINDOWS自带的计算器的普通型的所有功能,不过我的数字处理全都是用的DOUBLE型的。由于科学型中的 很多按钮的功能我不知道,所以没写。
我已经做了可执行文件,在CSDN的资源里。
/**
@(#)Calculator.java
@author Renull
@version 1.00 2007/11/27
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.lang.Math.*;
public class Calculator implements ActionListener {
private JFrame frame;
private JPanel panel1,panel2,panel3;
private JTextField tfr,tfm;
private JLabel label;
private JButton[] bt;
private static String number,toMemory,temp;
private static Double result,num;
private short sign,presign;
public Calculator() {
frame = new JFrame("Normal Calculator");
frame.setLayout(new BorderLayout(5,8));
panel1 = new JPanel();
panel1.setLayout(new BorderLayout(10,15));
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,5,5,5));
panel3 = new JPanel();
panel3.setLayout(new GridLayout(1,1,5,5));
Font font = new Font("黑体",Font.BOLD,16);
tfr = new JTextField("0.",20);
tfr.setHorizontalAlignment(JTextField.RIGHT);
tfr.setFont(font);
tfr.setEditable(false);
tfm = new JTextField();
tfm.setHorizontalAlignment(JTextField.CENTER);
tfm.setEditable(false);
tfm.setFont(font);
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(font);
String[] btname = {"Mc","7","8","9","/","sqr","Mr","4","5","6","*","%","Ms","1","2","3","-","1/x","M+","0","+/-",".","+","=","←","CE","C"};
bt = new JButton[btname.length];
for(int i = 0;i < bt.length;i++) {
bt[i] = new JButton(btname[i]);
bt[i].setForeground(Color.red);
bt[i].setFont(font);
bt[i].setActionCommand(btname[i]);
bt[i].addActionListener(this);
}
for(int i = 1;(i - 1) * 3 < bt.length - 3;i += 2)
for(int j = 1;j<=3;j++)
bt[(i - 1) * 3 + j].setForeground(Color.BLUE);
for(int i = 5;i <= 17;i += 6)
bt[i].setForeground(Color.BLUE);
number = "";
toMemory = "";
sign = presign = -1;
result = 0.0;
num = -0.0;
panel3.add(label);
panel3.add(tfm);
for(int i = bt.length - 3;i < bt.length;i++)
panel3.add(bt[i]);
panel1.add(tfr,BorderLayout.NORTH);
panel1.add(panel3,BorderLayout.CENTER);
for(int i = 0;i < bt.length - 3;i++)
panel2.add(bt[i]);
frame.add(panel1,BorderLayout.NORTH);
frame.add(panel2,BorderLayout.CENTER);
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocation(100,50);
}
public void actionPerformed(ActionEvent e) {
if("1234567890.".contains(e.getActionCommand())) {
if("123456789".contains(e.getActionCommand()))
number += e.getActionCommand();
if(e.getActionCommand().equals("0"))
if(number == "" || number.charAt(0) != '0') number += "0";
if(e.getActionCommand().equals("."))
if(!(number.contains("."))) {
if(number == "") number += "0.";
else number += ".";
}
tfr.setText(number);
}
else {
temp = e.getActionCommand();
if("+/-*sqr%1/x".contains(temp)) {
if("+-*/".contains(temp))
workOut_double();
else
workOut_single();
}
else if(temp.equals("="))
workOut_equal();
else if("McMrMsM+".contains(temp))
memory();
else
reset();
}
}
public void workOut_single() {
num = Double.parseDouble(number);
if(temp.equals("+/-")) {
if(number != "" && Double.parseDouble(number) > 0.0)
tfr.setText("-" + number);
if(number != "" && Double.parseDouble(number) < 0.0)
tfr.setText(number.substring(1,number.length()));
num = Double.parseDouble(number);
}
if(temp.equals("sqr")) {
if(num >= 0) {
num = sqrt(num);
tfr.setText(Double.toString(num));
}
else
error();
}
if(temp.equals("%")) {
num /=100;
tfr.setText(Double.toString(num));
}
if(temp.equals("1/x")) {
if(num != 0.0) {
num = 1/num;
tfr.setText(Double.toString(num));
}
else
error();
}
number = tfr.getText();
}
public void workOut_double() {
num = Double.parseDouble(number);
switch(presign) {
case 0:
result += num;
break;
case 1:
result -= num;
break;
case 2:
result *= num;
break;
case 3:
if(num != 0.0) result /= num;
else error();
break;
default:
result = Double.parseDouble(tfr.getText());
}
if(temp == "+") sign = presign = 0;
if(temp == "-") sign = presign = 1;
if(temp == "*") sign = presign = 2;
if(temp == "/") sign = presign = 3;
if(!(tfr.getText().equals("error"))) tfr.setText(result.toString());
number = "";
}
public void error(){
temp = "C";
reset();
tfr.setText("error");
}
public void workOut_equal() {
if(presign == -1) ;
if(presign == 4) {
if(sign == 0) result += num;
if(sign == 1) result -= num;
if(sign == 2) result *= num;
if(sign == 3) {
if(num != 0.0)
result /= num;
else
error();
}
if(!(tfr.getText().equals("error")))
tfr.setText(result.toString());
}
if("0123".contains(Short.toString(presign)) && number != ""){
workOut_double();
presign = 4;
}
}
public void memory() {
if(temp.equals("Mc")) {
tfm.setText("");
label.setText("");
toMemory = "";
}
if(temp.equals("Mr")) {
if(!(toMemory.equals(""))) {
tfr.setText(toMemory);
number = toMemory;
}
else
tfr.setText("0.");
}
if(temp.equals("Ms")) {
toMemory = tfr.getText();
tfm.setText("M");
label.setText("memory");
}
if(temp.equals("M+")) {
if(toMemory != "") {
Double k;
k = Double.parseDouble(toMemory) + Double.parseDouble(tfr.getText());
toMemory = k.toString();
tfm.setText("M");
}
else {
toMemory = tfr.getText();
tfm.setText("M");
}
label.setText("memory");
}
}
public void reset(){
if(temp.equals("C")) {
tfr.setText("0.");
number = "";
result = 0.0;
sign = presign = -1;
}
if(temp.equals("CE")) {
tfr.setText("0.");
number = "";
}
if(temp.equals("←")) {
number = tfr.getText();
if(number.length() > 1 && Double.parseDouble(number) != 0.0) {
if(number.charAt(number.length()-1) == '0' && number.charAt(number.length() - 2) == '.') {
number = number.substring(0,number.length()-2);
tfr.setText(number);
}
number = number.substring(0,number.length()-1);
tfr.setText(number);
}
else {
tfr.setText("0.");
number = "";
}
}
}
public static void main(String[] args){
new Calculator();
}
}