写事件,getText,转换成byte,判断是否超过200
应该可以实现吧没用过DocumentListener

解决方案 »

  1.   

    用documentListener 监听JTextArea component 记录打击key的次数,来约束 
     
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    public class TextArea {
            static int length=0;
            JTextArea ta;
            TextArea()
              {
                 JFrame frame=new JFrame();
                 ta=new JTextArea();
                 frame.getContentPane().add(ta);
                 ta.setLineWrap(true);
                 ta.getDocument().addDocumentListener(new DocumentListener() {
                                    public void insertUpdate(DocumentEvent e) {
                                      if (length==200)
                                          ta.setEditable(false);
                                      length++;
                                    }                                public void removeUpdate(DocumentEvent e) {
                                    }                                public void changedUpdate(DocumentEvent e) {
                                    }
                                  });             frame.setSize(100,200);
                 frame.setVisible(true);          }
      public static void main(String[] args) {
      new TextArea();  }
    }