[问答题] 综合应用题下面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Label构件。要求点击First时则能在Label中显示出Command:First,而

[问答题] 综合应用题下面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Label构件。要求点击First时则能在Label中显示出Command:First,而,第1张

[问答题] 综合应用题下面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Label构件。要求点击First时则能在Label中显示出Command:First,而

[问答题] 综合应用题下面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Label构件。要求点击First时则能在Label中显示出Command:First,而点击Second时则能显示出Command:Second,要求只能使用重载一次actionPerformed()方法。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。程序运行结果如下: 注意:不改动程序的结构,不得增行或删行。import java.awt.*;import java.awt.event.*;import java.applet.*;public class ex2_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.addActionListener(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);}}ex2_3.html<

正确答案:

btn.addActionListener(this)btn.setActionCommand("second")l.setText(str)

参考解析:

本题主要考查Java语言中高级事件ActionEvent和AWT基本构件Label的常用方法的使用。解题关键是熟练掌握动作事件ActionEvent和Label构件的常用方法。在本题中,第1处,明确注册的事件监听器是监听按钮的,而不是Label的;第2处,调用ActionEvent的setActionCommand()方法改变了ActionCommand,使按下第二个按钮时显示Command:second而不是Command: First。第3处,调用Label的setText()方法,而不是Button的方法。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5469252.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存