问题 问答题

请完成下列Java程序:读取新浪首页文件的数据并且显示出来。要求编写JFrame扩展类,以String类的对象定义的url地址作为入口参数,该类实现根据url参数指定的地址进行连接和读取数据,并且能显示在一个文本区域内。
注童;请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ex15_2

public static void main(String args[])

UrlFrame page = new UrlFrame("http://www.sina.com.cn");
page.show();


class UrlFrame extends JFrame

JTextArea jta = new JTextArea("正在读取文件...");
URL url;
public UrlFrame(String strAddr)

super (strAddr);//使用父类的构造方法。
setSize(450, 300);
JScrollPane jsp = new JScrollPane(jta);
getContentPane().add(jsp);
WindowListener wl = new WindowAdapter()

public void windowClosing(WindowEvent we)

System.exit(0);

;
addWindowListener(wl);
try
url = new URL(strAddr);
______;
catch (MalformedURLException murle)
System.out.println("不合适的URL:"+ strAddr);


void getData(URL url)

URLConnection con = null;
InputStreamReader isr;
BufferedReader readBuf;
String strLine;
StringBuffer strBuf = new StringBuffer();
try
con = this.url.openConnection();
con.connect();
jta.setText("打开连接...");
isr = new InputStreamReader(con.getInputStream());
readBuf = new BufferedReader(isr);
jta.setText("读取数据...");
while ((strLine = readBuf.readLine()) != null)
strBuf.append(strLine + "\n");
______;
catch (IOException e)
System.out.println("IO错误:" + e.getMessage());


答案

参考答案:getData(url)
jta.setText(strBuf.toString())

解析: 本题主要考查面向对象的基本编程思想,Swing构件的使用,以及java.net包中的基本方法的使用。解题关键是能够从题目意思中提取有用信息,并抽象成自己制作的类,并实现这个类,完成一些基本功能。本题中,第1个空,调用类UrlFrame内部的方法getData,根据参数url所指出的地址获得文件数据:第2个空,将strBuf中的数据显示到jta对象所指定的文本区域中。

计算题
单项选择题