import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import formatstring;public class MyApp extends JApplet
{
   final JButton b1;
   JTextField jtf=new JTextField(formatstring.f_string);
   JPanel jp1;
   
   public MyApp()
   {
      Container con=getContentPane(); 
      JRootPane rootpane=getRootPane();
      jtf.setDocument(new formatstring(jtf));
      b1=new JButton("OK");
      jp1=new JPanel();
      rootpane.setDefaultButton(b1);
      con.setLayout(new FlowLayout());
      jp1.add(jtf);
      
      con.add("Center",jp1);
      con.add("South",b1);
        
     b1.addMouseListener(new java.awt.event.MouseAdapter() 
     {
        public void mouseClicked(MouseEvent e) 
        {
   //       dlg_show(b1.getText());
        }
     });
   
   b1.addActionListener(new java.awt.event.ActionListener() 
   {
      public void actionPerformed(ActionEvent e) 
      {
     //    dlg_show(b1.getText());
      }
    });
   
   }   
} import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
class formatstring extends PlainDocument
{
   public static String f_string="000.000.000.000";
   private static int s1=3,s2=7,s3=11;
   private JTextComponent jtc;
   private int newoffset;
   int i=-1;
   dlg dlg1;
   
   public formatstring(JTextComponent jtf)
   {
     jtc=jtf;
     try
     {
        insertString(0,f_string,null);
     }
     catch(Exception e)
     {
     
     }  
   }
  
   public void insertString(int offset,String s,AttributeSet aset) throws BadLocationException
   {
     String asd;
     int i;
     
     if(s.equals(f_string))
     {
        super.insertString(offset,s,aset);
     }
     else
     {
       try
       { 
         i=offset;
         if ((i==0)||(i==3)||(i==7)||(i==11) )
         {
            if (Integer.parseInt(s)>2)
                return;
         }       
         else
         {
           if (Integer.parseInt(s)>5)
                return;
         }
         Integer aa=new Integer(i);
    //     dlg1=new dlg("i",1,aa.toString());
       }
       catch(Exception ex)
       {
         return;
       }
       newoffset=offset;
       if(ats(offset))
       {
          newoffset++;
          jtc.setCaretPosition(newoffset);
          
       }
       super.remove(newoffset,1);
       super.insertString(newoffset,s,aset);
     }  
    } 
     public void remove(int offset,int length) throws BadLocationException
     {
       if (ats(offset))
          jtc.setCaretPosition(offset-1);
       else  
          jtc.setCaretPosition(offset); 
          
     } 
        
     private boolean ats(int offset)   
     {
       return offset==s1||offset==s2||offset==s3;
      
     }
     
      }   

解决方案 »

  1.   

    大致的思想就是在text的model类里,对接收的输入作校验,不合法的字符从输入缓冲中去掉。跟上面的代码差不多。
      

  2.   

    再提供另一种方法:监听按键事件时,消灭不合法事件:
    textField.addKeyListener(new KeyAdapter()
    {
       public void keyTyped(KeyEvent evt)
       {
         char ch=evt.getKeyChar();
         if(Character.isUpperCase(ch)
            ch=Character.toLowerCase(ch);
         if(ch>'a' && ch<'z')
            evt.consume();
       }
    });
      

  3.   

    不好意思,出错了
    if(ch>'a' && ch<'z')
    应改为
    if(ch>='a' && ch<='z')
    这种方法的好处是,处理程序永远不会接收到不合法的键入事件