import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;public class Calculator extends Applet implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
double x, y = 0; 
int f = 0;
String s= "";
JTextField tf1,tf2;
JButton b1,b2,b3,b4,b5,b6;
JButton b[] = new JButton[11]; public void init(){
setLayout(null);
p1.setBounds(1,2,500,100);
p2.setBounds(1,102,500,375);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(4,5));
tf1 = new JTextField(20);
tf2 = new JTextField(20);
tf1.setEditable(false);
tf2.setEditable(false);
add(p1);
add(p2);
p1.add(tf1);
p1.add(tf2);
for(int i=0; i<10; i++){
String s1=""+i;
b[i] = new JButton(s1);
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10] = new JButton(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("=");
b6 = new JButton("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
s = s+ e.getActionCommand();
for(int i=0; i<11; i++){
if(e.getSource() == b[i]){
tf1.setText(tf1.getText() + e.getActionCommand());
}
if(e.getSource() == b6){
tf1.setText("");
tf2.setText("");
x=0;y=0;f=0;s="";
}
if(e.getSource() == b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;f=1;
}
if(e.getSource() == b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y-x));y=y-x;}
f = 2;
}
if(e.getSource() == b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y*x));y=y*x;}
f = 3;
}
if(e.getSource() == b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y/x));y=y/x;}
f = 4;
}
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y + x));
if(f == 2) tf1.setText(""+(y - x));
if(f == 3) tf1.setText(""+(y * x));
if(f == 4) tf1.setText(""+(y / x));
y=0;
tf2.setText(s + tf1.getText());
}
}
}
}
运行结果只能加减,乘的话结果都是0.0,除的话结果是NaN,请问怎么回事