2015-10-06 44 views
0

我嘗試做:填充一個HashSet不工作的第一次 - 僅在第二個電話

我想一個HashSet來填充,該程序不知道新詞。 用戶按mainFrame上的「convert」按鈕。 mainFrame上給出了帶有單詞的文件的路徑。

如果這個單詞是新的,打開一個JDialog並要求插入新單詞(這樣您可以更改拼寫,例如第一個字母大)。

如果用戶按下JDialog上的「寫入」按鈕,該單詞將添加到HashSet中。

但是,如果我打印後我的HashSet,只有「舊」值顯示。 當我第二次按下mainFrame上的「convert」按鈕時,所有的值將在HashSet中正確顯示。

如果有人能幫助我,我將非常感激。 如果需要更多信息,請告訴我。 下面是從的ActionListener代碼當按鈕「轉換」按下:

 if (e.getActionCommand().equals(convert.getActionCommand())) { 
     try { 
      //a file with words is given 
      fileHandler = new FileIO(path.getText().trim()); 

      //lines is a ArrayList<String> and returns all the lines 
      lines = fileHandler.readFile(); 

      for (int i = 0; i < lines.size(); i++) { 
       //words is a String[] 
       words = lines.get(i).split(" "); 
       for (int j = 0; j < words.length; j++) { 

        //hs is a HashSet<String> 
        if (hs.contains(words[j])) { 
         System.out.println("hit: " + words[j]); 
        }else if (!hs.contains(words[j])) { 
         dialog = new JDialog(mainFrame); 
         dialog.setTitle("new Word"); 
         dialog.setLayout(new BorderLayout()); 

         newWord = new JTextField(words[j].toLowerCase()); 
         newWord.selectAll(); 

         write = new JButton("write"); 

         write.addActionListener(new ActionListener() { 

          public void actionPerformed(ActionEvent e) { 
           //can not use the counters "i" or "j" here otherwise it would be so easy... 
           s = newWord.getText().trim(); 
           dialog.setVisible(false); 
           if(dialog != null) 
            dialog.dispose(); 
           if (dialog.isActive()) { 
            System.out.println("active"); 
           }else { 
            //dead code ??? -- never executed so far 
            System.out.println("finally....done"); 
           } 
          } 
         }); 

         dialog.add(newWord, BorderLayout.NORTH); 
         dialog.add(write, BorderLayout.SOUTH); 
         dialog.pack(); 
         dialog.setVisible(true); 

         //todo is filled correctly IF pressed "convert Button" the second time 
         if (!s.contentEquals("")) { 
          words[j] = s; 
          hs.add(s); 
          s = ""; 
         } 

        } 
       } // words 


       //Displays the input line but why not the manipulated from the JDialog input? 
       StringBuffer sb = new StringBuffer(); 
       for (String string : words) { 
        sb.append(string); 
        sb.append(" "); 
       } 
       System.out.println(sb.toString()); 
       lines.set(i, sb.toString()); 
       sb.delete(0, sb.length()); 

      } // lines 

代碼寫入(在文件中),當我按下主機上的「退出」按鈕,將顯示我的HashSet:

hashSetHandler = new HashSetIO(WORDS_FILE); 
    hashSetHandler.write(hs); 
    for (String string : hs) { 
     System.out.println(string); 
+0

's'是如何聲明的?它是一個封閉類的成員變量? – erickson

+0

它是在同一類中聲明的變量,如: 公共類MainFrame { \t HashSet hs; 該類有一個名爲「mainFrame」的Jframe,並且在該Jframe的Actionlistener(也在同一類中聲明)中有我顯示的代碼 – Xavinlol

+0

您說這個單詞沒有添加到'HashSet',但是您的代碼打印了什麼是'行',而不是'hs'。這是什麼?當你第二次按下「轉換」按鈕時,它是否會提示你添加相同的單詞,還是不會彈出任何對話框? – erickson

回答

0

您應該考慮製作dialog模式。

當您按convert時,目前您看到許多彈出式窗口(每個新文件對應一個文件),不是嗎?

模式和「正常」之間的區別dialog將在這裏會影響您的代碼:

dialog.setVisible(true); 
//todo is filled correctly IF pressed "convert Button" the second time 
if (!s.contentEquals("")) { 
    words[j] = s; 
    hs.add(s); 
    s = ""; 
} 

直到write按鈕被按下一個模式dialog將在dialog.setVisible(true)線阻斷,你的ActionListener將處理該事件,設置s = newWord.getText().trim();並處理dialog

此事件(write按下)後,才進行處理的代碼之後的dialog.setVisible(true)的線將被執行,並且s將包含newWord.getText().trim()的值的。

用「正常」(非模態)dialog你是不是在dialog.setVisible(true)堵因此

if (!s.contentEquals("")) 

線將檢查的是尚未設定s一定的價值(你有沒有按下write按鈕尚)測試將失敗,並且if塊中的代碼將不會執行。

我會建議您調試代碼在if行上設置斷點並在write按鈕的ActionListener中設置斷點。然後,當代碼執行時以及字段的值是什麼時,你會更好地理解。

+0

嘿,非常感謝,解決了所有問題:)現在,HashSet在我第一次按下「convert」並插入單詞時被正確填充。 是的,它確實爲沒有模態標誌的每個未知單詞打開一個對話框。 我不明白爲什麼,但我會找到答案。 'dialog = new JDialog(mainFrame,JDialog.ModalityType.DOCUMENT_MODAL);' – Xavinlol

相關問題