JTextArea 不显示

发布于 2024-10-27 19:58:18 字数 4927 浏览 2 评论 0原文

import net.htmlparser.jericho.*;
import java.util.*;
import java.awt.BorderLayout;
import java.io.*;
import java.net.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class RenderToText extends JFrame {


    static JTextArea _resultArea = new JTextArea(100, 100);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";

    public RenderToText(){
        _resultArea.setEditable(false);
        //Starting to write files
        try{
        FileReader fr = new FileReader(
                "C:\\Users\\user\\fypworkspace\\FYP\\abc.txt");
        BufferedReader textReader = new BufferedReader(fr);

        // for each URL, process the URL and render the HTML file
        int numberofURL = 11;
        String[] URL = new String[numberofURL];
        int a;




        // For each URL, assign one text file to store the contents

        // for each URL, extract the URL contents

        for (a = 0; a < numberofURL; a++) {
            for (int i = 0; i < numberofURL; i++) {

                URL[a] = textReader.readLine();
                try{
                try {
                    try {
                        // Render the text from the HTML file
                        String sourceUrlString = URL[a];


                        System.out.println("Using argument of \""
                                    + sourceUrlString + '"');


                        if (sourceUrlString.indexOf(':') == -1)
                            sourceUrlString = "file:" + sourceUrlString;
                        Source source = new Source(new URL(sourceUrlString));
                        String renderedText = source.getRenderer()
                                .toString();
                        _resultArea.append("\nSimple rendering of the HTML document:\n" + newline);
                        _resultArea.append(renderedText+ newline);

                        // Write the rendered text to a text file

                        String filename = ("abc" + i + ".txt");
                        Writer output = null;
                        String text = renderedText;
                        File file = new File(filename);
                        output = new BufferedWriter(new FileWriter(file));
                        output.write(text);
                        output.close();
                        _resultArea.append("Your file has been written"+ newline);

                        // Count the number of words available in the
                        // rendered text.

                        BufferedReader br = new BufferedReader(
                                new FileReader(
                                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                                + i + ".txt"));
                        String line = "", str = "";
                        int count = 0;
                        while ((line = br.readLine()) != null) {
                            str += line + " ";

                        }
                        StringTokenizer st = new StringTokenizer(str);
                        while (st.hasMoreTokens()) {
                            @SuppressWarnings("unused")
                            String s = st.nextToken();
                            count++;
                        }
                        _resultArea.append("File has " + count + " words."+ newline);
                    } catch (UnknownServiceException ex) {
                        System.out.println("The following url cannot be processed"+ newline);

                    }

                    System.out.println("\n");
                    System.out.println("\n");
                    System.out.println("\n");
                } catch (NullPointerException ex) {
                    System.out.println("End of URL");
                    System.exit(0);
                }
            }catch(IOException ex){
                System.out.println("The following url cannot be processed due to the need to login");
            }
            }
        }


    }catch (IOException e1) {
    }
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    content.add(scrollingArea, BorderLayout.CENTER);

    this.setContentPane(content);
    this.setTitle("TextAreaDemo B");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    }



    public static void main(String[] args) throws IOException {
        JFrame win = new RenderToText();
        win.setVisible(true);
    }
    }

此代码假设显示一个带有该程序输出的 JTextArea。该程序渲染 html 页面并提取其内容。它很奇怪,因为我可以运行它并在控制台中显示结果,但是,我无法在 Jtext Area 中显示它。我哪里失踪了?

要打印的文件假设是这行代码:

   _resultArea.append(renderedText+ newline);

但是,执行后,JTextArea 不会出现。

import net.htmlparser.jericho.*;
import java.util.*;
import java.awt.BorderLayout;
import java.io.*;
import java.net.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class RenderToText extends JFrame {


    static JTextArea _resultArea = new JTextArea(100, 100);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";

    public RenderToText(){
        _resultArea.setEditable(false);
        //Starting to write files
        try{
        FileReader fr = new FileReader(
                "C:\\Users\\user\\fypworkspace\\FYP\\abc.txt");
        BufferedReader textReader = new BufferedReader(fr);

        // for each URL, process the URL and render the HTML file
        int numberofURL = 11;
        String[] URL = new String[numberofURL];
        int a;




        // For each URL, assign one text file to store the contents

        // for each URL, extract the URL contents

        for (a = 0; a < numberofURL; a++) {
            for (int i = 0; i < numberofURL; i++) {

                URL[a] = textReader.readLine();
                try{
                try {
                    try {
                        // Render the text from the HTML file
                        String sourceUrlString = URL[a];


                        System.out.println("Using argument of \""
                                    + sourceUrlString + '"');


                        if (sourceUrlString.indexOf(':') == -1)
                            sourceUrlString = "file:" + sourceUrlString;
                        Source source = new Source(new URL(sourceUrlString));
                        String renderedText = source.getRenderer()
                                .toString();
                        _resultArea.append("\nSimple rendering of the HTML document:\n" + newline);
                        _resultArea.append(renderedText+ newline);

                        // Write the rendered text to a text file

                        String filename = ("abc" + i + ".txt");
                        Writer output = null;
                        String text = renderedText;
                        File file = new File(filename);
                        output = new BufferedWriter(new FileWriter(file));
                        output.write(text);
                        output.close();
                        _resultArea.append("Your file has been written"+ newline);

                        // Count the number of words available in the
                        // rendered text.

                        BufferedReader br = new BufferedReader(
                                new FileReader(
                                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                                + i + ".txt"));
                        String line = "", str = "";
                        int count = 0;
                        while ((line = br.readLine()) != null) {
                            str += line + " ";

                        }
                        StringTokenizer st = new StringTokenizer(str);
                        while (st.hasMoreTokens()) {
                            @SuppressWarnings("unused")
                            String s = st.nextToken();
                            count++;
                        }
                        _resultArea.append("File has " + count + " words."+ newline);
                    } catch (UnknownServiceException ex) {
                        System.out.println("The following url cannot be processed"+ newline);

                    }

                    System.out.println("\n");
                    System.out.println("\n");
                    System.out.println("\n");
                } catch (NullPointerException ex) {
                    System.out.println("End of URL");
                    System.exit(0);
                }
            }catch(IOException ex){
                System.out.println("The following url cannot be processed due to the need to login");
            }
            }
        }


    }catch (IOException e1) {
    }
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    content.add(scrollingArea, BorderLayout.CENTER);

    this.setContentPane(content);
    this.setTitle("TextAreaDemo B");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    }



    public static void main(String[] args) throws IOException {
        JFrame win = new RenderToText();
        win.setVisible(true);
    }
    }

This code suppose to display a JTextArea with the outputs of this program. This program render html page and extract their contents. Its wierd since i can run it and display the result in the console, however, i could not display it in Jtext Area. Where do i missing?

The file to be printed suppose to be this line of code :

   _resultArea.append(renderedText+ newline);

However, upon execution, the JTextArea does not appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-11-03 19:58:18

将其设置为可见 _resultArea.setVisible(true);

看起来您在处理数据之前实际上并未添加 GUI 组件。不确定这是否是您想要的。

Set it visible _resultArea.setVisible(true);

And it looks like you dont actually add the GUI components until after your process your data. Not sure if that is what you want or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文