JAVA设计一个用户注册的小程序
求代码 可以运行 谢谢
在管理员命理提示符里能运行显示的那种 初学的我 能javac 文件名.java 然后可以显示用户名: 密码: ……一个一个的 展开
下面是HTML代码:
<html>
<script language="javascript">
function on_submit()
{//验证数据的合法性
if (form1.username.value == "")
{
alert("用户名不能为空,请输入用户名!");
form1.username.focus();
return false;
}
if (form1.userpassword.value == "")
{
alert("用户密码不能为空,请输入密码!");
form1.userpassword.focus();
return false;
}
if (form1.reuserpassword.value == "")
{
alert("用户确认密码不能为空,请输入密码!");
form1.reuserpassword.focus();
return false;
}
if (form1.userpassword.value != form1.reuserpassword.value)
{
alert("密码与确认密码不同!");
form1.userpassword.focus();
return false;
}
if (!(form1.idnumber.value.length == 15) && !(form1.idnumber.value.length == 18))
{
alert("身份证输入有误,请重新输入!");
form1.idnumber.focus();
return false;
}
if (form1.ema.value.length == 0)
{
alert("请输入E-mail地址!");
form1.ema.focus();
return false;
}
if (form1.ema.value.length != 0)
{
for (i=0; i<form1.ema.value.length; i++)
if (form1.ema.value.charAt(i)=="@")
break;
if (i == form1.ema.value.length)
{
alert("非法E-Mail地址!");
form1.ema.focus();
return false;
}
}
else
{
alert("请输入E-mail!");
form1.ema.focus();
return false;
}
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新用户注册</title>
</head>
<body>
<form method="POST" action="regist.jsp" name="form1"
onsubmit="return on_submit()">
新用户注册
<br>
<br>
用户名(*):
<input type="text" name="username" size=20>
<br>
密 码(*):
<input type="password" name="userpassword" size="20">
<br>
再输一次密码(*):
<input type="password" name="reuserpassword" size="20">
<br>
性 别:
<input type="radio" value="男" checked name="sex">
男
<input type="radio" name="sex" value="女">
女
<br>
出生年月:
<input name="year" size="4" maxlength="4">
年
<select name="month">
<option value="1" selected>
1
</option>
<option value="2">
2
</option>
<option value="3">
3
</option>
<option value="4">
4
</option>
<option value="5">
5
</option>
<option value="6">
6
</option>
<option value="7">
7
</option>
<option value="8">
8
</option>
<option value="9">
9
</option>
<option value="10">
10
</option>
<option value="11">
11
</option>
<option value="12">
12
</option>
</select>
月
<input name="day" size="3" maxlength=4>
日
<br>
身份证号:
<input type="text" name="idnumber" size=20>
<br>
电子邮箱:(*)
<input name="ema" maxlength="28">
<br>
家庭住址:
<input type="text" name="address" size="20">
<br>
<input type="submit" value="提交" name="B1">
<input type="reset" value="全部重写" name="B2">
<br>
</form>
</body>
</html>
运行结果如下图:
不好意思,昨天晚上回来一时忘了,下面是源码:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Register extends JFrame implements ActionListener {
JLabel name,password,repassword,age,email,address,zip,ID;
JTextField tname,tage,temail,taddress,tzip,tID;
JPasswordField tpassword,trepassword;
JButton OK,cancel;
public static void main(String [] args) {
new Register();
}
public Register() {
JPanel p=new JPanel(new GridLayout(9,2));
name=new JLabel("姓名:",JLabel.CENTER);
tname=new JTextField(8);
p.add(name);
p.add(tname);
password=new JLabel("密码:",JLabel.CENTER);
tpassword=new JPasswordField(16);
p.add(password);
p.add(tpassword);
repassword=new JLabel("确认密码:",JLabel.CENTER);
trepassword=new JPasswordField(16);
p.add(repassword);
p.add(trepassword);
age=new JLabel("年龄:",JLabel.CENTER);
tage=new JTextField(4);
p.add(age);
p.add(tage);
email=new JLabel("电子邮件:",JLabel.CENTER);
temail=new JTextField(35);
p.add(email);
p.add(temail);
address=new JLabel("地址:",JLabel.CENTER);
taddress=new JTextField(50);
p.add(address);
p.add(taddress);
zip=new JLabel("邮编:",JLabel.CENTER);
tzip=new JTextField(10);
p.add(zip);
p.add(tzip);
ID=new JLabel("身份证号码:",JLabel.CENTER);
tID=new JTextField(18);
p.add(ID);
p.add(tID);
OK=new JButton("注册");
cancel=new JButton("取消");
p.add(OK);
p.add(cancel);
OK.addActionListener(this);
cancel.addActionListener(this);
setContentPane(p);
setTitle("注册");
setBounds(100,100,150,400);
setVisible(true);
}
public boolean checkPassword() {
String s1=new String(tpassword.getPassword());
String s2=new String(trepassword.getPassword());
if(s1.equals(s2))
return true;
else
return false;
}
public boolean checkEmail() {
String s=temail.getText();
if(s.contains("@"))
return true;
else
return false;
}
public boolean checkID() {
String id=tID.getText();
if(id.length()==15||id.length()==18)
return true;
else
return false;
}
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(b==OK) {
if(checkID()&&checkEmail()&&checkPassword()) {
JOptionPane.showMessageDialog(this,"恭喜你,注册成功!");
}
else {
boolean b1=checkPassword();
boolean b2=checkEmail();
boolean b3=checkID();
if(!b1) {
JOptionPane.showMessageDialog(this,"两次输入密码不一致");
}
if(!b2) {
JOptionPane.showMessageDialog(this,"电子邮件格式不正确");
}
if(!b3) {
JOptionPane.showMessageDialog(this,"身份证号码位数必须为15或18");
}
clear();
}
}
if(b==cancel) {
clear();
}
}
public void clear() {
tname.setText("");
tpassword.setText("");
trepassword.setText("");
tage.setText("");
temail.setText("");
taddress.setText("");
tzip.setText("");
tID.setText("");
}
}
2010-05-15