2016-11-23 92 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