这个问题已经问了多遍了:
Swing的设计采用MVC设计模式,所以只要控制‘Model'就行了:
下面的组件 ValidateTextField 可以实现你要的功能:
class ValidateTextField extends JTextField{
    ValidateTextField(int size){
super(size);
    }
    protected Document createDefaultModel(){
return new ValidateDocument();
    }
}
class ValidateDocument extends PlainDocument{
    public void insertString(int offset, String str, AttributeSet a)
    throws BadLocationException{
if(str.charAt(0)>=(char)'0'&&str.charAt(0)<=(char)'9')
    super.insertString(offset, str, a);
    }
}