有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从控制台读取URL并在GUI的文本区域中打印

我正在从事“使用JavaRMI的分布式网络爬虫”项目。 网络会抓取页面,并在两个控制台上显示URL——一个是客户端控制台,另一个是服务器控制台。 我的问题是,我必须从控制台读取这些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) 个答案

  1. # 1 楼答案

    一个问题是,您正在actionPerformed方法内的AWT线程(“事件调度线程”)上的一个循环中完成所有工作,并在该循环中更新GUI

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

    见:

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

    t2.setText("" + hp_tmp);