import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class URLDemo
{
public static void main(String[] args)
{
new URLDemo();
}
private Frame f;
private TextField tf;
private TextArea ta;
private Button but;
URLDemo()
{
init();
}
public void init()
{
f=new Frame("MyFrame");
f.setBounds(300,250,600,500);
f.setLayout(new FlowLayout());
tf=new TextField(63);
ta=new TextArea(35,70);
but = new Button("转到");
f.add(tf);
f.add(but);
f.add(ta);
MyEvent();
f.setVisible(true);
}
public void MyEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
showDir();
}
catch (IOException ex)
{
throw new RuntimeException("操作失败");
}
}
});
}
public void showDir() throws IOException,MalformedURLException
{
ta.setText("");
String urlPath=tf.getText();
URL url = new URL(urlPath);
URLConnection conn=url.openConnection();
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len=in.read(buf);
ta.setText(new String(buf,0,len));
}
}
【java编程】TCP_Socket网络编程之用URL类创建浏览器
最新推荐文章于 2021-08-16 08:25:00 发布