寻题宝
当前位置:计算机-计算机二级

[问答题]下面是一个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


ex28_3





相关题目

关于我们 | 网站声明 | 联系我们 | 违法和不良信息举报

Copyright © 2020 - 2022  石家庄晋咸软件科技有限公司   版权所有   冀ICP备2021011520号-1    冀公网安备 13012102000087号