简体   繁体   English

JTextArea不显示

[英]JTextArea does not show up

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. 该代码假定使用该程序的输出显示JTextArea This program render html page and extract their contents. 该程序呈现html页面并提取其内容。 Its wierd since i can run it and display the result in the console, however, i could not display it in Jtext Area . 因为我可以运行它并在控制台中显示结果,所以它很奇怪,但是我无法在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. 但是,执行后,不会出现JTextArea

Set it visible _resultArea.setVisible(true); 将其设置为可见的_resultArea.setVisible(true);

And it looks like you dont actually add the GUI components until after your process your data. 看起来您实际上没有添加GUI组件,直到处理完数据为止。 Not sure if that is what you want or not. 不确定这是否是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM