Example:
   KeyStroke ks = KeyStroke.getKeyStroke("shift C");   textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
                          "CustomAction");
   textfield.getActionMap().put("CustomAction", new AbstractAction() {   public void actionPerformed(ActionEvent ae) {
         // whatever action
   }
});

解决方案 »

  1.   

    最新java核心技术卷一,有例子可以实现对所有键盘动作的监听(包括拷贝粘贴)
      

  2.   

    我找不到最新的java核心技术.烦劳将代码贴上.谢谢.
      

  3.   

    摇滚java兄给的代码没有注释,而且我还没查到相关资料,还请摇滚兄指点.
      

  4.   

    JTextField textfield = new JTextField();
       //加入textfield到界面
       ...
       //界面初始化的时候加入以下代码
       KeyStroke ks = KeyStroke.getKeyStroke("shift C");//你可以自己定义键盘按键,可以是ctrl c或者其他,查看jdk文档
       textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
                             "CustomAction");
       textfield.getActionMap().put("CustomAction", new AbstractAction() {
      
       public void actionPerformed(ActionEvent ae) {
         //这里调用你自己的方法
         copy();    
       }
    });...
    private void copy(){
      //执行代码
    }
    那么在当光标在textfield的时候
    你按下相应键盘就会执行copy方法
      

  5.   

    JTextField textfield = new JTextField();
       //加入textfield到界面
       ...
       //界面初始化的时候加入以下代码
       KeyStroke ks = KeyStroke.getKeyStroke("shift C");//你可以自己定义键盘按键,可以是ctrl c或者其他,查看jdk文档
       textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
                             "CustomAction");
       textfield.getActionMap().put("CustomAction", new AbstractAction() {
      
       public void actionPerformed(ActionEvent ae) {
         //这里调用你自己的方法
         copy();    
       }
    });...
    private void copy(){
      //执行代码
    }
    那么在当光标在textfield的时候
    你按下相应键盘就会执行copy方法
      

  6.   

    好长...
    IntTextField实现监听:
    class IntTextDocument extends PlainDocument
    {  
       public void insertString(int offs, String str, 
          AttributeSet a) 
          throws BadLocationException 
       {  
          if (str == null) return;      String oldString = getText(0, getLength());
          String newString = oldString.substring(0, offs)
             + str + oldString.substring(offs);      if (canBecomeValid(newString))
             super.insertString(offs, str, a);
       }   /**
          A helper function that tests whether a string is a valid
          integer
          @param s a string
          @return true if s is a valid integer
       */
       public static boolean isValid(String s)
       {
          try
          {  
             Integer.parseInt(s);
             return true;
          }
          catch(NumberFormatException e)
          {  
             return false; 
          }
       }   /**
          A helper function that tests whether a string is a 
          substring of a valid integer
          @param s a string
          @return true if s can be extended to a valid integer
       */
       public static boolean canBecomeValid(String s)
       {
          return s.equals("") || s.equals("-") || isValid(s);
       }
    }/**
       A text field for editing integer values
    */
    class IntTextField extends JTextField 

       /**
          Constructs an IntTextField.
          @param defval the default value
          @param size the field size
       */
       public IntTextField(int defval, int size)
       {  
          super("" + defval, size);
          setInputVerifier(new IntTextFieldVerifier());
          //      Document doc = getDocument();
          //      doc.addUndoableEditListener(new UndoListener());
       }   protected Document createDefaultModel() 
       {  
          return new IntTextDocument();
       }   /**
          Checks if the contents of this field is a valid integer.
          @return true of the field contents is valid
       */
       public boolean isValid()
       {
          return IntTextDocument.isValid(getText());
       }   /**
          Gets the numeric value of the field contents.
          @param the number that the user typed into the field, or
          0 if the field contents is not valid.
       */
       public int getValue()
       {  
          try
          {  
             return Integer.parseInt(getText());
          }
          catch(NumberFormatException e)
          {
             return 0;  
          }
       }
    }/**
       A verifier that checks if the contents of a text component
       is a valid integer.
    */
    class IntTextFieldVerifier extends InputVerifier
    {
       public boolean verify(JComponent component)
       {
          String text = ((JTextComponent)component).getText();
          return IntTextDocument.isValid(text);
       }
    }
      

  7.   

    你可以自己写一个类继承于TextField并实现ActionListener接口
    这样不是很好吗?
    当然java2技术核心上的那个方法也很不错
    他是用TextField.getDocument();来获取一个Document对象
    然后由Document对象来实现对输入的监听
    可以这么说你所见到的textfield只是个外表,实际上处理任务的是document对象