参见:http://expert.csdn.net/Expert/topic/1783/1783604.xml?temp=5.898684E-02

解决方案 »

  1.   

    jTextArea1.addKeyListener(new KeyAdapter() {
              public void keyTyped(KeyEvent e) {
                  String str = jTextArea1.getText();
                  if(str.length()>10){
                      System.out.println("字数太多");
                  }
              }
          });
    很笨的办法,呵呵
      

  2.   

    谢谢!我已经解结了,myTextArea = new JTextArea (new maxLengthDocument (30));
    public class maxLengthDocument extends PlainDocument{
       int maxChars;
       public maxLengthDocument (int max){
          maxChars = max;   }
       public void insertString(int offset, String s, AttributeSet a) 
              throws BadLocationException   {
          if (getLength() + s.length() > maxChars) {
              Toolkit.getDefaultToolkit().beep();
              return;      
          }
          super.insertString (offset, s, a);   
       }
    }