下面是一个Applet程序,其功能是有两个按钮,分别为First和Second,以及一个Label构件。要求单击 First时能在Label中显示出'Command:First',而单击Second时能显示出'Command:Second',要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
import javA.awt.*;
import javA.awt.event.*;
import javA.applet.*;
/*
<applet code=ex04_3.class width=800 height=400>
</applet>
*/
Public class ex04_3 extends Applet implements ActionListener
{
private String str='ok';
private Label l;
private Button btn;
public void init()
{
setLayout(null);
l=new Label(str);
l.reshape(10,10,100,30);
add(l);
btn=new Button('First');
btn.reshape(10,50,60,20);
l.addActionListene(this);
add(btn);
btn=new Button('Second');
btn.reshape(10,100,60,20);
btn.setActionCommand('First');
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent ae)
{
str='Command:'+aE.getActionCommand();
btn.setText(str);
}
}
ex04_3.html
<HTML>
<HEAD>
<TITLE>ex04_3</TITLE>
</HEAD>
<BODY>
<applet code='ex2_3.class' width=800 height=400>
</applet>
</BODY>
</HTML>
A.awt.*;
B.awt.event.*;
C.applet.*;
D.class
E.reshape(10,10,100,30);
F.reshape(10,50,60,20);
G.addActionListene(this);
H.reshape(10,100,60,20);
I.setActionCommand('First');
J.addActionListener(this);
K.getActionCommand();
L.setText(str);
M.html
N.class'
【参考答案】btn.addActionListener(this)btn.setActionCommand('second')l.s......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)