问答题 [说明] 面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Label控件。要求点击First时则能在Label中显示出Command:First,而点击Second时则能显示出 Command: Second,要求只能使用重载一次actionPerfonned()方法。 程序运行结果如图6所示。
【正确答案】
【答案解析】(1)setLayout(null); (2)btn=new Button("First"); (3)btn.addActionListener (this); (4)bm. setActionConnnand ("second"); (5)]. setText (str); [解析] 本题主要考查Java语言中高级事件ActionEvent和AWT基本构件Label的常用方法的使用。解题关键是熟练掌握动作事件Ac-tionEvent和Label构件的常用方法。在本题中,明确注册的事件监听器是监听按钮的,而不是Label的;调用ActionEvent的setActionConnnand ()方法改变了ActionCommand,使按下第二个按钮时显示Command:second而不是Command: First。调用Label的setText()方法,而不是 Button的方法。