如何只允许使用KeyListener在JTextField中编写字母表

时间:2017-01-20 07:24:00

标签: java swing jtextfield keylistener

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

class Form implements KeyListener {
    private JFrame f;
    private JLabel lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9;
    private JTextField tf1, tf2, tf3, tf4, tf5, tf6, tempField;
    private JTextArea ta1;
    private JPanel jp1, jp2, jp3;
    private JButton jb1, jb2, jb3;
    private JComboBox cb1, cb2, cb3, cb4;
    private ButtonGroup bg1;
    private JRadioButton rb1, rb2;

    Form() {
        f = new JFrame();

        lb1 = new JLabel("First Name");
        f.add(lb1);
        tf1 = new JTextField();
        f.add (tf1);

        lb2 = new JLabel("Middle Name");
        f.add(lb2);
        tf2 = new JTextField();
        f.add (tf2);

        lb3 = new JLabel("Last Name");
        f.add(lb3);
        tf3 = new JTextField();
        f.add (tf3);

        lb4 = new JLabel("Date of Birth");
        f.add(lb4);
        /*tf4 = new JTextField();
        f.add (tf4);*/
        String[] day = {"", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
        String[] month = {"", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
        String[] year = {"", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010"};
        jp1 = new JPanel();
        cb1 = new JComboBox(day);
        cb2 = new JComboBox(month);
        cb3 = new JComboBox(year);
        jp1.add(cb1);
        jp1.add(cb2);
        jp1.add(cb3);
        f.add(jp1);

        lb5 = new JLabel("Sex");
        f.add(lb5);
        rb1 = new JRadioButton("Male");
        rb2 = new JRadioButton("Female");
        bg1 = new ButtonGroup();
        bg1.add(rb1);
        bg1.add(rb2);
        jp2 = new JPanel();
        jp2.add(rb1);
        jp2.add(rb2);
        f.add(jp2);

        lb6 = new JLabel("Country");
        f.add(lb6);
        String[] country = {"", "Skaper", "India", "Japan", "Taiwan", "Indonesia", "Philippines", "China", "Australia", "Triangle"};
        cb4 = new JComboBox(country);
        f.add(cb4);

        lb7 = new JLabel("Address");
        f.add(lb7);
        ta1 = new JTextArea();
        f.add(ta1);

        lb8 = new JLabel("Email ID");
        f.add(lb8);
        tf5 = new JTextField();
        f.add (tf5);

        lb9 = new JLabel("Phone Number");
        f.add(lb9);
        tf6 = new JTextField();
        f.add (tf6);

        jp3 = new JPanel();
        jb1 = new JButton("Submit");
        jb2 = new JButton("Reset");
        jp3.add(jb1);
        jp3.add(jb2);
        f.add(jp3);
        jb3 = new JButton("Exit");
        f.add(jb3);

        tf1.addKeyListener(this);
        tf2.addKeyListener(this);
        tf3.addKeyListener(this);

        f.setSize(500, 500);
        f.setVisible(true);
        f.setLayout(new GridLayout(10, 2, 15, 15));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {
        tempField = (JTextField)e.getSource();
        if (tempField.getText >= "0" && tempField.getText <= "9") {
            JOptionPane.showMessageDialog(f, "Please enter only Alphabets");
            e.consume();
        }
    }

    public static void main(String[] args) {
        new Form();
    }
}

我创建了一个使用文本字段接受名称和其他详细信息的表单。如何在tf1,tf2,tf3中添加异常,这样当我按下并释放数字键时,它将使用JOptionPane显示错误消息。这也应该只在我输入tf1,tf2,tf3中的数字键时才会发生,它分别接受firstName,middleName和lastName。 如果按键事件来自textField(tf6)接受电话号码,我只需要允许号码。

0 个答案:

没有答案