1、判断asc码的取值范围不就行了
2、键盘监听,可以实现

解决方案 »

  1.   

    想要监听键盘输入,需要安装文本过滤器,覆盖格式器类的方法,并且传递格式器类的对象到JFormattedTextField.试试这个:import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.*;
    import javax.swing.text.*;public class TestInputNumber {
       public static void main(String[] args) {  
          FormatTestFrame frame = new FormatTestFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.show();
       }
    }class FormatTestFrame extends JFrame {  
       public FormatTestFrame() {  
          setTitle("FormatTest");
          setSize(300, 100);      Container contentPane = getContentPane();
          JLabel lblIntField = new JLabel("NumberFilter:");
          JFormattedTextField intField
             = new JFormattedTextField(new 
                InternationalFormatter(
                   NumberFormat.getIntegerInstance()) {
                   protected DocumentFilter getDocumentFilter() {
                      return filter;
                   }
                   private DocumentFilter filter = new IntFilter();
                });
          intField.setValue(new Integer(100));
          intField.setColumns(10);      JPanel mainPanel = new JPanel();
          mainPanel.add(lblIntField);
          mainPanel.add(intField);
          contentPane.add(mainPanel, BorderLayout.CENTER);
       }
        class IntFilter extends DocumentFilter {
           public void insertString(FilterBypass fb, int offset, 
              String string, AttributeSet attr) 
              throws BadLocationException {
              StringBuffer buffer = new StringBuffer(string);
              for (int i = buffer.length() - 1; i >= 0; i--) {
                 char ch = buffer.charAt(i);
                 if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER) 
                    buffer.deleteCharAt(i);
              }
              super.insertString(fb, offset, buffer.toString(), attr);
           }       public void replace(FilterBypass fb, int offset, 
              int length, String string, AttributeSet attr) 
              throws BadLocationException {
              if (string != null) {
                 StringBuffer buffer = new StringBuffer(string);
                 for (int i = buffer.length() - 1; i >= 0; i--) {
                    char ch = buffer.charAt(i);
                    if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER)
                       buffer.deleteCharAt(i);
                 }
                 string = buffer.toString();
              }
              super.replace(fb, offset, length, string, attr);
           }
        }
    }
      

  2.   

    下面是只能输入数字和"\"的JTextField,另外还增加了输入最多字符个数的限制:
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;public class test extends JTextField
    {
    private numDoc doc = null; public test() {
    super(12);
            setDocument( doc = new numDoc());
    }     public test(int maxLen)
        {
            super(12);
            setDocument( doc = new numDoc(maxLen));
        }
        
    public void setMaxLen( int maxLen ) {
    doc.setMaxLen( maxLen );
    } public int getMaxLen() {
    return doc.getMaxLen();
    } public static void main( String args[] ) {
    JFrame f = new JFrame("Only input number characters" );
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Container c = f.getContentPane();
    test test1;
    c.add( test1 = new test(),BorderLayout.NORTH );
    c.add( new test( 10 ),BorderLayout.CENTER );
    test1.setMaxLen( 20 );
    f.pack();
    f.show();
    }
    }class numDoc extends PlainDocument
    {
    int maxLength=16; public numDoc(int maxLen)
    {
    maxLength=maxLen;
    }

    public numDoc(){} public void setMaxLen( int maxLength ) {
    this.maxLength = maxLength;
    } public int getMaxLen() {
    return maxLength;
    } public void insertString(int offset,String s,AttributeSet a)throws BadLocationException 
    {
    int len=getLength();
    String str=getText(0,len);
    if ( (str+s).length()>maxLength )
    {
    Toolkit.getDefaultToolkit().beep();
    return;
    }
    try
    {
    java.util.StringTokenizer tokens = new java.util.StringTokenizer( str+s,"\\" );
    while( tokens.hasMoreElements() ) {
    Integer.parseInt( (String)tokens.nextElement() );
    }
    }
    catch ( Exception e )
    {
    Toolkit.getDefaultToolkit().beep();
        return;
    }

    super.insertString(offset,s,a);
    }
    }
      

  3.   

    谢谢  Danger2000(飞鱼)  和  tomcatjava(小鱼儿) 2位哥哥
    昨晚我写了一个类,实现了上面的功能,不过这边分数很少,在这里分数都给 Danger2000(飞鱼)

    http://expert.csdn.net/Expert/topic/3000/3000655.xml?temp=.2860224 中还有40分,请tomcatjava(小鱼儿) 过去一下,分数都给你此外http://expert.csdn.net/Expert/topic/3005/3005761.xml?temp=.5663874 中还有50,希望Danger2000(飞鱼)  和  tomcatjava(小鱼儿)帮忙解决全屏显示问题。