2011-03-28 10 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 만 표시한다고 가정합니다. 하 워버, 아무것도 나왔다. – jasper

답변

1

설정하여 볼 수 _resultArea.setVisible(true);

그리고 실제로 데이터 프로세스 후까지 GUI 구성 요소를 추가 해달라고 것 같습니다. 그것이 당신이 원하는 것인지 아닌지 확실하지 않습니다.