package two;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class calculator implements ActionListener { int count = 0;
private static int a;
JFrame frame = new JFrame("计算器");
JTextArea area = new JTextArea();
JTextField fieldshow = new JTextField("0");
JTextField fieldcalculator = new JTextField();
JPanel leftpanel = new JPanel();
JPanel rightpanel = new JPanel();
JPanel buttonpanel = new JPanel();
JPanel motionpanel = new JPanel();
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("+");
JButton button5 = new JButton("c");
JButton button6 = new JButton("4");
JButton button7 = new JButton("5");
JButton button8 = new JButton("6");
JButton button9 = new JButton("-");
JButton button10 = new JButton("退格");
JButton button11 = new JButton("7");
JButton button12 = new JButton("8");
JButton button13 = new JButton("9");
JButton button14 = new JButton("*");
JButton button15 = new JButton("sin");
JButton button16 = new JButton("0");
JButton button17 = new JButton("+/-");
JButton button18 = new JButton(".");
JButton button19 = new JButton("/");
JButton button20 = new JButton("=");
JButton button21 = new JButton("保存");
JButton button22 = new JButton("复制");
JButton button23 = new JButton("清除");
StringBuffer S = new StringBuffer("");
// t用来记录前一个运算符号是+ - * / =中的哪一个
char t;
// count用来实现运算符号计数,a用来识别退格、正负号操作前一个操作是否是运算符号+ - * / sin =
// int count = 0, a = 0;
Double x1, x2 = 0d, result;
public static void main(String a[]) {
calculator that = new calculator();
that.go();
}
void go() {
leftpanel.setLayout(new BorderLayout());
leftpanel.add(fieldshow, BorderLayout.NORTH);
leftpanel.add(buttonpanel, BorderLayout.CENTER);
buttonpanel.setLayout(new GridLayout(4, 5));
buttonpanel.add(button1);
buttonpanel.add(button2);
buttonpanel.add(button3);
buttonpanel.add(button4);
buttonpanel.add(button5);
buttonpanel.add(button6);
buttonpanel.add(button7);
buttonpanel.add(button8);
buttonpanel.add(button9);
buttonpanel.add(button10);
buttonpanel.add(button11);
buttonpanel.add(button12);
buttonpanel.add(button13);
buttonpanel.add(button14);
buttonpanel.add(button15);
buttonpanel.add(button16);
buttonpanel.add(button17);
buttonpanel.add(button18);
buttonpanel.add(button19);
buttonpanel.add(button20);
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);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
button17.addActionListener(this);
button18.addActionListener(this);
button19.addActionListener(this);
button20.addActionListener(this);
button21.addActionListener(this);
button22.addActionListener(this);
button23.addActionListener(this);
rightpanel.setLayout(new BorderLayout());
rightpanel.add(fieldcalculator, BorderLayout.NORTH);
rightpanel.add(area, BorderLayout.CENTER);
rightpanel.add(motionpanel, BorderLayout.SOUTH);
motionpanel.add(button21);
motionpanel.add(button22);
motionpanel.add(button23);
Container con = frame.getContentPane();
frame.setLayout(new GridLayout(1, 2));
con.add(leftpanel);
con.add(rightpanel);
frame.setBounds(200, 200, 600, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowListener() {// 关闭程序时弹出警告
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(frame, "确实要关闭程序吗?",
"警告", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_CANCEL_OPTION) {
System.exit(0);
} else {
}
}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
});
}
public void actionPerformed(ActionEvent e) {
// 0123456789等按钮
if (e.getSource() == button1 || e.getSource() == button2
|| e.getSource() == button3 || e.getSource() == button6
|| e.getSource() == button7 || e.getSource() == button8
|| e.getSource() == button11 || e.getSource() == button12
|| e.getSource() == button13 || e.getSource() == button16) {
a = 0;
if (count == 0) {
S.append(e.getActionCommand());
fieldshow.setText(S.toString());
x1 = Double.parseDouble(fieldshow.getText());
} else if (count >= 1) {
S.append(e.getActionCommand());
fieldshow.setText(S.toString());
x2 = Double.parseDouble(fieldshow.getText());
}
}
// 小数点的容错性
if (e.getSource() == button18)// 单击"."按钮输入小数
{
a = 0;
if (fieldshow.getText().trim().indexOf(".") != -1)// 判断字符串中是否已经包含了小数点
{
} else// 如果没有小数点
{
if (fieldshow.getText().trim().equals("0"))// 如果初时显示为0
{
S.setLength(0);
fieldshow.setText((S.append("0" + e.getActionCommand()))
.toString());
} else if (fieldshow.getText().trim().equals(""))// 如果初时显示为空则不做任何操作
{
} else {
fieldshow
.setText(S.append(e.getActionCommand()).toString());
}
}
}
// 运算符号 + - * /
if (e.getSource() == button4 || e.getSource() == button9
|| e.getSource() == button14 || e.getSource() == button19) {
count++;
a = 1;
S.setLength(0);
if (e.getSource() == button4)// +法
{
switch (t) {
case '+':
result = x1 + x2;
fieldshow.setText(result.toString());
fieldcalculator.setText(x1 + "+" + x2 + "=" + result);
area.append(x1 + "+" + x2 + "=" + result + "\n");
break;
case '-':
result = x1 - x2;
fieldshow.setText(result.toString());
fieldcalculator.setText(x1 + "+" + x2 + "=" + result);
area.append(x1 + "-" + x2 + "=" + result + "\n");
break;
case '*':
result = x1 * x2;
fieldshow.setText(result.toString());
fieldcalculator.setText(x1 + "+" + x2 + "=" + result);
area.append(x1 + "*" + x2 + "=" + result + "\n");
break;
case '/':