方法一,使用InputVerifier,详见:JComponent.setInputVerifier
方法二:为JTextField添加InputMethodListener,在内容即将被输入时检测输入字符。

解决方案 »

  1.   

    JTextField txt=new JTextField("............");
    try
    {
    int x=Integer.parseInt(txt.getText());
    }
    catch(NumberFormatException ne)  
    {
    JOptionPane msg=new JOptionPane();
    msg.showMessageDialog(null, "不能有非数字!", "Error",JOptionPane.ERROR_MESSAGE);
    }
      

  2.   

    方法三:为JTextField控件添加FocusListener,当控件将要失去焦点时判断,如果非法,把焦点再抓回来。
    方法四:在某个按钮的事件方法中检查所有的输入信息的有效性。
      

  3.   

    userIDtxt= new JFormattedTextField(new 
                InternationalFormatter(NumberFormat.getIntegerInstance())
                {
                   private DocumentFilter filter = new IntFilter();
                   protected DocumentFilter getDocumentFilter()
                   {
                      return filter;
                   }
                   
                });
            userIDtxt.setColumns(10);
    ……class IntFilter extends DocumentFilter
    {
       public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) 
          throws BadLocationException 
       {
          StringBuilder builder = new StringBuilder(string);
          for (int i = builder.length() - 1; i >= 0; i--)
          {
             int cp = builder.codePointAt(i);
             if (!Character.isDigit(cp)) 
             {
                builder.deleteCharAt(i);
                if (Character.isSupplementaryCodePoint(cp))
                {
                   i--;
                   builder.deleteCharAt(i);
                }
             }
          }
          super.insertString(fb, offset, builder.toString(), attr);
       }   public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr) 
          throws BadLocationException 
       {
          if (string != null) 
          {
             StringBuilder builder = new StringBuilder(string);
             for (int i = builder.length() - 1; i >= 0; i--)
             {
                int cp = builder.codePointAt(i);
                if (!Character.isDigit(cp)) 
                {
                   builder.deleteCharAt(i);
                   if (Character.isSupplementaryCodePoint(cp))
                   {
                      i--;
                      builder.deleteCharAt(i);
                   }
                }
             }
             string = builder.toString();
          }
          super.replace(fb, offset, length, string, attr);
       }
    }
      

  4.   

    不过这个方法有个不好的一点,它可以屏蔽掉其它输入 不过会显示一个带,分隔符的数字,我还没看完java核心技术,所以目前也不知道怎么解决。应该可以弄掉的 ^_^
      

  5.   

    已经搞定,不过我是按2楼的方法搞的,不过我觉得它就是要抛出数字格式的异常,
    只要在try中定义了int x=Integer.parseInt(txt.getText());  //(我把那些最基本的给忘了,唉~)
    在catch中加入我的意思就可以搞定了。
    angues1980(石头心) 
    你给的我还正在看。
    谢谢大家!!!
      

  6.   

    JTextField t1=new JTextField ();
    String k1=t1.getText();
    try{
    Character c=new Character(kl.charAt(t));
    if(c.hashCode()>57 | c.hashCode()<48)
    {
    JOptionPane.showMessageDialog(null,"请规范你的输入,输入的不是数字!","Error",JOptionPane.WARNING_MESSAGE);
                                return;
    }
    }catch(Exception r){}
      

  7.   

    其实要是我写我也用int x=Integer.parseInt(txt.getText());来实现
    给你的那个知道有那么个方法就行了,没必要搞那么复杂