public abstract class InputVerifier extends Object
InputVerifier的子类,并使用JComponent的setInputVerifier方法,将其子类的实例附加到其想要验证的输入的JComponent中。 
       在焦点转移到请求它的另一个Swing组件之前,调用输入验证器的shouldYieldFocus方法。 
       仅当该方法返回true 。 
       以下示例有两个文本字段,第一个文本字段要求用户输入字符串“pass”。 如果在第一个文本字段中输入该字符串,则用户可以通过在其中单击或按TAB来前进到第二个文本字段。 但是,如果在第一个文本字段中输入另一个字符串,则用户将无法将焦点转移到第二个文本字段。
  import java.awt.*;
 import java.util.*;
 import java.awt.event.*;
 import javax.swing.*;
 // This program demonstrates the use of the Swing InputVerifier class.
 // It creates two text fields; the first of the text fields expects the
 // string "pass" as input, and will allow focus to advance out of it
 // only after that string is typed in by the user.
 public class VerifierTest extends JFrame {
     public VerifierTest() {
         JTextField tf1 = new JTextField ("Type \"pass\" here");
         getContentPane().add (tf1, BorderLayout.NORTH);
         tf1.setInputVerifier(new PassVerifier());
         JTextField tf2 = new JTextField ("TextField2");
         getContentPane().add (tf2, BorderLayout.SOUTH);
         WindowListener l = new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                 System.exit(0);
             }
         };
         addWindowListener(l);
     }
     class PassVerifier extends InputVerifier {
         public boolean verify(JComponent input) {
             JTextField tf = (JTextField) input;
             return "pass".equals(tf.getText());
         }
     }
     public static void main(String[] args) {
         Frame f = new VerifierTest();
         f.pack();
         f.setVisible(true);
     }
 }  
      | Constructor and Description | 
|---|
| InputVerifier() | 
| Modifier and Type | Method and Description | 
|---|---|
| boolean | shouldYieldFocus(JComponent input)
              致电 
              verify(input)以确保输入有效。 | 
| abstract boolean | verify(JComponent input)
              检查JComponent的输入是否有效。 
             | 
public abstract boolean verify(JComponent input)
input - 要验证的JComponent 
           true当有效时, 
            false无效时 
           JComponent.setInputVerifier(javax.swing.InputVerifier) , 
            JComponent.getInputVerifier() 
           public boolean shouldYieldFocus(JComponent input)
verify(input)以确保输入有效。 
           这种方法可以有副作用。 
           特别地,当用户尝试将焦点从参数组件推进到该窗口中的另一个Swing组件时,将调用此方法。 
           如果此方法返回true ,则焦点正常传输; 
           如果它返回false ,则焦点保留在参数组件中。 
          input - 要验证的JComponent 
           true当有效时, 
            false无效时 
           JComponent.setInputVerifier(javax.swing.InputVerifier) , 
            JComponent.getInputVerifier() 
            Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
 Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.