import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import javax.swing.*;
public class panelCommon extends JPanel implements ActionListener {
JButton button1, button2, button3, button4, button5, button6, button7,
button8, button9, button0, buttonJia, buttonJian, buttonCheng,
buttonChu, buttonDeng, buttonDian, buttonC, buttonDel, buttonZF,
buttonGH;
JTextField jtf;
CalWin calwin;
char oper;
String key, allKey = "1234567890.±del", jjccKey = "+-*/";
BigDecimal d1, d2;
boolean first;
public panelCommon(CalWin cw) {
calwin = cw;
jtf = new JTextField(18);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setEditable(false);
jtf.setBackground(new Color(255, 255, 255));
jtf.setText("0");
this.add(jtf);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
buttonJia = new JButton("+");
buttonJian = new JButton("-");
buttonCheng = new JButton("*");
buttonChu = new JButton("/");
buttonDeng = new JButton("=");
buttonDian = new JButton(".");
buttonDel = new JButton("del");
buttonC = new JButton("c");
buttonZF = new JButton("±");
buttonGH = new JButton("√");
this.add(buttonDel);
this.add(buttonC);
this.add(buttonZF);
this.add(buttonGH);
this.add(button1);
this.add(button2);
this.add(button3);
this.add(buttonJia);
this.add(button4);
this.add(button5);
this.add(button6);
this.add(buttonJian);
this.add(button7);
this.add(button8);
this.add(button9);
this.add(buttonCheng);
this.add(button0);
this.add(buttonDian);
this.add(buttonDeng);
this.add(buttonChu);
buttonDel.addActionListener(this);
buttonC.addActionListener(this);
buttonZF.addActionListener(this);
buttonGH.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button0.addActionListener(this);
buttonJia.addActionListener(this);
buttonJian.addActionListener(this);
buttonCheng.addActionListener(this);
buttonChu.addActionListener(this);
buttonDeng.addActionListener(this);
buttonDian.addActionListener(this);
init();
}
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
if (b == buttonDian) {
if (this.jtf.getText().indexOf(".") > 0)
calwin.cal.move();
else
jtf.setText(jtf.getText() + ".");
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonZF) {
BigDecimal d = getNum();
d = new BigDecimal("0").subtract(d);
setFormatNum(d);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonDel) {
if (jtf.getText().length() == 1 || jtf.getText().length() == 2
&& jtf.getText().startsWith("-")) {
jtf.setText("0");
} else {
String s = jtf.getText();
s = s.substring(0, s.length() - 1);
if (s.endsWith("E+") || s.endsWith("E-"))
s = s.substring(0, s.length() - 2);
jtf.setText(s);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
}
} else if (b == buttonC) {
init();
} else if (b == buttonJia) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
if (oper == '-' || oper == '/') {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
// System.out.print("111我执行啦\n");
oper = '+';
} else if (b == buttonJian) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
s = operate(d1, oper, d2);
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '-';
} else if (b == buttonCheng) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
if (oper == '-' || oper == '/') {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '*';
} else if (b == buttonChu) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
s = operate(d1, oper, d2);
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '/';
} else if (b == buttonGH) {
if (jtf.getText().startsWith("-")) {
calwin.cal.move();
init();
return;
}
BigDecimal d = getNum();
MathContext mc = new MathContext(10, RoundingMode.HALF_DOWN);
d = new BigDecimal(Math.sqrt(d.doubleValue()), mc);
setFormatNum(d);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonDeng) {
BigDecimal s = null;
d2 = getNum();
if (!key.equals("=")) {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
if (oper != '=' && !key.equals("=")) {// 上一次操作为 +-*/
d1 = getNum();
}
setFormatNum(s);
first = true;
} else {
if (this.jtf.getText().equals("0") || allKey.indexOf(key) == -1
) {
this.jtf.setText(b.getText());
}
else {
this.jtf.setText(jtf.getText() + b.getText());
}
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}// d2作被加数:d2+d1
}
key = b.getText();
// System.out.println(d1.toString()+" "+d2.toString());
}
public BigDecimal getNum() {
BigDecimal d = new BigDecimal(jtf.getText());
return d;
}
public void setFormatNum(BigDecimal dd) {
MathContext mc = new MathContext(18, RoundingMode.HALF_DOWN);
BigDecimal d = new BigDecimal(dd.toString(), mc);
String num = d.toString();
if (num.endsWith(".0"))
num = num.replaceAll("\\.0", "");
this.jtf.setText(num);
// System.out.print("222我执行啦\n");
}
public void init() {
d1 = new BigDecimal("0");
d1 = new BigDecimal("0");
key = "=";
oper = '=';
jtf.setText("0");
first = true;
}
public BigDecimal operate(BigDecimal a1, char c, BigDecimal a2) {
MathContext mc = new MathContext(10, RoundingMode.HALF_DOWN);
switch (c) {
case '+':
return a1.add(a2);
case '-':
return a1.subtract(a2);
case '*':
return a1.multiply(a2);
case '/':
if (a2.compareTo(new BigDecimal("0")) == 0) {
init();
calwin.cal.move();
return new BigDecimal("0");
}
return a1.divide(a2, mc);
default:
return a2;
}
}
}