package Counter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Counter {
public static void main(String args[]) {
MyFrame my = new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener {
JTextField tf;
JPanel p, p1, p2;
JButton b[];
JButton plu, sub, che, chu, xis, ce, deng, bai, sqrt, zhenfu, BackSpace;
boolean end;//用来判断是否输入数字完成
String str;
String s = null;
String ss, q;//存放得到的数和符号
double a;
public MyFrame() {
super("java计算器");
b = new JButton[10];
end = false;
for (int i = 0; i < b.length; i++) {
b[i] = new JButton(" " + i);
}
tf = new JTextField("0", 15);
this.add(tf, BorderLayout.NORTH);
//this.add(BackSpace,BorderLayout.SOUTH);
p = new JPanel();
p1 = new JPanel();
//p2=new Panel();
plu = new JButton("+");
sub = new JButton("-");
BackSpace = new JButton("BackSpace");
che = new JButton("*");
chu = new JButton("/");
xis = new JButton(".");
ce = new JButton("c");
deng = new JButton("=");
bai = new JButton("%");
sqrt = new JButton("sqrt");
zhenfu = new JButton("+/-");
plu.setForeground(Color.red);
sub.setForeground(Color.red);
che.setForeground(Color.red);
chu.setForeground(Color.red);
deng.setForeground(Color.blue);
sqrt.setForeground(Color.blue);
zhenfu.setForeground(Color.blue);
bai.setForeground(Color.blue);
xis.setForeground(Color.green);
ce.setForeground(Color.green);
tf.setForeground(Color.red);
p.setForeground(Color.pink);
p.add(b[1]);
p.add(b[2]);
p.add(b[3]);
p.add(plu);
p.add(b[4]);
p.add(b[5]);
p.add(b[6]);
p.add(sub);
p.add(b[7]);
p.add(b[8]);
p.add(b[9]);
p.add(che);
p.add(ce);
p.add(b[0]);
p.add(xis);
p.add(chu);
p.add(zhenfu);
p.add(bai);
p.add(sqrt);
p.add(deng);
p1.add(BackSpace);
//p2.add(tf);
this.add(p, BorderLayout.CENTER);
this.add(p1, BorderLayout.SOUTH);
//this.add(p2,BorderLayout.NORTH);
p.setLayout(new GridLayout(5, 5));
this.setSize(250, 200);
this.setVisible(true);
xis.addActionListener(this);
ce.addActionListener(this);
plu.addActionListener(this);
sub.addActionListener(this);
che.addActionListener(this);
chu.addActionListener(this);
bai.addActionListener(this);
deng.addActionListener(this);
zhenfu.addActionListener(this);
sqrt.addActionListener(this);
BackSpace.addActionListener(this);
for (int i = 0; i < b.length; i++) {
b[i].addActionListener(this);
b[i].setForeground(Color.green);
}
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void num(int i) {
s = String.valueOf(i);
if (end) {
tf.setText("0");
end = false;
}
if ((tf.getText()).equals("0")) {
tf.setText(s);
} else {
str = tf.getText() + s;//如果文本框的内容不为零,则在内容后面添加数字
tf.setText(str);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b[1]) {
num(1);
} else if (e.getSource() == b[2]) {
num(2);
} else if (e.getSource() == b[3]) {
num(3);
} else if (e.getSource() == b[4]) {
num(4);
} else if (e.getSource() == b[5]) {
num(5);
} else if (e.getSource() == b[6]) {
num(6);
} else if (e.getSource() == b[7]) {
num(7);
} else if (e.getSource() == b[8]) {
num(8);
} else if (e.getSource() == b[9]) {
num(9);
} else if (e.getSource() == b[0]) {
num(0);
} else if (e.getSource() == xis) {
if (tf.getText().trim().indexOf('.') != -1) {
} else {
tf.setText(tf.getText() + ".");
}
} else if (e.getSource() == ce) {
tf.setText("0");
} else if (e.getSource() == deng) {
jisuan();
ss = String.valueOf(a);
tf.setText(ss);
q = "";
} else if (e.getSource() == plu) {
jisuan();
tf.setText("");
q = "+";
} else if (e.getSource() == sub) {
jisuan();
tf.setText("");
q = "-";
} else if (e.getSource() == chu) {
jisuan();
tf.setText("");
q = "/";
} else if (e.getSource() == che) {
jisuan();
tf.setText("");
q = "*";
} else if (e.getSource() == bai) {
tf.setText("" + (0.01 * Double.parseDouble(tf.getText())));
} else if (e.getSource() == zhenfu) {
if (tf.getText().trim().indexOf('-') == -1 && tf.getText().length() > 0) {
tf.setText("" + ((-1) * Double.parseDouble(tf.getText())));
} else if (tf.getText().length() > 0) {
String y = tf.getText().substring(1);
tf.setText(y);
}
} else if (e.getSource() == sqrt) {
jisuan();
tf.setText("" + a);
q = "sqrt";
} else if (e.getSource() == BackSpace) {
String sq = tf.getText();
tf.setText("");
for (int i = 0; i < sq.length() - 1; i++) {
char a = sq.charAt(i);
tf.setText(tf.getText() + a);
}
}
}
public void jisuan() {
if (q == "+") {
a += Double.parseDouble(tf.getText());
} else if (q == "-") {
a -= Double.parseDouble(tf.getText());
} else if (q == "*") {
a *= Double.parseDouble(tf.getText());
} else if (q == "/") {
a /= Double.parseDouble(tf.getText());
} else if (q == "sqrt") {
a = Math.sqrt(Double.parseDouble(tf.getText()));
} else {
a = Double.parseDouble(tf.getText());
}
}
}