import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class UI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea jl1;
private JPanel jp2;
private JPanel jp1;
private JButton[] jb1;
String str = null;
String str1[];
private int j;
private boolean flag = true;
private BigDecimal a;
private int time = 0;;
public UI() {
str1 = new String[50];
jl1 = new JTextArea();
jp1 = new JPanel(new GridLayout(5, 4, 4, 4));
jp2 = new JPanel(new BorderLayout());
jp2.add(jl1);
jb1 = new JButton[20];
jb1[0] = new JButton("c");
jb1[1] = new JButton("/");
jb1[2] = new JButton("*");
jb1[3] = new JButton("DEL");
jb1[4] = new JButton("7");
jb1[5] = new JButton("8");
jb1[6] = new JButton("9");
jb1[7] = new JButton("+");
jb1[8] = new JButton("4");
jb1[9] = new JButton("5");
jb1[10] = new JButton("6");
jb1[11] = new JButton("-");
jb1[12] = new JButton("1");
jb1[13] = new JButton("2");
jb1[14] = new JButton("3");
jb1[15] = new JButton("=");
jb1[16] = new JButton("0");
jb1[17] = new JButton("#");
jb1[18] = new JButton(".");
jb1[19] = new JButton("%");
for (int i = 0; i < jb1.length; i++) {
jp1.add(jb1[i], i);
jb1[i].addActionListener(this);
}
this.add(jp2, "Center");
this.add(jp1, "South");
this.setBounds(400, 200, 270, 250);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new UI();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1[12]) {
str = jb1[12].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[13]) {
str = jb1[13].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[14]) {
str = jb1[14].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[7]) {
if (flag == true) {
str = jb1[7].getText();
jl1.append(str);
j = 1;
flag = false;
time = 0;
} else {
JOptionPane.showMessageDialog(this, "无法输入");
}
} else if (e.getSource() == jb1[8]) {
str = jb1[8].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[9]) {
str = jb1[9].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[10]) {
str = jb1[10].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[4]) {
str = jb1[4].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[5]) {
str = jb1[5].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[6]) {
str = jb1[6].getText();
jl1.append(str);
flag = true;
} else if (e.getSource() == jb1[11]) {
System.out.println(flag);
if (flag == true) {
str = jb1[11].getText();
jl1.append(str);
j = 2;
flag = false;
time = 0;
} else {
JOptionPane.showMessageDialog(this, "无法输入");
}
} else if (e.getSource() == jb1[2]) {
System.out.println(flag);
if (flag == true) {
str = jb1[2].getText();
jl1.append(str);
j = 3;
flag = false;
time = 0;
} else {
JOptionPane.showMessageDialog(this, "无法输入");
}
} else if (e.getSource() == jb1[1]) {
if (flag == true) {
str = jb1[1].getText();
jl1.append(str);
j = 4;
flag = false;
time = 0;
} else {
JOptionPane.showMessageDialog(this, "无法输入");
}
} else if (e.getSource() == jb1[0]) {
jl1.setText("");
} else if (e.getSource() == jb1[18]) {
if (time == 0) {
str = jb1[18].getText();
jl1.append(str);
}
time++;
} else if (e.getSource() == jb1[16]) {
str = jb1[16].getText();
jl1.append(str);
} else if (e.getSource() == jb1[3]) {
String str2 = jl1.getText().toString();
if (str2.length() > 0) {
str2 = str2.substring(0, str2.length() - 1);
jl1.setText(str2);
}
}
if (e.getSource() == jb1[15]) {
if (flag) {
caculate(j);
for (int i = 0; i < str1.length; i++) {
System.out.println(str1[i]);
}
}
}
}
public void caculate(int i) {
switch (i) {
case 1:
str1 = jl1.getText().trim().split("\\+");
System.out.println(str1[0]);
double a;
a = Double.parseDouble(str1[0]) + Double.parseDouble(str1[1]);
;
jl1.setText(String.valueOf(a));
time = 0;
break;
case 2:
str1 = jl1.getText().trim().split("\\-");
double b;
b = Double.parseDouble(str1[0]) - Double.parseDouble(str1[1]);
jl1.setText(String.valueOf(b));
time = 0;
break;
case 3:
str1 = jl1.getText().trim().split("\\*");
double c;
c = Double.parseDouble(str1[0]) * Double.parseDouble(str1[1]);
jl1.setText(String.valueOf(c));
time = 0;
break;
case 4:
str1 = jl1.getText().trim().split("\\/");
double d;
d = (Double.parseDouble(str1[0])) / Double.parseDouble(str1[1]);
jl1.setText(String.valueOf(d));
time = 0;
break;
default:
break;
}
}
}