从控制台读取URL并在GUI中以textarea打印

时间:2015-04-21 16:01:03

标签: java swing

我正在使用Java RMI"开发项目"分布式Web爬虫。 Web抓取页面并在两个控制台上显示URL - 客户端1和服务器1。 我的问题是我必须从控制台读取这些URL并将其显示在窗口的文本区域下。我已经尝试了很多,但URL没有显示在文本区域中。 但是它们会在控制台上显示出来。

我的代码是:

import java.net.*;
import java.io.*;
import java.util.Date;
import java.io.File;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.Channels;
import java.rmi.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class UCDemo implements ActionListener {
    static String dispServerURL;
    JFrame f1;
    JPanel p1;
    JLabel l1;
    JLabel l2;
    JTextField t1;
    JTextArea t2;
    JButton b1;

    public void showFrame() {
        f1 = new JFrame("Web Crawler");
        p1 = new JPanel();
        f1.setSize(7000, 7000);
        f1.setVisible(true);
        f1.setBackground(Color.pink);
        f1.getContentPane().add(p1);
        l1 = new JLabel("Enter seed URL");
        t1 = new JTextField(100);
        b1 = new JButton("Start");
        l2 = new JLabel("Result");
        t2 = new JTextArea(200, 200);

        p1.add(l1);
        p1.add(t1);
        p1.add(b1);
        p1.add(l2);
        p1.add(t2);

        b1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        try {
            DispServerIntf dispServerIntf = (DispServerIntf) Naming.lookup(dispServerURL);
            if (ae.getSource() == b1) {
                String n1, k;
                InputStreamReader ir = new InputStreamReader(System.in);
                n1 = t1.getText();
                int c = 0;
                Document document = null;
                URL hp = new URL(n1);
                URLConnection hpcon = hp.openConnection();

                try {
                    document = Jsoup.parse(hp, 3000);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                for (Element element : document.getElementsByTag("a")) {
                    c++;
                    ReadableByteChannel rbc = Channels.newChannel(hp.openStream());
                    FileOutputStream fos = new FileOutputStream("te.html");
                    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                    if (c <= 7) {
                        Document doc_tmp = null;
                        URL hp_tmp = new URL(element.attr("href"));
                        t2.setText("" + hp_tmp);
                        URLConnection hpcontmp = hp_tmp.openConnection();
                        /**
                         * try { doc_tmp=Jsoup.parse(hp_tmp,3000); }
                         * catch(IOException e) {e.printStackTrace(); } for
                         * (Element ele: doc_tmp.getElementsByTag("a")) {
                         * System.out.println(ele.attr("href")); }
                         **/
                        ReadableByteChannel rbc_tmp = Channels.newChannel(hp_tmp.openStream());
                        FileOutputStream fos_tmp = new FileOutputStream("te" + c + ".html");
                        fos_tmp.getChannel().transferFrom(rbc_tmp, 0, Long.MAX_VALUE);

                    } else {
                        dispServerIntf.send(element.attr("href"));
                    }
                }
                /**
                 * BufferedReader in=new BufferedReader(new
                 * InputStreamReader(hpcon.getInputStream())); String inputline;
                 * while((inputline=in.readLine())!=null)
                 * System.out.println(inputline); in.close();
                 **/
            }
        } catch (Exception e) {
        }
    }

    public static void main(String args[]) {
        dispServerURL = "rmi://" + args[0] + "/DispServer";

        new UCDemo().showFrame();
    }
}

1 个答案:

答案 0 :(得分:0)

一个问题是你在actionPerformed方法中的AWT线程(&#34;事件调度线程&#34;)中循环完成所有工作,并在该循环中更新GUI。

这意味着GUI将无法正确更新(因为更新GUI的线程忙于执行HTML解析工作,并且无法自由处理GUI事件)。您可能需要在单独的线程中执行主循环,然后使用invokeLater()更新AWT线程中的GUI。

请参阅:

此外,您始终将文本设置为单个值,因此您只能看到最新的URL,而不是它们的列表:

t2.setText("" + hp_tmp);
相关问题