2011-04-03 58 views
0
import org.jsoup.Jsoup; 


@SuppressWarnings({ "unused", "serial" }) 

public class SimpleWebCrawler extends JFrame { 

    JTextField yourInputField = new JTextField(20); 
    static JTextArea _resultArea = new JTextArea(200, 200); 
    JScrollPane scrollingArea = new JScrollPane(_resultArea); 
    private final static String newline = "\n"; 
    String word2; 


    public SimpleWebCrawler() throws MalformedURLException { 

     yourInputField.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
      word2 = yourInputField.getText(); 
     } 
     }); 


     _resultArea.setEditable(false); 


     try { 
      URL my_url = new URL("http://" + word2 + "/"); 
      BufferedReader br = new BufferedReader(new InputStreamReader(
        my_url.openStream())); 
      String strTemp = ""; 
      while (null != (strTemp = br.readLine())) { 
       _resultArea.append(strTemp + newline); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     _resultArea.append("\n"); 
     _resultArea.append("\n"); 
     _resultArea.append("\n"); 


     String url = "http://" + word2 + "/"; 
     print("Fetching %s...", url); 

     try{ 
     Document doc = Jsoup.connect(url).get(); 
     Elements links = doc.select("a[href]"); 


     System.out.println("\n"); 

     BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt")); 
     _resultArea.append("\n"); 
     for (Element link : links) { 
      print(" %s ", link.attr("abs:href"), trim(link.text(), 35)); 

      bw.write(link.attr("abs:href")); 
      bw.write(System.getProperty("line.separator")); 
     } 
     bw.flush(); 
     bw.close(); 
     } catch (IOException e1) { 

     } 
     JPanel content = new JPanel(); 

     content.setLayout(new BorderLayout()); 
     content.add(scrollingArea, BorderLayout.CENTER); 
     content.add(yourInputField,BorderLayout.SOUTH); 


     this.setContentPane(content); 
     this.setTitle("Crawled Links"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     JPanel content2 = new JPanel(); 
     this.setContentPane(content2); 
     this.setTitle("Input the URL"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 


     } 

     private static void print(String msg, Object... args) { 

      _resultArea.append(String.format(msg, args) +newline); 
     } 

     private static String trim(String s, int width) { 
      if (s.length() > width) 
       return s.substring(0, width - 1) + "."; 
      else 
       return s; 
     } 

     //.. Get the content pane, set layout, add to center 




    public static void main(String[] args) throws IOException { 

     JFrame win = new SimpleWebCrawler(); 
     win.setVisible(true); 

    } 

} 

我想創建一個JTextField接受用戶輸入。輸入將轉到這行代碼來處理代碼。不能創建一個JTextField

URL my_url = new URL("http://" + word2 + "/"); 

     String url = "http://" + word2 + "/"; 

然而,代碼,而不要求用戶進行輸入運行。 JTextField沒有出現,我直接得到一個錯誤,因爲我輸入了輸入。

我想讓JTextField接受來自用戶的輸入。然而,它並沒有出現,並且代碼直接進行處理,最終得到了空的my_url和rmpty url變量。

如何根據我發佈的代碼創建JTextField?看來我創建的Jtextfield與我的代碼發生衝突。

+2

爲了更快得到更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)(並非所有人都使用自動插入導入的IDE!)。 – 2011-04-03 07:23:09

回答

2

Java swing不遵循命令式的方法,而是事件驅動。您的構造函數方法總共執行並且不等待您的輸入。

您不得在此方法中包含業務邏輯(即所有這些讀/寫內容),而是將其分配到單獨的一箇中,並從您在輸入字段中註冊的操作偵聽器中調用它。 (請參閱(例如)http://download.oracle.com/javase/tutorial/uiswing/events/index.html

注意:如果您的邏輯非常重,則應該產生後臺線程,而不是直接在動作偵聽器中執行此操作(請參閱Swingworker)。

注意B:班上有很多奇怪的代碼。

this.setContentPane(content); 
this.setTitle("Crawled Links"); 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
this.pack(); 
JPanel content2 = new JPanel(); 
this.setContentPane(content2); 
this.setTitle("Input the URL"); 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
this.pack(); 

正如之前提到的,這將立即運行,因此您的面板content永遠不會顯示在所有的,因爲它會通過content2被覆蓋。