网友您好, 请在下方输入框内输入要搜索的题目:
下面是一个Applet程序,其功能是捕捉用户所按下的键,并将捕捉到的键时间,传给程序,程序再将所按下的字符显示在Applet中。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.applet.*;
/*
<applet code="ex15_3.class,, width=800 height=400 >
</applet>
*/
public class ex15_3 extends JApplet{
private String str1;
private String str2;
private String str3;
public void init(){
str1 = "Character:";
str2 = "Number: ";
str3 = "Key State:";
}
public void paint(Graphics g){
g.drawString(str1, 20, 30);
g.drawString(str2, 20, 50);
g.drawString(str3, 20, 70);
}
public boolean keyUp(Event e, int n){
str1 = "Character:" + String.valueOf(n);
str2 = "Number:" + n;
str3 = "Key State:Key Up";
repaint();
return true;
}
public boolean keyDown(Event e, int n){
str1 = "Character:" + String.valueOf(n);
str2 = "Number:" + n;
str3 = "Key State:Key Down";
repaint();
return true;
}
}
ex15_3.html
<HTML>
<HEAD>
<TITLE>ex15_3</TITLE>
</HEAD>
<BODY>
<applet code="ex15 3.class" width=800 height=400 >
</applet>
</BODY>
</HTML>
参考答案