t1.setText(将String.valueOf(V));与t2.setText(String.valueOf(v))不是交互式的 这里,如果我按下一个整数(两个它将显示前一个和当前整数)valueu t1不显示,但t2将给出数据变化的即时反映
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class aacal extends Applet implements ActionListener{
static int cv,pv,v,res;
String cap;
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("+");
Button b4=new Button("=");
TextField t1=new TextField(10);
TextField t2=new TextField(10);
public void init(){
// This textfield is to display the result and integer on buttons
add(t1);
t1.addActionListener(this);
t1.setText("0");
b1.addActionListener(this);
add(b1);
b2.addActionListener(this);
add(b2);
b3.addActionListener(this);
add(b3);
b4.addActionListener(this);
add(b4);
//This textfield is to display the ActionCommand on buttons
add(t2);
t2.addActionListener(this);
t2.setText("0");
}
public void actionPerformed(ActionEvent ae){
cap=ae.getActionCommand();
try{
cv=Integer.parseInt(t2.getText());
v=cv*10+Integer.parseInt(cap);
t2.setText(String.valueOf(v));
t1.setText(String.valueOf(v));
}catch(NumberFormatException ne){
try{
t2.setText(cap);
}catch(NumberFormatException e){
t2.setText("BYE");
}
}
switch(cap){
case "+":
pv=cv;
break;
case"=":
res=cv+pv;
t1.setText(String.valueOf(res));
break;
}
}
}
答案 0 :(得分:0)
v=cv*10+Integer.parseInt(cap);
中有一个例外,导致t2.setText(cap);
被执行。