JTextFields仅接受数字的常用方法

JTextFields仅接受数字的常用方法

本文介绍了JTextFields仅接受数字的常用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我限制 JTextFields 只接受数字。我正在使用以下代码。

I limit my JTextFields to accept only numbers. I was using following code for this.

  // my textboxes
  t1=new JTextField(10);
  t2=new JTextField(10);
  t3=new JTextField(10);

  // for the first one
  t1.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
          char c = e.getKeyChar();
          if (!((c >= '0') && (c <= '9') ||
             (c == KeyEvent.VK_BACK_SPACE) ||
             (c == KeyEvent.VK_DELETE))) {
            getToolkit().beep();
            e.consume();
          }
        }
      });

假设我有20个 texboxes 哪个需要相同的验证检查。那么我们需要写这个代码20次吗?我可以写一个通用的方法来实现它吗?我是 Swing 的新手。

Suppose I have 20 texboxes which need the same validation check. So do we need to write this code 20 times? Can I write a common method to implement this? I am new to Swing.

推荐答案

再次使用DocumentFilter作为这将处理复制和粘贴,这将允许在文档接受文本之前过滤

Again, use a DocumentFilter as this will handle copy and paste, this will allow filtering before the Document accepts the text:

import javax.swing.*;
import javax.swing.text.*;

public class DocListenerEg extends JPanel {
   private static final int FIELD_COUNT = 5;
   private JTextField[] fields = new JTextField[FIELD_COUNT];

   public DocListenerEg() {
      MyDocFilter myFilter = new MyDocFilter();
      for (int i = 0; i < fields.length; i++) {
         fields[i] = new JTextField(5);
         ((PlainDocument) fields[i].getDocument()).setDocumentFilter(myFilter);
         add(fields[i]);
      }
   }

   private class MyDocFilter extends DocumentFilter {
      public void insertString(DocumentFilter.FilterBypass fb, int offset,
            String text, AttributeSet attr) throws BadLocationException {
         for (char c : text.toCharArray()) {
            if (!Character.isDigit(c)) {
               return;
            }
         }
         fb.insertString(offset, text.toUpperCase(), attr);
      }

      public void replace(DocumentFilter.FilterBypass fb, int offset,
            int length, String text, AttributeSet attrs)
            throws BadLocationException {
         for (char c : text.toCharArray()) {
            if (!Character.isDigit(c)) {
               return;
            }
         }
         fb.replace(offset, length, text.toUpperCase(), attrs);
      }
   }

   private static void createAndShowGui() {
      DocListenerEg mainPanel = new DocListenerEg();

      JFrame frame = new JFrame("DocListenerEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

这篇关于JTextFields仅接受数字的常用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:34