package calculator;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
public class calculator extends JFrame implements ActionListener {
// 第一行的计算式
private JTextField tfinput = new JTextField();
// 第二行的计算结果,初始值设为0
private JTextField tfAnswer = new JTextField("0");
private String x="";
private String y="";
private String fuhao="";
private String show="";
private Double answer;
// 构造方法
public calculator() {
// 调用父类的构造函数,
super("计算器");
// 各个按钮上的文字
String[] keysValue = { "7", "8", "9", "÷", "4", "5", "6",
"×", "1", "2", "3", "-", "0","CE", "+","=" };
// 各个按钮上的动作命令标识
String[] actionCmd = { "7", "8", "9", "/", "4", "5", "6",
"*", "1", "2", "3", "-", "0","CE", "+","=" };
JButton keys[] = new JButton[keysValue.length];
Font font=new Font("宋体",Font.PLAIN,18);
tfinput.setBounds(10, 10, 240, 40);
tfinput.setFont(font);
tfinput.setBackground(Color.white);
tfinput.setEditable(false);// 计算式不能修改
// 设置计算结果文本框大小
tfAnswer.setBounds(10, 50, 240, 40);
tfAnswer.setFont(font);
tfAnswer.setBackground(Color.white);
tfAnswer.setHorizontalAlignment(SwingConstants.RIGHT);
tfAnswer.setEditable(false);// 计算结果不能修改
// 设置窗口布局
this.setLayout(null);
this.add(tfAnswer); // 将计算式文本框添加到窗口中
this.add(tfinput);// 将计算结果文本框添加到窗口中
// 放置按钮
int x = 10, y = 100;
for (int i = 0; i < keysValue.length; i++) {
keys[i] = new JButton();
keys[i].setText(keysValue[i]);
keys[i].setActionCommand(actionCmd[i]);
keys[i].setBounds(x, y, 60, 45);
keys[i].setFont(font);
if (x <= 130) {
x += 60;
} else {
x = 10;
y += 50;
}
this.add(keys[i]);
}
// 每个按钮都添加监听
for (int i = 0; i < keysValue.length; i++) {
keys[i].addActionListener(this);
}
// 窗口大小不能调整
this.setResizable(false);
// 设置窗口大小
this.setSize(270, 350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void dengyu(String fh){
int key=1;
if(fh.equals("/")&&y.equals("0")){
key=0;
tfAnswer.setText("Error");
}
else if(fh.equals("+"))
answer=Double.parseDouble(x)+Double.parseDouble(y);
else if(fh.equals("-"))
answer=Double.parseDouble(x)-Double.parseDouble(y);
else if(fh.equals("/"))
answer=Double.parseDouble(x)/Double.parseDouble(y);
else if (fh.equals("*"))
answer=Double.parseDouble(x)*Double.parseDouble(y);
if(key==1){
x=Double.toString(answer); // 将结果付给第一个数,3+2 再按+号 5就等于第一个数 然后再输入第二个数
tfAnswer.setText(x);
y="";
fuhao="";
}
}
// 事件处理
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("1")
|| e.getActionCommand().equals("2")
|| e.getActionCommand().equals("3")
|| e.getActionCommand().equals("4")
|| e.getActionCommand().equals("5")
|| e.getActionCommand().equals("6")
|| e.getActionCommand().equals("7")
|| e.getActionCommand().equals("8")
|| e.getActionCommand().equals("9")
|| e.getActionCommand().equals("0")){ //数字
if(!fuhao.equals("")){// 有符号
if(y.startsWith("0")) //如果0开头,按下如何数字将取代它
y=e.getActionCommand();
else
y+=e.getActionCommand(); //否则非零开头 加到字符串末尾 “2”+“3”="23"
if(y.startsWith("-")) //如果以负号开头, "-"+"3"="-3"
show+=y.substring(1);
else
if(y.length()>1) //不是负数, 且长度大于1
show+=y.substring(y.length()-1);// 将字符串的末尾显示
else
if(!show.endsWith("0")) //长度为一 ,且不为0
show+=y; //直接显示
else{
}
tfinput.setText(show);
}else{
if(x.startsWith("0"))//如果0开头
x=e.getActionCommand();
else
x+=e.getActionCommand();
if(x.length()>1) // 用于显示
show+=x.substring(x.length()-1);
else //x为一位数
if(!x.equals("0"))//x是不为零的一位数
if(show.equals("0"))
show=x;
else
show+=x;
else{
if(show.equals(""))
show+=x;
}
if(show.startsWith("--"))
show=show.substring(1);
tfinput.setText(show);
}
}
// 运算符
if(e.getActionCommand().equals("CE")){
x="";
y="";
fuhao="";
show="";
tfAnswer.setText("0");
tfinput.setText("");
}
//加法
if(e.getActionCommand().equals("+")){
if(x.equals("")){ //无第一个数
if(!x.startsWith("+")){
x="+"+x; //第一个数显示正号
show+=x;
}
tfinput.setText(show);
}else{ // 有第一个数
if(!fuhao.equals("")){ //有符号
if(!y.equals("")){ //连续运算
dengyu(fuhao);
fuhao="+";
show+=fuhao;
tfinput.setText(show);
}
}else{ //无符号
fuhao="+";
show+=fuhao;
tfinput.setText(show);
}
}
}
//减法
if(e.getActionCommand().equals("-")){
if(!fuhao.equals("")){ // 有符号
if(!y.equals("")&&!y.equals("-")){ //y是一个可以参与计算的数
dengyu(fuhao);
fuhao="-";
show+=fuhao;
tfinput.setText(show);
}else{ //有符号, 没有第二个数
if(!y.startsWith("-")){ //表示负数
y="-"+y;
show+=y;
}
tfinput.setText(show);
}
}else{// 无符号
if(x.equals("")){ //第一个数为空,添加负数
x="-"+x;
show+=x;
tfinput.setText(show);
}else{ //第一个数不为空,添加符号
fuhao="-";
show+=fuhao;
tfinput.setText(show);
}
}
}
//乘法
if(e.getActionCommand().equals("*")){
if(!fuhao.equals("")){ //有符号
if(!y.equals("")) //将前面的数进行运算,将符号录入
dengyu(fuhao);
}
if(!show.endsWith("/")&&!show.endsWith("*")&&!show.endsWith("+")&&!show.endsWith("-")&&!show.equals("")){
fuhao="*"; //防止连续符号出现
show+=fuhao;
}
tfinput.setText(show);
}
//除法
if(e.getActionCommand().equals("/")){
if(!fuhao.equals("")){
if(!y.equals("")&&!y.equals("0")) // 排查除0运算
dengyu(fuhao);
if(y.equals("0"))
tfAnswer.setText("Error"); //除0
}
if(!show.endsWith("/")&&!show.endsWith("*")&&!show.endsWith("+")&&!show.endsWith("-")&&!show.equals("")){
fuhao="/";
show+=fuhao;
}
tfinput.setText(show);
}
//等于
if(e.getActionCommand().equals("="))
if(!x.equals("")&&!y.equals("")&&!fuhao.equals(""))
dengyu(fuhao);
}
// 主函数
public static void main(String[] args) {
new calculator();
}
}