import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class calculator {
private JFrame f =new JFrame("Calculator");
private JPanel inputPanel =new JPanel();
private JPanel buttonsPanel=new JPanel();
private JPanel operationPanel =new JPanel();
private JTextField input=new JTextField(20);
private JTextField show=new JTextField(20);
private JButton backspace=new JButton("退格");
private JButton CE=new JButton("CE");
private JButton C=new JButton("C");
private JButton sqrt=new JButton("sqrt");
private JButton fang=new JButton("^");
private JButton mod=new JButton("%");
private JButton dao=new JButton("1/x");
private JButton sin=new JButton("sin");
private JButton cos=new JButton("cos");
private JButton lg=new JButton("ln");
private JButton add=new JButton("+");
private JButton sub=new JButton("-");
private JButton mul=new JButton("*");
private JButton div=new JButton("/");
private JButton decimal=new JButton (".");
private JButton equal=new JButton ("=");
private JButton zero=new JButton ("0");
private JButton one=new JButton ("1");
private JButton two=new JButton ("2");
private JButton three=new JButton ("3");
private JButton four=new JButton ("4");
private JButton five=new JButton ("5");
private JButton six=new JButton ("6");
private JButton seven=new JButton ("7");
private JButton eight=new JButton ("8");
private JButton nine=new JButton ("9");
private String numl="";
private String operator="";
public static void main(String[] args){
new calculator();
}
public calculator(){
show.setText("0");
show.setEditable(false);
show.setHorizontalAlignment(JTextField.RIGHT);
input.setText("0");
input.setEditable(false);
input.setHorizontalAlignment(JTextField.RIGHT);
inputPanel.setLayout(new GridLayout(2,1));
inputPanel.add(show);
inputPanel.add(input);
f.add(inputPanel,BorderLayout.NORTH);
operationPanel.add(sin);
sin.setBackground(new Color(100,255,10));
operationPanel.add(cos);
cos.setBackground(new Color(100,255,10));
operationPanel.add(CE);
CE.setBackground(new Color(100,255,10));
operationPanel.add(C);
C.setBackground(new Color(100,255,10));
operationPanel.add(backspace);
backspace.setBackground(new Color(100,255,10));
f.add(operationPanel,BorderLayout.CENTER);
buttonsPanel.setLayout(new GridLayout(4,5));
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(div);
buttonsPanel.add(mod);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(mul);
buttonsPanel.add(fang);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(sub);
buttonsPanel.add(sqrt);
buttonsPanel.add(zero);
buttonsPanel.add(decimal);
buttonsPanel.add(equal);
buttonsPanel.add(add);
buttonsPanel.add(dao);
f.add(buttonsPanel, BorderLayout.SOUTH);
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
add.addMouseListener(new CaleMouseListener());
sub.addMouseListener(new CaleMouseListener());
mul.addMouseListener(new CaleMouseListener());
div.addMouseListener(new CaleMouseListener());
sqrt.addMouseListener(new CaleMouseListener());
fang.addMouseListener(new CaleMouseListener());
mod.addMouseListener(new CaleMouseListener());
dao.addMouseListener(new CaleMouseListener());
sin.addMouseListener(new CaleMouseListener());
cos.addMouseListener(new CaleMouseListener());
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}