简体   繁体   English

JTextField:如何验证JTextField中的数字输入

[英]JTextField: How to validate number input in JTextField

How to validate a textfield to enter only 10 digits mobile number in Swing? 如何在Swing中验证文本字段以仅输入10位手机号码?

I have three text field. 我有三个文本字段。

  1. For name 为了名字
  2. For Contact 联系方式
  3. No. For Email 否,用于电子邮件

I want to click on submit button then check name, contact and email right or wrong. 我想单击“提交”按钮,然后检查姓名,联系人和电子邮件的对与错。 I also want to set limit of character 我也想设定字数限制

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class UpDateProfile extends JFrame implements ActionListener
{
    JLabel name_lbl,email_lbl,contact_lbl;
    JTextField name_text,email_text,contact_text;
    JButton submit_btn;
    public UpDateProfile()
    {
            super("Velidation demo");

            setSize(650,450);
            setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2-325,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2-225);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new GridBagLayout());
            setResizable(false);

            GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.insets = new Insets(5,3,5,3);

        name_lbl = new JLabel("Name :");
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(name_lbl,gbc1);

        name_text = new JTextField(30);
        gbc1.gridx = 1;
        gbc1.gridy = 0;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 3;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(name_text,gbc1);

        email_lbl = new JLabel("E-mail :");
        gbc1.gridx = 0;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(email_lbl,gbc1);

        alt_email_text = new JTextField(30);
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 3;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(alt_email_text,gbc1);

        contact_lbl = new JLabel("Contact No. :");
        gbc1.gridx = 0;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(contact_lbl,gbc1);

        contact_text = new JTextField(10);
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(contact_text,gbc1);

        submit_btn = new JButton("Submit");
        submit_btn.addActionListener(this);
        gbc1.gridx = 2;
        gbc1.gridy = 7;
        gbc1.ipadx = 10;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 2;
        gbc1.anchor = GridBagConstraints.CENTER;
        add(submit_btn,gbc1);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==submit_btn)
        {

        }
    }
}
public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==submit_btn)
    {
        String text = contact_text.getText();
        if(text.matches("\\d{10}")){
             // The text entered is a 10-digit number
        }else{
             // The text is not valid
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM