import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Text extends JFrame implements ActionListener,TextListener
{
private JLabel username,password,email,telephone;
private JTextField usernameField,emailField;
private TextField teleField;
private JPasswordField passwordField;
private JButton registButton,cancelButton;
public Text()
{
super("用户注册");
Container container = getContentPane();
container.setLayout(new FlowLayout());
username = new JLabel("用户名");
username.setToolTipText("请输入用户名");
password = new JLabel("密 码");
password.setToolTipText("密码不能少于6位");
email = new JLabel("邮 箱");
email.setToolTipText("邮箱中必须包含@字符");
telephone = new JLabel("电 话");
telephone.setToolTipText("电话只能为数字");
usernameField = new JTextField(15);
usernameField.addActionListener(this);
passwordField = new JPasswordField(15);
passwordField.addActionListener(this);
emailField = new JTextField(15);
emailField.addActionListener(this);
teleField = new TextField(21);
teleField.addActionListener(this);
teleField.addTextListener(this);
registButton = new JButton("注册");
cancelButton = new JButton("取消");
container.add(username);
container.add(usernameField);
container.add(password);
container.add(passwordField);
container.add(email);
container.add(emailField);
container.add(telephone);
container.add(teleField);
container.add(registButton);
container.add(cancelButton);
registButton.addActionListener(this);
cancelButton.addActionListener(this);
setSize(250,200);
setVisible(true);
setResizable(false);
}
public void textValueChanged(TextEvent event)
{
if (event.getSource() == teleField)
{
/*if (!checkNumber(teleField.getText()))
{
JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE);
teleField.setText("");
} */
}
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == usernameField)
{
if (usernameField.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"用户名不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
}
if (event.getSource() == passwordField)
{
if (passwordField.getPassword().length == 0)
{
JOptionPane.showMessageDialog(this,"密码不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
else if (passwordField.getPassword().length < 6)
{
JOptionPane.showMessageDialog(this,"密码长度不能小于6位","温馨提示",JOptionPane.INFORMATION_MESSAGE);
passwordField.setText("");
}
}
if (event.getSource() == emailField)
{
if (emailField.getText().indexOf("@") < 0 || emailField.getText().indexOf("@") >= (emailField.getText().length() - 1) || emailField.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"您的邮箱格式不正确","温馨提示",JOptionPane.INFORMATION_MESSAGE);
emailField.setText("");
}
}
if (event.getSource() == teleField)
{
if (teleField.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"联系电话不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
else if (!checkNumber(teleField.getText()))
{
JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE);
teleField.setText("");
}
}
if (event.getSource() == registButton)
{
if (usernameField.getText().equals("") || passwordField.getPassword().length == 0 || emailField.getText().equals("") || teleField.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "您填写的信息不完整","温馨提示",JOptionPane.INFORMATION_MESSAGE);
usernameField.setText("");
passwordField.setText("");
emailField.setText("");
teleField.setText("");
}
else if (passwordField.getPassword().length < 6)
{
JOptionPane.showMessageDialog(this,"密码长度不能小于6位","温馨提示",JOptionPane.INFORMATION_MESSAGE);
passwordField.setText("");
}
else if (emailField.getText().indexOf("@") < 0 || emailField.getText().indexOf("@") >= (emailField.getText().length() - 1))
{
JOptionPane.showMessageDialog(this,"您的邮箱格式不正确","温馨提示",JOptionPane.INFORMATION_MESSAGE);
emailField.setText("");
}
else if (!checkNumber(teleField.getText()))
{
JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE);
teleField.setText("");
}
else
JOptionPane.showMessageDialog(this,"恭喜您,注册成功","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
if (event.getSource() == cancelButton)
{
usernameField.setText("");
passwordField.setText("");
emailField.setText("");
teleField.setText("");
}
}
public boolean checkNumber(String str)
{
for (int i=0;i<str.length();i++)
{
return false;
}
return true;
}
public static void main(String args[])
{
Text application = new Text();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}