使用java swing在JTextField中进行字母验证

时间:2011-12-21 09:28:26

标签: java swing jtextfield

当我在第一个JTextField中输入一个值时,需要对其进行验证,以便它只接受字母字符而不接受其他字符。当我输入字母以外的字符时,不应该将其放入JTextField,并且应该在按下其他键时以标签格式给出“仅应输入字母字符”的消息。

这是我使用的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class valid extends JFrame  {
    perform1 per1=new perform1();
    String num1="Alpha";
    String num2="Numeric";
    String num3="Alphanumeric";
    Container contentPane = getContentPane();
    JPanel jp=new JPanel(new GridLayout(2, 2));
    JLabel label1=new JLabel("STUDENT NAME", JLabel.LEFT);
    JLabel label2=new JLabel("REG NO", JLabel.LEFT);
    JTextField text1=new JTextField(15);
    JTextField text2=new JTextField(15);
    public valid(){
        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text1, e, num1);

            }
        });
        text2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text2, e, num2);
            }
        });
        setLayout(new FlowLayout(FlowLayout.CENTER));
        jp.add(label1);
        jp.add(text1);
        jp.add(label2);
        jp.add(text2);
        contentPane.add("Center", jp);
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String ar[])
    {
        new valid();
    }
}
class perform1{
public void dis(JTextField PTxtCtrl,KeyEvent PKeyvalue,String PTempStr){
    JLabel error=new JLabel();
    //JTextField text=new JTextField();
    String TmpCurntStr;
    String TmpConvStr;
    value val = value.valueOf(PTempStr);

   switch(val){
       case Alpha:
           if((PKeyvalue.getKeyChar()>='a'&& PKeyvalue.getKeyChar()<='z')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           TmpConvStr=TmpCurntStr.toUpperCase();
           PTxtCtrl.setText(TmpConvStr);
           //PTxtCtrl.setEditable(true);
           //error.setText("");
          }else{
           //PTxtCtrl.setEditable(false);
           //error.setText("* Enter only numeric digits(0-9)");

           }
           break;
       case Numeric:
           if((PKeyvalue.getKeyChar()>='0'&& PKeyvalue.getKeyChar()<='9')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           System.out.println("hai");
           }else{
               JOptionPane.showMessageDialog(null,"Only numeric");
           }
           break;
       case Alphanumeric:
           break;
   }
 }
  enum value{
    Alpha,
    Numeric,
    Alphanumeric
}
}

3 个答案:

答案 0 :(得分:4)

您可以使用JFormattedTextField或通常的JTextField,但可以指定自己的DocumentFilter。

答案 1 :(得分:1)

如果要删除不是字母的字符,可以使用replaceAll替换它们。例如:

TmpCurntStr=PTxtCtrl.getText();
TmpConvStr=TmpConvStr.replaceAll("[^a-zA-Z]", "");
PTxtCtrl.setText(TmpConvStr);

答案 2 :(得分:1)

如果您不理解,请更正检查,然后告诉我。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class valid extends JFrame  {
    perform1 per1=new perform1();
    String num1="Alpha";
    String num2="Numeric";
    String num3="Alphanumeric";
    Container contentPane = getContentPane();
    JPanel jp=new JPanel(new GridLayout(2, 2));
    JLabel label1=new JLabel("STUDENT NAME", JLabel.LEFT);
    JLabel label2=new JLabel("REG NO", JLabel.LEFT);
    JTextField text1=new JTextField(15);
    JTextField text2=new JTextField(15);
    public valid(){
        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                /**
                 * check ASCII value of character enter in text1
                 * if it not fall in alphabets then send Alphanumeric
                 * 
                 */
                char text = e.getKeyChar();
                if(text >= 65 && text <= 90 || text >= 97 && text <= 122){
                    per1.dis(text1, e, num1);
                }else{
                    per1.dis(text1, e, num3);
                }

            }
        });
        text2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text2, e, num2);
            }
        });
        setLayout(new FlowLayout(FlowLayout.CENTER));
        jp.add(label1);
        jp.add(text1);
        jp.add(label2);
        jp.add(text2);
        contentPane.add("Center", jp);
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String ar[])
    {
        new valid();
    }
}
class perform1{
public void dis(JTextField PTxtCtrl,KeyEvent PKeyvalue,String PTempStr){
    JLabel error=new JLabel();
    //JTextField text=new JTextField();
    String TmpCurntStr;
    String TmpConvStr;
    value val = value.valueOf(PTempStr);

   switch(val){
       case Alpha:
           if((PKeyvalue.getKeyChar()>='a'&& PKeyvalue.getKeyChar()<='z')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           TmpConvStr=TmpCurntStr.toUpperCase();
           PTxtCtrl.setText(TmpConvStr);
           //PTxtCtrl.setEditable(true);
           //error.setText("");
          }else{
           //PTxtCtrl.setEditable(false);
           //error.setText("* Enter only numeric digits(0-9)");

           }
           break;
       case Numeric:
           if((PKeyvalue.getKeyChar()>='0'&& PKeyvalue.getKeyChar()<='9')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           System.out.println("hai");
           }else{
               JOptionPane.showMessageDialog(null,"Only numeric");
               PTxtCtrl.setText("");
           }
           break;
       case Alphanumeric:
//         System.err.println("check");
           /**
            * if it send Alphanumeric then give a pop up message
            */
           JOptionPane.showMessageDialog(null,"Only Alphabets");
           PTxtCtrl.setText("");
           break;
   }
 }
  enum value{
    Alpha,
    Numeric,
    Alphanumeric
}
}