问答题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ex25_2 {
public static void main(String[] arg) {
UrlFrame page = new UrlFrame("http://www.sina.com.cn");
page.show();
}
}
class UrlFrame extends JFrame {
JTextArea jta = new JmextArea("正在读取文件…");
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());
}
}
}