//package Test8;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

class MousePolice implements MouseListener {
	JTextField textField;
	String string = "";

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		String temString = "";
		String[] st;

		JButton button = (JButton) e.getSource();
		String str = button.getText();
		if (!str.equals("Back") && !str.equals("CE") && !str.equals("C")
				&& !str.equals("About")) {
			temString = str;
		} else if (str.equals("About")) {
			JOptionPane.showMessageDialog(null, "此计算器仅能计算简单的二元和一元运算,\n"
					+ "请用户在使用的时候注意!\n" + "提示:Back回退一位,CE或者C清除所有数据!");
		}
		if (str.equals("+") && string.contains("+")) {
			temString = temString.substring(0, temString.length() - 1);
		} else if (str.equals("-") && string.contains("-")) {
			temString = temString.substring(0, temString.length() - 1);
		} else if (str.equals("*") && string.contains("*")) {
			temString = temString.substring(0, temString.length() - 1);
		} else if (str.equals("/") && string.contains("/")) {
			temString = temString.substring(0, temString.length() - 1);
		}
		string += temString;

		if (str.equals("CE") || str.equals("C")) {
			string = "";
		} else if (str.equals("Back") && string.length() > 0) {
			string = string.substring(0, string.length() - 1);
		} else if (str.equals("1/x")) {
			string = 1
					/ (Double.parseDouble(string.substring(0,
							string.length() - 3))) + "";
		} else if (str.equals("sqrt")) {
			string = Math.sqrt(Double.parseDouble(string.substring(0,
					string.length() - 4)))
					+ "";
		} else if (str.equals("%")) {
			string = 0.01
					* (Double.parseDouble(string.substring(0,
							string.length() - 1))) + "";
		} else if (str.equals("+/-")) {
			string = (-1)
					* (Double.parseDouble(string.substring(0,
							string.length() - 3))) + "";
		} else if (str.equals("=")) {
			{
				boolean flag = true;
				string = string.substring(0, string.length() - 1);
				try {
					st = string.split("[+]");
					string = (Double.parseDouble(st[0]) + Double
							.parseDouble(st[1])) + "";
					flag = false;
				} catch (Exception e1) {
					try {
						if (flag) {
							st = string.split("[-]");
							string = (Double.parseDouble(st[0]) - Double
									.parseDouble(st[1])) + "";
						}

					} catch (Exception e2) {
						try {
							if (flag) {
								st = string.split("[*]");
								string = (Double.parseDouble(st[0]) * Double
										.parseDouble(st[1])) + "";
							}

						} catch (Exception e3) {
							try {
								if (flag) {
									st = string.split("[/]");
									string = (Double.parseDouble(st[0]) / Double
											.parseDouble(st[1])) + "";
								}

							} catch (Exception e4) {
								try {
									Double.parseDouble(string);
								} catch (Exception e5) {
									string = "数学语法错误!";
								}
							}
						}
					}
				}
			}
		}
		textField.setText(string);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}

@SuppressWarnings("serial")
public class Calculator extends JFrame {
	public Calculator() {
		init();
		FlowLayout flow = new FlowLayout();
		flow.setAlignment(FlowLayout.LEFT);
		flow.setHgap(10);
		setLayout(flow);
		setBounds(525, 250, 300, 220);
		setResizable(false);
		setVisible(true);
		setTitle("简易计算器——海涛开发");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	void init() {
		JPanel panel1, panel2, panel3, panel4;
		JTextField field;
		JButton[] buttons1, buttons2, buttons3;
		MousePolice police;

		panel1 = new JPanel();
		police = new MousePolice();
		panel1.setLayout(new GridLayout(1, 1));
		field = new JTextField(24);
		police.setTextField(field);
		field.setEditable(false);
		panel1.add(field);
		add(panel1);

		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(1, 4));
		buttons1 = new JButton[4];
		for (int i = 0; i < buttons1.length; i++) {
			buttons1[i] = new JButton();
			buttons1[i].setForeground(Color.BLUE);
			buttons1[i].addMouseListener(police);
		}
		buttons1[0].setText("Back");
		buttons1[1].setText("CE");
		buttons1[2].setText("C");
		buttons1[3].setText("About");
		for (JButton button : buttons1) {
			panel2.add(button);
		}
		add(panel2);

		panel3 = new JPanel();
		panel3.setLayout(new GridLayout(4, 3));
		buttons2 = new JButton[12];
		for (int i = 0; i < buttons2.length; i++) {
			buttons2[i] = new JButton();
			buttons2[i].setText("" + (9 - i));
			if (i == 10) {
				buttons2[i].setText("+/-");
			} else if (i == 11) {
				buttons2[i].setText(".");
			}
			panel3.add(buttons2[i]);
			buttons2[i].addMouseListener(police);
		}
		add(panel3);

		panel4 = new JPanel();
		panel4.setLayout(new GridLayout(4, 2));
		buttons3 = new JButton[8];
		for (int i = 0; i < buttons3.length; i++) {
			buttons3[i] = new JButton();
			if (i % 2 == 0 || i == buttons3.length - 1) {
				buttons3[i].setForeground(Color.RED);
			}
			buttons3[i].addMouseListener(police);
		}
		buttons3[0].setText("/");
		buttons3[1].setText("sqrt");
		buttons3[2].setText("*");
		buttons3[3].setText("%");
		buttons3[4].setText("-");
		buttons3[5].setText("1/x");
		buttons3[6].setText("+");
		buttons3[7].setText("=");
		for (JButton button : buttons3) {
			panel4.add(button);
		}
		add(panel4);

	}

	public static void main(String[] args) {
		new Calculator();
	}
}