日新月异
5
公司 :杭州神话信息技术有限公司
部门 :运营中心设计部
岗位 :UI视觉设计
8
关注
15
粉丝
7030
微博
56
被赞
新浪微博
原创达人
友情链接:
找感兴趣的人
精彩内容
热门应用
关于我们
手机玩微博
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
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;
public void init() {
btnURL = new ButtonURL("新浪网","http://www.sina.com.cn/");
btnURL = new ButtonURL("163","http://www.163.com/");
btnURL = new ButtonURL("搜狐","http://www.sohu.com/");
GridLayout gl = new GridLayout(1,3);
getContentPane().setLayout(gl);
for (int i = 0; i < btnURL.length; i++) {
btnURL.addActionListener(this);
getContentPane().add(btnURL);
}
}
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(31) (ButtonURL)ae.getSource() showDocument(load)new GridLayout(3,1) (ButtonURL)ae.getSource() showDocument(load) 解析:本题主要考查面向对象的基本程序设计思想和Applet事件处理机制及Applet和Application相结合的程序设计。解题关键是熟悉swing的基本构件,并会根据需要进行扩展,这里的ButtonURL类就是对JButton类的扩展