public void init() {
    // 添加按钮
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);

    mainPanel.setLayout(new GridLayout(0, 3));
    mainWin.add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField0 =
        new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
              protected DocumentFilter getDocumentFilter() {
                return new NumberFilter();
              }
            });
    intField0.setValue(100);
    addRow("只接受数字的文本框", intField0);

    JFormattedTextField intField1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField1.setValue(new Integer(100));
    // 添加输入校验器
    intField1.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("带输入校验器的文本框", intField1);

    // 创建自定义格式器对象
    IPAddressFormatter ipFormatter = new IPAddressFormatter();
    ipFormatter.setOverwriteMode(false);
    // 以自定义格式器对象创建格式化文本框
    JFormattedTextField ipField = new JFormattedTextField(ipFormatter);
    ipField.setValue(new byte[] {(byte) 192, (byte) 168, 4, 1});
    addRow("IP地址格式", ipField);

    mainWin.add(buttonPanel, BorderLayout.SOUTH);
    mainWin.pack();
    mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWin.setVisible(true);
  }
 // 当输入组件失去焦点时,该方法被触发
 public boolean verify(JComponent component) {
   JFormattedTextField field = (JFormattedTextField) component;
   // 返回用户输入是否有效
   return field.isEditValid();
 }