gpt4 book ai didi

java - JTextField : How to validate number input in JTextField

转载 作者:行者123 更新时间:2023-11-30 08:06:41 34 4
gpt4 key购买 nike

如何验证文本字段在 Swing 中仅输入 10 位数字的手机号码?

我有三个文本字段。

  1. 姓名
  2. 联系方式
  3. 没有。对于电子邮件

我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我还想设置字符限制

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
}
}
}

关于java - JTextField : How to validate number input in JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999048/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com