2016-11-23 90 views
3

我有一個名爲input的JTextArea,我試圖在按下向上箭頭鍵時將字符串inputValue加載到它中。到目前爲止,這段代碼似乎不起作用,我不確定原因。請幫忙。向JtextArea添加鍵監聽器

input.addKeyListener(new KeyListener() {    
     public void keyTyped(KeyEvent e) { 
      System.out.println("test"); 
      if(e.getKeyCode() == KeyEvent.VK_UP) { 
       input.setText(inputValue); 
       System.out.println("up is pressed"); 
      }  
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      // TODO Auto-generated method stub  
     } 
    }); 
+0

請勿在Swing文本組件中使用KeyListener,因爲它們會混淆組件的本機功能。有很多更好的方法來捕獲這些組件中的按鍵,包括使用DocumentListener,DocumentFilters和Key綁定。 –

回答

2

你應該覆蓋無效的keyPressed代替的keyTyped

@Override 
     public void keyPressed(KeyEvent e) { 
      System.out.println("test"); 
      if(e.getKeyCode() == KeyEvent.VK_UP) { 
       input.setText(inputValue); 
       System.out.println("up is pressed"); 

     } 

,因爲它不是一個卡拉科特

5

使用低級別的聽衆像Swing文本組件,如JTextAreas KeyListeners時,你要小心,因爲搞亂這些可能會導致文本組件行爲不當。

更好的方法是使用DocumentListener,如果您正在尋找文檔或DocumentFilter的更改,如果您想要偵聽並阻止或更改之前的文本條目它發生。

如果你只是想要得到諸如向上箭頭之類的關鍵字的通知,我會使用鍵綁定--JTextArea使用它自己來通知鍵響應並對其作出反應,並將鍵替換新的一個。如果您謹慎操作,您甚至可以在新操作中調用與按鍵相關的原始操作。例如:

import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class TextAreaTrapUp extends JPanel { 
    private JTextArea textArea = new JTextArea(20, 40); 

    public TextAreaTrapUp() { 
     // get JTextArea's InputMap and ActionMap 
     int condition = JComponent.WHEN_FOCUSED; 
     InputMap inputMap = textArea.getInputMap(condition); 
     ActionMap actionMap = textArea.getActionMap(); 

     // get the up keystroke 
     KeyStroke upKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0); 
     String upKey = (String) inputMap.get(upKeyStroke); // get the input map's key for this keystorke 
     Action originalUpAction = actionMap.get(upKey); // and get the action map's original action for this key 

     Action newUpAction = new NewUpAction(originalUpAction); // create our new up action passing in the old one 
     actionMap.put(upKey, newUpAction); // and set this into our ActionMap 

     textArea.setWrapStyleWord(true); 
     textArea.setLineWrap(true); 
     add(new JScrollPane(textArea)); 
    } 

    // Action called when up-arrow pressed 
    private class NewUpAction extends AbstractAction { 
     private Action originalUpAction; // the original action 

     public NewUpAction(Action originalUpAction) { 
      this.originalUpAction = originalUpAction; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Up Arrow Pressed"); 

      // if you want to move the caret up, then call the original action 
      // as well 
      if (originalUpAction != null) { 
       originalUpAction.actionPerformed(e); 
      } 
     } 
    } 

    private static void createAndShowGui() { 
     TextAreaTrapUp mainPanel = new TextAreaTrapUp(); 

     JFrame frame = new JFrame("TextAreaTrapUp"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 
+0

在swing組件上使用動作偵聽器時,我更早遇到過這個問題。我最終使用線程鎖定來排除故障,但我認爲這從長遠來看可能會更好。好吧。 – CNorlander

+0

@CNorlander:我不確定我是否瞭解你最後的評論。但是有一個問題:爲什麼你要在JTextArea中捕捉向上箭頭的新聞,因爲這是一個不尋常的要求?你是否想讓向上的箭頭也能正常工作 - 移動插入符號?還有一個小小的考慮:考慮對所有顯示努力嘗試幫助你的答案進行投票。 –

+0

文本區域用於在文本冒險遊戲中輸入,並且當不需要來自用戶的輸入時被禁用。當該字段未啓用時,我不希望用戶能夠使用此向上箭頭命令。向上箭頭鍵不需要執行其正常功能。 – CNorlander