JTextField:如何验证 JTextField 中的数字输入
JTextField: How to validate number input in JTextField
如何在 Swing 中验证文本字段是否只输入 10 位手机号码?
我有三个文本字段。
- 名字
- 联系方式
- 没有。对于电子邮件
我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我也想设置字数限制
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
}
}
}
如何在 Swing 中验证文本字段是否只输入 10 位手机号码?
我有三个文本字段。
- 名字
- 联系方式
- 没有。对于电子邮件
我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我也想设置字数限制
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
}
}
}