问答题

下面是一个Applet程序,其功能是实现对JButton类的扩展,封装成上网助力一样的按钮类,每个按钮对象对应一个 URL地址,点击则访问相应的URL。要求在窗口中从上到下排列3个这样的按钮。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 程序运行结果如下:
import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ex28_3 extends JApplet implements ActionListener { ButtonURL[] btnURL = new ButtonURL[3]; public void init() { btnURL[0] = new ButtonURL("新浪网","http://www.sina.com.cn/"); btnURL[1] = new ButtonURL("163","http://www.163.com/"); btnURL[2] = new ButtonURL("搜狐","http://www.sohu.com/"); GridLayout gl = new GridLayout(1,3); getContentPane().setLayout(gl); for (int i = 0; i < btnURL.length; i++) { btnURL[i].addActionListener(this); getContentPane().add(btnURL[i]); } } public void actionPerformed(ActionEvent ae) { ButtonURL btnClick =ae.getSource();//获取发生事件的对象 try { URL load = new URL(btnClick.strAddr); getAppletContext().showDocument(strAddr); } catch (MalformedURLException e) { showStatus("Bad URL:" + btnClick.strAddr); } } } class ButtonURL extends JButton { String strAddr; ButtonURL(String strLabel, String strAddress) { super(strLabel); strAddr = strAddress; } } ex28_3.html <HTML> <HEAD> <TITLE>ex28_3</TITLE> </HEAD> <BODY> <applet code="ex28_3.class" width=800 height=400 > </applet> </BODY> </HTML>

【参考答案】

new GridLayout(3,1) (ButtonURL)ae.getSource() showDocument(l......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)