[color=#0033ff,strength=3);]package com;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
[color=#0033ff,strength=3);]public class MainForm extends JFrame {
[color=#0033ff,strength=3);] private JButton start = new JButton("开始");
[color=#0033ff,strength=3);] private JTextField tx = new JTextField(20);
[color=#0033ff,strength=3);] private JTextArea ts = new JTextArea(30, 70);
[color=#0033ff,strength=3);] JPanel top = new JPanel(), bot = new JPanel(),main=new JPanel();
Container con = null;
public MainForm(String title) {
super(title);
con = getContentPane();
top.setLayout(new FlowLayout());
top.add(new JLabel("请输入网址"));
top.add(tx);
top.add(start);
ts.setLineWrap(true);
ts.setForeground(Color.black);
ts.setAutoscrolls(true);
bot.add(new JScrollPane(ts));
main.add(top, BorderLayout.NORTH);
main.add(bot, BorderLayout.CENTER);
con.add(main);
setSize(800, 700);
//pack();
setVisible(true);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if(tx.getText()==""||tx.getText().length()<=0)
{
JOptionPane.showMessageDialog(null, "请输入网址");
return ;
}
ts.setText("");
UrlUtil ut=new UrlUtil(tx.getText(),ts);
}
});
[color=#0033ff,strength=3);] }
public static void main(String args[]) {
MainForm main = new MainForm("网络抓取器");
}
[color=#0033ff,strength=3);]}
class UrlUtil extends Thread{
private JTextArea ts;
private String ul;
public UrlUtil(String ul,JTextArea ts)
{
this.ul=ul;
this.ts=ts;
Thread thread=new Thread(this);
thread.start();
}
InputStream is=null;
BufferedReader reader=null;
StringBuffer buf=new StringBuffer();
public void getContents(String ul,JTextArea ts) {
}
public void run()
{
String line="";
int count=0;
try {
URL url = new URL(ul);
URLConnection con = url.openConnection();
con.connect();
is=con.getInputStream();
reader=new BufferedReader(new InputStreamReader(is,"gbk"));
while((line=reader.readLine())!=null)
{
//buf.append(line);
//buf.append("\n");
ts.append(line);
//ts.append("\n");
Thread.sleep(200);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
// ts.setText(buf.toString());
}
} |