public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==b1)
{
str = ta.getText() + "1"; //把1放文本框内
ta.setText(str);
}
else if(e.getSource()==b2)
{ str = ta.getText() + "2"; //把2放文本框内
ta.setText(str);
}
else if(e.getSource()==b3)
{ str = ta.getText() + "3";//把3放文本框内
ta.setText(str);
}
else if(e.getSource()==b4)
{ str = ta.getText() + "4";
ta.setText(str);
}//把4放文本框内
else if(e.getSource()==b5)
{ str = ta.getText() + "5";
ta.setText(str);
}//把5放文本框内
else if(e.getSource()==b6)
{ str = ta.getText() + "6";
ta.setText(str);
}//把6放文本框内
else if(e.getSource()==b7)
{ str = ta.getText() + "7";
ta.setText(str);
}//把7放文本框内
else if(e.getSource()==b8)
{ str = ta.getText() + "8";
ta.setText(str);
}//把8放文本框内
else if(e.getSource()==b9)
{ str = ta.getText() + "9";
ta.setText(str);
}//把9放文本框内
else if(e.getSource()==b0)
{ str = ta.getText() + "0";
ta.setText(str);
}
//符号事件
else if(e.getSource()==a1) //点号
{
str=ta.getText();
if(str.indexOf(".")<=1)
{
str+=".";
ta.setText(str);
}
}
else if(e.getSource()==a3) //AC号
{
ta.setText("");
}
else if(e.getSource()==a4) //BackSpace
{
str=ta.getText();
if(str.length()>0)
ta.setText(str.substring(0,str.length()-1));
//指引当前位置
}
else if(e.getSource()==f1) //加号
{
num1=Double.parseDouble(ta.getText());
add=true;
ta.setText("");
}
else if(e.getSource()==f2) //减号
{
num1=Double.parseDouble(ta.getText());
sub=true;
ta.setText("");
}
else if(e.getSource()==f3) //乘
{
num1=Double.parseDouble(ta.getText());
mul=true;
ta.setText("");
}
else if(e.getSource()==f4) //除
{
num1=Double.parseDouble(ta.getText());
div=true;
ta.setText("");
}
else if(e.getSource()==a5) //平方
{
num1=Double.parseDouble(ta.getText());
num1=num1*num1;
ta.setText(num1+"");
}
else if(e.getSource()==a6) //平方根
{
num1=Double.parseDouble(ta.getText());
num1= Math.sqrt(num1);
ta.setText(num1+"");
}
else if(e.getSource()==a2) //等号
{
num2=Double.parseDouble(ta.getText());
if(add)
{
num1=num1+num2;
}
else if(sub)
{
num1=num1-num2;
}
else if(mul)
{
num1=num1*num2;
}
else if(div)
{
num1=num1/num2;
}
str = ""+num1;/×r的值拼接上num的值
ta.setText(str);//赋值
add=false;//默认设为false
sub=false;
mul=false;
div=false;
事件监听