2013-03-07 69 views
3

我有一個窗體用JButton編寫,我點擊完成一個動作。但是當我點擊它時,我的框架會掛起。我不能退出它,直到我使用任務欄結束它。問題是什麼?這是我的代碼:當我點擊按鈕時,爲什麼我的揮杆會掛起?

jButton1.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(java.awt.event.ActionEvent evt) { 
     jButton1ActionPerformed(evt); 
    } 
}); 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    String[] stopwords = {"a", "sometime", "sometimes", "somewhere", "still", 
      "such", "system", "take", "ten", "than", "that", "the", "their", 
      "them", "themselves", "then", "thence", "there", "thereafter", 
      "thereby", "therefore", "therein", "thereupon", "these", "they", 
      "thickv", "thin", "third", "this", "those", "though", "three", 
      "through", "throughout", "thru", "thus", "to", "together", "too", 
      "top", "toward", "towards", "twelve", "twenty", "two", "un", 
      "under", "until", "up", "upon", "us", "very", "via", "was", "we", 
      "well", "were", "what", "whatever", "when", "whence", "whenever", 
      "where", "whereafter", "whereas","whereby", "wherein", "whereupon", 
      "wherever", "whether", "which", "while","whither", "who", "whoever", 
      "whole", "whom", "whose", "why", "will", "with", "within", "without", 
      "would", "yet", "you", "your", "yours", "yourself", "yourselves", 
      "contact", "grounds", "buyers", "tried", "said,", "plan", "value", 
      "principle.", "forces", "sent:", "is,", "was", "like", "discussion", 
      "tmus", "diffrent.", "layout", "area.", "thanks", "thankyou", 
      "hello", "bye", "rise","fell","fall","psqft.","http://","km","miles"}; 
    try { 
     Scanner fip1 = new Scanner(new File(selectedFile)); 
     FileOutputStream out=new FileOutputStream("d:/StopWords.txt"); 
     while(fip1.hasNext()) { 
      int flag=1; 
      String s1=fip1.next(); 
      s1=s1.toLowerCase(); 
      for(int i=0;i<stopwords.length;i++){ 
       if(s1.equals(stopwords[i])) { 
        flag=0; 
       } 
      } 
      if(flag!=1) { 
      // System.out.println(s1); 
      // jTextArea1.append("\n"+s1); 
      PrintStream p=new PrintStream(out); 
      p.println(s1); 
      p.close(); 
     } 
    } 
     JOptionPane.showMessageDialog(null,"STOP WORD REMOVAL IS DONE");   
    } catch(Exception e){ 
     System.err.println("cannot read file"); 
    } 
}           
+1

'selectedFile'包含什麼?這可能只是一堆令牌,操作很長。 – 2013-03-07 14:45:49

+0

你的輸入文本文件有多大? – yaens 2013-03-07 14:46:17

+3

由於Swing是單線程的,因此當您使用長時間運行的操作阻塞線程時,它無法更新UI。 – Robin 2013-03-07 14:46:28

回答

1

這聽起來像一個循環,Java具有非常好的調試服務,通常內置於IDE中。你應該試試看看你是否可以追蹤發生了什麼。在你處理按鈕的地方停下來,然後按照這裏的代碼。

你也可以使用Hashmap作爲停用詞表,但那不是你的錯誤,只是一種改進。

編輯:as @robin指出Swing操作必須快速結束。

+0

幫助我舉一個例子來添加hashmap – 2013-03-07 15:04:35

+0

在線有幾十個HashMap例子,這裏是官方的Oracle Java教程:http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html – 2013-03-07 18:33:18

1

我認爲這是因爲掃描儀的使用。爲什麼不使用outputStream? 不管什麼問題 - 您都應該使用SwingWorker來執行髒IO工作,並保持UI的響應。

相關問題