2011-03-28 5 views
2
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ページをレンダリングし、その内容を抽出します。私はそれを実行してコンソールに結果を表示することができて以来、そのwierdしかし、私はそれをJtext Areaに表示することができませんでした。どこに行かないの?JTextAreaが表示されない

、印刷するファイルのコード行であることを仮定:

_resultArea.append(renderedText+ newline); 

しかし、実行時に、JTextAreaは表示されません。

+0

'String str =" ";'を 'StringBuilder sb = new StringBuilder();'にリファクタリングすることを検討してください。あなたはそれをループしており、そのファイルの大きさは分かりません。 – corsiKa

+0

GUIコンポーネントはありますか?あなたのメインフレーム? – MByD

+0

IDEコンソールから取得した結果を表示し、JTextAreaに表示しようとしています。 JTextAreaだけを表示すると仮定します。 Howerver、何も出てこなかった。 – jasper

答えて

1

設定して見える_resultArea.setVisible(true);

そして、あなたが実際にあなたのデータ、あなたのプロセスが終了するまでGUIコンポーネントを追加していけないように見えます。それがあなたの望むものかどうかは分かりません。

関連する問題