2014-01-09 47 views
2

我寫了一個代碼,以查找並在JTextArea突出一個字,我打一個問題,我太累了,有頭痛,看看我的錯誤。 我有一個搜索欄(TextField),在那裏我可以輸入一個單詞,並且該單詞在我的TextArea中獲得熒光筆。問題是,當我按我的「ENTER」鍵,TextField被取消,我必須再次點擊它來尋找下一個單詞。我錯過了什麼?JTextField。找到並突出顯示這個詞在JTextArea

findfieldpage1 = new JTextField(); 
findfieldpage1.setBounds(37, 295, 63, 24); 
gtapage1.add(findfieldpage1); 

findfieldpage1.addKeyListener(new KeyAdapter() { 
    public void keyPressed(KeyEvent evt) { 
     int code = evt.getKeyCode(); 
     if(code == KeyEvent.VK_ENTER){ 
      String find = findfieldpage1.getText().toLowerCase(); 
      textpage1.requestFocusInWindow(); 
      if (find != null && find.length() > 0) { 
       Document document = textpage1.getDocument(); 
       int findLength = find.length(); 
       try { 
        boolean found = false; 
        if (pos + findLength > document.getLength()) { 
         pos = 0; 
        } 
       while (pos + findLength <= document.getLength()) { 
        String match = document.getText(pos, findLength).toLowerCase(); 
        if (match.equals(find)) { 
         found = true; 
         break; 
        } 
        pos++; 
       } 
       if (found) { 
        Rectangle viewRect = textpage1.modelToView(pos); 
        textpage1.scrollRectToVisible(viewRect); 
        textpage1.setCaretPosition(pos + findLength); 
        textpage1.moveCaretPosition(pos); 
        pos += findLength; 
       } 
      } catch (Exception exp) { 
       exp.printStackTrace(); 
      } 
     } 
     } 
    } 
}); 
+1

如果你需要幫助,你必須提供一個[SSCCE(http://sscce.org)演示該問題。如果沒有這些,我們可以給你的最好建議是休息一下,然後用新鮮的眼睛看看。 –

+1

通過取消選中你的意思是失去了焦點,或者裏面的文字沒有被選中? – rhobincu

+0

@KevinWorkman我會按照你的意見,睡一覺。 – MrSilent

回答

2

搜索完成後

在末尾加上我想你不到轉化中的焦點回到文本字段:Jtextfield.requestfocus()

2

10在你的方法是textpage1.requestFocusInWindow();線,這就是爲什麼它失去了重點,因爲你將它轉移到JTextArea。

0

最後,以避免麻煩。我添加了一個JButton並將keyPressed更改爲一個actionPerformed。所以每次點擊該按鈕時,它都會查找並突出顯示我在TextField中輸入的字符串。謝謝你們的幫助,我很感激。

+2

您可以添加一個'ActionListener'到'JTextField'以及任何改變,只是讓你知道:) – rhobincu

+0

是的,我知道。 :) – MrSilent

0

再添加一個監聽到文本區域。焦點轉移到文本區域後,這種方式將保留在textarea中,並且事件(輸入按鍵)等將使搜索發生。

/搜索申請開始/

JTextField findtext = new JTextField(); 
    //findtext.setBounds(112,549, 63, 24); 
    frame.add(findtext,BorderLayout.BEFORE_FIRST_LINE); 

    findtext.addKeyListener(new KeyAdapter() { 
     public void keyPressed(KeyEvent evt) { 
      int code = evt.getKeyCode(); 

      if(code == KeyEvent.VK_ENTER){ 
       String find = findtext.getText().toLowerCase(); 
       tarea.requestFocusInWindow(); 
       //findtext.requestFocus(); 
       //findtext.requestFocusInWindow(); 
       if (find != null && find.length()> 0) { 
        Document document = tarea.getDocument(); 
        int findLength = find.length(); 
        try { 
         //int pos=0; 
         boolean found = false; 
         if (pos + findLength > document.getLength()) { 
          pos = 0; 
         } 
        while (pos + findLength <= document.getLength()) { 
         String match = document.getText(pos, findLength).toLowerCase(); 
         if (match.equals(find)) { 
          found = true; 
          break; 
         } 
         pos++; 
        } 
        if (found) { 
         Rectangle viewRect = tarea.modelToView(pos); 
         tarea.scrollRectToVisible(viewRect); 
         tarea.setCaretPosition(pos + findLength); 
         tarea.moveCaretPosition(pos); 
         pos += findLength; 
         //Thread.sleep(2000); 
         // findtext.requestFocusInWindow(); 

        } 

       } catch (Exception exp) { 
        exp.printStackTrace(); 
       } 
      } 

       }//findtext.requestFocusInWindow(); 
     } 
    }); 

    /*control back to textarea*/ 
    tarea.addKeyListener(new KeyAdapter() { 
     public void keyPressed(KeyEvent evt) { 
      int code = evt.getKeyCode(); 

      if(code == KeyEvent.VK_ENTER){ 
       String find = findtext.getText().toLowerCase(); 
       tarea.requestFocusInWindow(); 
       //findtext.requestFocus(); 
       //findtext.requestFocusInWindow(); 
       if (find != null && find.length()> 0) { 
        Document document = tarea.getDocument(); 
        int findLength = find.length(); 
        try { 
         //int pos=0; 
         boolean found = false; 
         if (pos + findLength > document.getLength()) { 
          pos = 0; 
         } 
        while (pos + findLength <= document.getLength()) { 
         String match = document.getText(pos, findLength).toLowerCase(); 
         if (match.equals(find)) { 
          found = true; 
          break; 
         } 
         pos++; 
        } 
        if (found) { 
         Rectangle viewRect = tarea.modelToView(pos); 
         tarea.scrollRectToVisible(viewRect); 
         tarea.setCaretPosition(pos + findLength); 
         tarea.moveCaretPosition(pos); 
         pos += findLength; 
         //Thread.sleep(2000); 
         // findtext.requestFocusInWindow(); 

        } 

       } catch (Exception exp) { 
        exp.printStackTrace(); 
       } 
      } 

       }//findtext.requestFocusInWindow(); 
     } 
    }); 


    /*search filed ends*/