import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
public class Jsq extends JFrame implements ActionListener
{
JFrame jf = new JFrame("计算器");
JTextField JtfAnswer = new JTextField();
JButton bPoint,bEqual,bPlus,bMinus,bMulti,bDivision,bSqrt,bDao,bMod,
bAddandSub,bSub,bC,bBackspace,bMAdd,bCe,Tzero;
JButton b[];
boolean com = false;//所按键是否是"+","-","*","/"
int i = 0,j=0,k=0,p=1;
String sum = " ",sum1=" ";
double total = 0,m=0;
int symbol = 0;
public Jsq() //初始化计数器界面
{
Container c = jf.getContentPane();
JtfAnswer.setHorizontalAlignment(JTextField.RIGHT);
JtfAnswer.setForeground(Color.blue);
JtfAnswer.setText("0.0");
JtfAnswer.setFocusable(false);
c.add(JtfAnswer,"North");
JLabel name = new JLabel("***",JLabel.CENTER);
name.setForeground(Color.blue);
name.setFont(new Font("隶书 ",Font.ITALIC|Font.BOLD,24));
c.add(name,"South");
JPanel pnl=new JPanel();
c.add(pnl,"Center");
b= new JButton[10];
for (int i = 0; i < b.length; i++)
{
b[i] = new JButton(Integer.toString(i));
}
pnl.setLayout(new GridLayout(5,5,6,8));
JButton Space2 = new JButton(" ");
Space2.addActionListener(this);
Space2.setBackground(Color.lightGray);
pnl.add(Space2);
Tzero = new JButton("");
Tzero.setForeground(Color.blue);
Tzero.setBackground(Color.lightGray);
Tzero.addActionListener(this);
pnl.add(Tzero);
bCe = new JButton("");
bCe.setBackground(Color.lightGray);
bCe.addActionListener(this);
pnl.add(bCe);
bBackspace = new JButton("back");
bBackspace.setForeground(Color.red);
bBackspace.setBackground(Color.lightGray);
bBackspace.addActionListener(this);
pnl.add(bBackspace);
bC = new JButton("C");
bC.setForeground(Color.red);
bC.setBackground(Color.lightGray);
bC.addActionListener(this);
pnl.add(bC);
b[1].addActionListener(this);
b[1].setForeground(Color.blue);
b[1].setBackground(Color.lightGray);
pnl.add(b[1]);
b[2].addActionListener(this);
b[2].setForeground(Color.blue);
b[2].setBackground(Color.lightGray);
pnl.add(b[2]);
b[3].addActionListener(this);
b[3].setForeground(Color.blue);
b[3].setBackground(Color.lightGray);
pnl.add(b[3]);
bPlus=new JButton("+");
bPlus.setForeground(Color.red);
bPlus.setBackground(Color.lightGray);
bPlus.addActionListener(this);
pnl.add(bPlus);
bSqrt=new JButton("sqrt");
bSqrt.setForeground(Color.red);
bSqrt.setBackground(Color.lightGray);
bSqrt.addActionListener(this);
pnl.add(bSqrt);
b[4].addActionListener(this);
b[4].setForeground(Color.blue);
b[4].setBackground(Color.lightGray);;
pnl.add(b[4]);
b[5].addActionListener(this);
b[5].setForeground(Color.blue);
b[5].setBackground(Color.lightGray);
pnl.add(b[5]);
b[6].addActionListener(this);
b[6].setForeground(Color.blue);
b[6].setBackground(Color.lightGray);
pnl.add(b[6]);
bMinus=new JButton("-");
bMinus.setForeground(Color.red);
bMinus.setBackground(Color.lightGray);
bMinus.addActionListener(this);
pnl.add(bMinus);
bMod=new JButton("%");
bMod.setForeground(Color.red);
bMod.setBackground(Color.lightGray);
bMod.addActionListener(this);
pnl.add(bMod);
b[7].addActionListener(this);
b[7].setForeground(Color.blue);
b[7].setBackground(Color.lightGray);
pnl.add(b[7]);
b[8].addActionListener(this);
b[8].setForeground(Color.blue);
b[8].setBackground(Color.lightGray);
pnl.add(b[8]);
b[9].addActionListener(this);
b[9].setForeground(Color.blue);
b[9].setBackground(Color.lightGray);
pnl.add(b[9]);
bMulti=new JButton("*");
bMulti.setForeground(Color.red);
bMulti.setBackground(Color.lightGray);
bMulti.addActionListener(this);
pnl.add(bMulti);
bDao=new JButton("1/x");
bDao.setForeground(Color.red);
bDao.setBackground(Color.lightGray);
bDao.addActionListener(this);
pnl.add(bDao);
b[0].setForeground(Color.blue);
b[0].setBackground(Color.lightGray);
b[0].addActionListener(this);
pnl.add(b[0]);
bPoint=new JButton(".");
bPoint.setForeground(Color.red);
bPoint.setBackground(Color.lightGray);
bPoint.addActionListener(this);
pnl.add(bPoint);
bAddandSub=new JButton("+/-");
bAddandSub.setForeground(Color.red);
bAddandSub.setBackground(Color.lightGray);
bAddandSub.addActionListener(this);
pnl.add(bAddandSub);
bDivision=new JButton("/");
bDivision.setForeground(Color.red);
bDivision.setBackground(Color.lightGray);
bDivision.addActionListener(this);
pnl.add(bDivision);
bEqual=new JButton("=");
bEqual.setForeground(Color.red);
bEqual.setBackground(Color.lightGray);
bEqual.addActionListener(this);
pnl.add(bEqual);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.pack();
jf.show();
}
public void actionPerformed(ActionEvent e)
{
for(int m=1;m<10;m++) //获得数字键
{
if(e.getSource()==b[m])
{ p=1;
if (com || i == 0)
{
JtfAnswer.setText(Integer.toString(m));
com = false;
i = 1;
}
else
{sum1=JtfAnswer.getText();
sum = JtfAnswer.getText();
JtfAnswer.setText(sum + Integer.toString(m));
}
sum = JtfAnswer.getText();
}
}
if (e.getSource() == b[0])
{ p=1;
if (com || i == 0)
{
JtfAnswer.setText("0");
com = false;
i = 1;
}
else
{ sum1 = JtfAnswer.getText();
sum = JtfAnswer.getText();
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0)
{
JtfAnswer.setText(sum + "0");
}
else
{
if (sum.trim().indexOf(".") == -1)
{
JtfAnswer.setText(sum);
}
else
{
JtfAnswer.setText(sum + "0");
}
}
}
}
else
if (e.getSource() == bC) //清零
{
JtfAnswer.setText("0");
i = 0;j=0;symbol=0;k=0;p=1;
com = true;sum1="";
}
else
if (e.getSource() == bPoint) //小数点
{
p=1;
if (com || i == 0)
{
JtfAnswer.setText("0.");
com = false;
i = 1;
}
else
{ sum1=JtfAnswer.getText();
sum = JtfAnswer.getText();
if (sum.trim().indexOf(".") == -1)
{
JtfAnswer.setText(sum + ".");
}
else
{
JtfAnswer.setText(sum);
}
}
}
else
if(e.getSource() == bSqrt) //开根号
{
com=false;
i=0;j=0;p=1;sum1="";
total = Double.parseDouble(JtfAnswer.getText());
if(total>=0)
{
double sq;
sq = Math.sqrt(total);
JtfAnswer.setText(sq+" ");
}
else
JtfAnswer.setText("被开方数不能小于零,") ;
}
else
if(e.getSource() == bDao) //求倒数
{
com = false;
i=0;j=0;p=1;sum1="";
if(Double.parseDouble(JtfAnswer.getText())!=0)
{
total=Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(1.0/total+" ");
}
else
{
JtfAnswer.setText("除数不能为零,");
}
}
else
if(e.getSource() == bAddandSub )
{
com = false;
i=0;j=0;p=1;sum1="";sum1="";
if(Double.parseDouble(JtfAnswer.getText())>0)
JtfAnswer.setText("-"+Double.parseDouble(JtfAnswer.getText()));
else
JtfAnswer.setText(""+Math.abs(Double.parseDouble(JtfAnswer.getText())));
}
else
if(e.getSource() == bMod)//求百分比
{
com=false;
i=0;j=0;p=1;sum1="";
JtfAnswer.setText(" "+Double.parseDouble(JtfAnswer.getText())/100);
}
else
if(e.getSource() == bBackspace) //退格键
{
com=false;
i=0;j=0;p=1;sum1="";
sum=sum.substring(0,sum.length()-1);
JtfAnswer.setText(sum);
}
//获得点击+,-,*./之前的数值
else
if (e.getSource() == bPlus)
{
com = true;
p = 1;sum1="";
if(j==0){total = Double.parseDouble(JtfAnswer.getText());symbol = 1;k=1;}
j=j+1;
if(j==2)
{
switch (symbol)
{
case 1:
{
double ad = total + Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;k=1;
total=Double.parseDouble(JtfAnswer.getText());
break;
}
case 2:
{if(k!=1){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());
total=Double.parseDouble(JtfAnswer.getText());
symbol=1;k=1;j=1;i=0;}
else{ double ad = total - Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(String.valueOf(ad));
i = 0;j=1;symbol=1;
total=Double.parseDouble(JtfAnswer.getText());}
}
break;
}
case 3:
{if(k!=1){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=1;k=1;j=1;i=0;}
else{double ad = total * Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=1;
total=Double.parseDouble(JtfAnswer.getText());}}
break;
}
case 4:
{if(k!=1){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=1;k=1;j=1;i=0;}
else{double ad = total / Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=1;
total=Double.parseDouble(JtfAnswer.getText());}}
break;
}
}
}
}
else
if (e.getSource() == bMinus)
{
com = true;
p=1;sum1="";
if(j==0){total = Double.parseDouble(JtfAnswer.getText());symbol = 2;k=2;i = 0;}
j=j+1;
if(j==2 )
{
switch (symbol)
{
case 1:
{if(k!=2){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=2;k=2;j=1;i=0;}
else{double ad = total + Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=2;
total=Double.parseDouble(JtfAnswer.getText());}}
break;
}
case 2:
{
double ad = total - Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(String.valueOf(ad));
i = 0;j=1;k=2;
total=Double.parseDouble(JtfAnswer.getText());
break;
}
case 3:
{ if(k!=2){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=2;k=2;j=1;i=0;}
else{double ad = total * Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=2;
total=Double.parseDouble(JtfAnswer.getText());}}
break;
}
case 4:
{ if(k!=2){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=2;k=2;j=1;i=0;}
else{double ad = total / Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=2;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
}
}
}
else
if (e.getSource() == bMulti)
{
com = true;
p=1;sum1="";
if(j==0){total = Double.parseDouble(JtfAnswer.getText());symbol = 3;k=3;i = 0;}
j=j+1;
if(j==2)
{
switch (symbol)
{
case 1:
{if(k!=3){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=3;k=3;j=1;i=0;}
else{double ad = total + Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=3;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
case 2:
{ if(k!=3){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=3;k=3;j=1;i=0;}
else{double ad = total - Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(String.valueOf(ad));
i = 0;j=1;symbol=3;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
case 3:
{
double ad = total * Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;k=3;
total=Double.parseDouble(JtfAnswer.getText());
break;
}
case 4:
{ if(k!=3){if(i!=0){JtfAnswer.setText(JtfAnswer.getText());symbol=3;k=3;j=1;i=0;}
else{double ad = total / Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=3;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
}
}
}
else
if (e.getSource() == bDivision)
{
com = true;
p=1;sum1="";
if(j==0){total = Double.parseDouble(JtfAnswer.getText());symbol = 4;k=4;i = 0;}
j=j+1;
if(j==2)
{
switch (symbol)
{
case 1:
{if(k!=4){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=4;k=4;j=1;i=0;}
else{double ad = total + Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=4;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
case 2:
{ if(k!=4){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=4;k=4;j=1;i=0;}
else{double ad = total - Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(String.valueOf(ad));
i = 0;j=1;symbol=4;
total=Double.parseDouble(JtfAnswer.getText());
}}
break;
}
case 3:
{ if(k!=4){if(i!=1){JtfAnswer.setText(JtfAnswer.getText());symbol=4;k=4;j=1;i=0;}
else{double ad = total * Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;symbol=4;
total=Double.parseDouble(JtfAnswer.getText());
}
}
break;
}
case 4:
{
double ad = total / Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=1;k=4;
total=Double.parseDouble(JtfAnswer.getText());
break;
}
}
}
}
else
if (e.getSource() == bEqual)
{p=p+1;
switch (symbol)
{
case 1://计算加法
{
double ad = total + Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=0;
sum = "";
break;
}
case 2://计算减法
{
double ad = total - Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(String.valueOf(ad));
i = 0;j=0;
sum = "";
break;
}
case 3://计算乘法
{
double ad = total * Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=0;
sum = "";
break;
}
case 4://计算除法
{ if(Double.parseDouble(JtfAnswer.getText())==0)
{JtfAnswer.setText("除数不能为零。");
}
else{
double ad = total / Double.parseDouble(JtfAnswer.getText());
JtfAnswer.setText(ad + "");
i = 0;j=0;
sum = "";}
break;
}
}
System.out.println(com);System.out.println("symbol"+"="+symbol);
}
}
public static void main(String[] args)
{
Jsq jsq=new Jsq();
}
}