你把这句
b.addActionListener(new ActionListener()
     {
      public void actionPerformed(ActionEvent e)
      {
       if(!(txt.getText().length() >='A' && txt.getText().length() <= 'Z')||(txt.getText().length() >='a' && txt.getText().length() <= 'z'))
//  就是这句有问题   
改为:
b.addKeyListener(new KeyAdapter()
    {
     public void keyTyped(KeyEvent e)
      {
        char c = e.getKeyChar();
        String textValue = OnlyInputCharacterField.this.getText();
        int len = textValue.length();
        if (Character.isLetter(c))
        { return;}
        else
        { e.consume();}
      }
    });
试试

解决方案 »

  1.   

    if (Character.isLetter(c))
            { return;}
            else
            { e.consume();}在这里的else中漏了一句
    System.out.println("请输入字母!");
      

  2.   

    我把完整的改过的代码给你:个人意见import java.awt.*;
    import java.awt.event.*;public class TestWeb {
      public static void main(String args[]) {
        FrameDemo f = new FrameDemo();
        f.show();
      }
    }class FrameDemo
        extends Frame {
      Button b = new Button("ok");
      TextField txt = new TextField(20);
      public FrameDemo() {
        setSize(200, 100);
        setLayout(new FlowLayout());
        add(txt);
        add(b);
        txt.addKeyListener(new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (Character.isLetter(c)) {
              return;
            }
            else {
              e.consume();
              System.out.println("Please input!");
            }
          }
        });    /*
         b.addActionListener(new ActionListener()
         {
         public void actionPerformed(ActionEvent e)
         {
         if(!(txt.getText().length() >='A' && txt.getText().length() <= 'Z')||
         (txt.getText().length() >='a' && txt.getText().length() <= 'z'))
         {
         System.out.println("请输入字母");
         }
         }
         });*/  }
    }
      

  3.   

    to: sealwzq(幻影)编译错误
    --------------------Configuration: j2sdk1.4.2 <Default>--------------------
    E:\Test.java:39: cannot resolve symbol
    symbol: class OnlyInputCharacterField 
    String textValue = OnlyInputCharacterField.this.getText();
                                                       ^
    1 errorProcess completed.
      

  4.   

    to: sealwzq(幻影)
    现在编译成功了
    但是有个问题,不能按退格符或者delete阿,如果输错的话……
      

  5.   

    你可以判断,如果是删除键就作相应的
    可以增加一句
    if(c=='\b'){你的代码}
      

  6.   

    package myjava;import java.awt.*;
    import java.awt.event.*;public class TestWeb
    {
      public static void main(String args[])
      {
        FrameDemo f = new FrameDemo();
        f.show();
      }
    }class FrameDemo extends Frame implements  ActionListener
    {
      Button b = new Button("ok");
      TextField txt = new TextField(20);
      public FrameDemo()
      {
        setSize(200, 100);
        setLayout(new FlowLayout());
        add(txt);
        add(b);
        txt.addKeyListener(new KeyAdapter()
        {
          public void keyTyped(KeyEvent e)
          {
            char c = e.getKeyChar();
            String textValue = txt.getText();
            int len = textValue.length();
            if (c == '\b'){}
            else
            {
              if (Character.isLetter(c))
              {
                return;
              }
              else
              {
                e.consume();
                System.out.println("Please input!");
              }
            }      }
        });
        txt.addActionListener(this);
      }  public void actionPerformed(ActionEvent evt)
      {
        System.out.println(txt.getText());
      }
    }