package com.calculator.one;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Calculator implements ActionListener {
/**
*
*/
JFrame f = new JFrame();
JTextField tf = new JTextField("0.");
JMenuBar bar = new JMenuBar();
JMenu menuEdit = new JMenu("编辑(E)");
JMenu menuView = new JMenu("查看(V)");
JMenu menuHelp = new JMenu("帮助(H)");
JMenuItem menuEditCopy = new JMenuItem("复制(C)");
JMenuItem menuEditPaste = new JMenuItem("粘贴(V)");
JMenuItem menuHelpAbout = new JMenuItem("关于计算器(A)");
JPanel panel0 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JButton btBk = new JButton(" Backspace");
JButton btCe = new JButton("Ce");
JButton btCl = new JButton("C");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton btDiv = new JButton("/");
JButton btSqrt = new JButton("sqrt");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton btMult = new JButton("*");
JButton btMol = new JButton(" % ");
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton btMinu = new JButton("-");
JButton btCoun = new JButton(" 1/x");
JButton bt0 = new JButton("0");
JButton btPoint = new JButton(".");
JButton btEqual = new JButton("=");
JButton btAdd = new JButton("+");
JButton btSign = new JButton(" +/-");
/**
* @param args
*/
public void init() {
f.add(panel0);
f.add(tf, "North");
f.setJMenuBar(bar);
bar.add(menuEdit);
bar.add(menuView);
bar.add(menuHelp);
menuEdit.add(menuEditCopy);
menuEdit.add(menuEditPaste);
menuHelp.add(menuHelpAbout);
tf.setEditable(false);// 不可编辑
tf.setHorizontalAlignment(JTextField.RIGHT);// 从右侧输入数据
panel0.add(panel2);
panel0.add(panel);
panel0.add(panel3);
panel0.add(panel4);
panel0.add(panel5);
panel2.setLayout(new GridLayout(1, 4));
panel2.add(btBk);
panel2.add(btCe);
panel2.add(btCl);
panel.setLayout(new GridLayout(1, 5));
panel.add(bt7);
panel.add(bt8);
panel.add(bt9);
panel.add(btDiv);
panel.add(btSqrt);// 根号
panel3.setLayout(new GridLayout(1, 5));
panel3.add(bt4);
panel3.add(bt5);
panel3.add(bt6);
panel3.add(btMult);
panel3.add(btMol);// 百分号
panel4.setLayout(new GridLayout(1, 5));
panel4.add(bt1);
panel4.add(bt2);
panel4.add(bt3);
panel4.add(btMinu);
panel4.add(btCoun);// 倒数
panel5.setLayout(new GridLayout(1, 5));
panel5.add(bt0);
panel5.add(btPoint);
panel5.add(btAdd);
panel5.add(btEqual);
panel5.add(btSign);// 正负符号
btBk.setForeground(new Color(255, 0, 0));
btCe.setForeground(new Color(255, 0, 0));
btCl.setForeground(new Color(255, 0, 0));
btAdd.setForeground(new Color(255, 0, 0));
btMult.setForeground(new Color(255, 0, 0));
btDiv.setForeground(new Color(255, 0, 0));
btMinu.setForeground(new Color(255, 0, 0));
btEqual.setForeground(new Color(255, 0, 0));
bt0.setForeground(new Color(0, 0, 255));
bt1.setForeground(new Color(0, 0, 255));
bt2.setForeground(new Color(0, 0, 255));
bt3.setForeground(new Color(0, 0, 255));
bt4.setForeground(new Color(0, 0, 255));
bt5.setForeground(new Color(0, 0, 255));
bt6.setForeground(new Color(0, 0, 255));
bt7.setForeground(new Color(0, 0, 255));
bt8.setForeground(new Color(0, 0, 255));
bt9.setForeground(new Color(0, 0, 255));
btSqrt.setForeground(new Color(0, 0, 255));
btMol.setForeground(new Color(0, 0, 255));
btCoun.setForeground(new Color(0, 0, 255));
btSign.setForeground(new Color(0, 0, 255));
// 注册监听
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
btAdd.addActionListener(this);
btMinu.addActionListener(this);
btMult.addActionListener(this);
btDiv.addActionListener(this);
btEqual.addActionListener(this);
btPoint.addActionListener(this);
btSqrt.addActionListener(this);
btMol.addActionListener(this);
btCoun.addActionListener(this);
btSign.addActionListener(this);
btBk.addActionListener(this);
btCe.addActionListener(this);
btCl.addActionListener(this);
menuEditCopy.addActionListener(this);
menuEditPaste.addActionListener(this);
menuHelpAbout.addActionListener(this);
f.addWindowListener(new MyWinLis());
f.setTitle("计算器");
f.setSize(295, 240);
f.setResizable(false);// 不能改变窗体大小
f.setVisible(true);
}
String foreNum = null;
String currOpr = null;
String currNum = null;
boolean i = true;
String copy;// 复制
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
Object obj = e.getSource();
// 输入数字
if (obj == bt1 || obj == bt2 || obj == bt3 || obj == bt4 || obj == bt5
|| obj == bt6 || obj == bt7 || obj == bt8 || obj == bt9
|| obj == bt0) {
if (i == true)
tf.setText(e.getActionCommand());
else
tf.setText(tf.getText() + e.getActionCommand());
i = false;
}
// 小数点
else if (obj == btPoint) {
if (i == false)
tf.setText(tf.getText() + '.');
}
// 实现开根号
else if (obj == btSqrt) {
if (tf.getText().charAt(0) == '-') {
tf.setText("函数输入无效");
} else
tf.setText(Double.toString(java.lang.Math.sqrt(Double
.parseDouble(tf.getText()))));
i = true;
}
// 实现倒数
else if (obj == btCoun) {
if (tf.getText().charAt(0) == '0') {
tf.setText("零不能求倒数");
i = true;
} else {
String s = Double
.toString(1 / Double.parseDouble(tf.getText()));
foreNum = tf.getText();
tf.setText(s);
i = true;
}
}
// 实现+/-符号
else if (obj == btSign) {
boolean isNumber = true;
String s = tf.getText();
for (int m = 0; m < s.length(); m++)
if (!(s.charAt(m) >= '0' && s.charAt(m) <= '9'
|| s.charAt(m) == '.' || s.charAt(m) == '-')) {
break;
}
if (isNumber == true) {
// 如果当前字符串首字母有-号,代表现在是个负数,再按下时,则将首符号去掉
if (s.charAt(0) == '-') {
tf.setText("");
for (int m = 1; m < s.length(); m++) {
char a = s.charAt(m);
tf.setText(tf.getText() + a);
}
}
// 如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
else
tf.setText('-' + s);
}
}
// 实现%功能
else if (obj == btMol) {
boolean isNumber = true;
if (isNumber == true)
tf.setText(String
.valueOf(Double.parseDouble(tf.getText()) / 100));
else
tf.setText("错误");
}
// 实现符号运算
else if (obj == btAdd || obj == btMinu || obj == btDiv || obj == btMult) {
if (i == false) {
currNum = tf.getText();
if (currOpr == "+")
tf.setText(String.valueOf(Double.parseDouble(foreNum)
+ Double.parseDouble(currNum)));
else if (currOpr == "-")
tf.setText(String.valueOf(Double.parseDouble(f