Register.java
上传用户:tendy5003
上传日期:2022-07-16
资源大小:210k
文件大小:9k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7. /*<p>Title:HappyChat聊天系统用户注册程序</p>
  8.  *<p>Description:新用户通过输入相关信息注册成系统用户</p>
  9.  *<p>Copyright:Copyright(C)2006</p>
  10.  *<p>Filename:Register.java</p>
  11.  *@author 刘志成
  12.  *@version 1.0
  13.  */
  14. public class Register extends JFrame  implements ActionListener
  15. {
  16. JPanel  pnlRegister;
  17. JLabel  lblUserName,lblGender,lblAge;
  18. JLabel  lblPassword,lblConfirmPass,lblEmail,logoPosition;
  19. JTextField  txtUserName,txtAge,txtEmail;
  20. JPasswordField  pwdUserPassword,pwdConfirmPass;
  21. JRadioButton  rbtnMale,rbtnFemale;
  22. ButtonGroup  btngGender;
  23.     JButton  btnOk,btnCancel,btnClear;
  24. String  strServerIp;
  25.     //用于将窗口用于定位
  26. Dimension scrnsize;
  27.     Toolkit toolkit=Toolkit.getDefaultToolkit();
  28.     //构造方法
  29. public Register(String ip)
  30. {
  31. super("[HappyChat]聊天室注册窗口");
  32. strServerIp=ip;
  33. pnlRegister=new JPanel();
  34. this.getContentPane().add(pnlRegister);
  35. lblUserName=new JLabel("用 户 名:");
  36. lblGender=new JLabel("性    别:");
  37. lblAge=new JLabel("年    龄:");
  38. lblPassword=new JLabel("口    令:");
  39. lblConfirmPass=new JLabel("确认口令:");
  40. lblEmail=new JLabel("电子邮件:");
  41. txtUserName=new JTextField(30);
  42. txtEmail=new JTextField(30);
  43. txtAge=new JTextField(10);
  44. pwdUserPassword=new JPasswordField(30);
  45. pwdConfirmPass=new JPasswordField(30);
  46. rbtnMale=new JRadioButton("男",true);
  47. rbtnFemale=new JRadioButton("女");
  48.     btngGender=new ButtonGroup();
  49.     btnOk=new JButton("确定(O)");
  50.     btnOk.setMnemonic('O');
  51.     btnOk.setToolTipText("保存注册信息");
  52. btnCancel=new JButton("返回(B)");
  53. btnCancel.setMnemonic('B');
  54. btnCancel.setToolTipText("返回登录窗口");
  55. btnClear=new JButton("清空(L)");
  56. btnClear.setMnemonic('L');
  57. btnClear.setToolTipText("清空注册信息");
  58. /*  该布局采用手动布局           *
  59.  * setBounds设置组件位置        *
  60.  *  setFont设置字体、字型、字号  *
  61.  * setForeground设置文字的颜色  *
  62.  *  setBackground设置背景色      *
  63.  *  setOpaque将背景设置为透明    */
  64. pnlRegister.setLayout(null);    //组件用手动布局
  65. pnlRegister.setBackground(new Color(52,130,203));
  66. lblUserName.setBounds(30,80,100,30);
  67. txtUserName.setBounds(110,85,120,20);
  68. lblPassword.setBounds(30,105,100,30);
  69. pwdUserPassword.setBounds(110,110,120,20);
  70. lblConfirmPass.setBounds(30,130,100,30);
  71. pwdConfirmPass.setBounds(110,135,120,20);
  72. lblGender.setBounds(30,155,100,30);
  73. rbtnMale.setBounds(110,160,60,20);
  74. rbtnFemale.setBounds(190,160,60,20);
  75. lblAge.setBounds(30,180,100,30);
  76. txtAge.setBounds(110,185,120,20);
  77. lblEmail.setBounds(30,205,100,30);
  78. txtEmail.setBounds(110,210,120,20);
  79.     btnOk.setBounds(250,135,80,25);
  80.     btnCancel.setBounds(250,170,80,25);
  81.     btnClear.setBounds(250,210,80,25);
  82. Font fontstr=new Font("宋体",Font.PLAIN,12);
  83. lblUserName.setFont(fontstr);
  84.     lblGender.setFont(fontstr);
  85. lblPassword.setFont(fontstr);
  86. lblConfirmPass.setFont(fontstr);
  87. lblAge.setFont(fontstr);
  88. lblEmail.setFont(fontstr);
  89.         rbtnMale.setFont(fontstr);
  90. rbtnFemale.setFont(fontstr);
  91. txtUserName.setFont(fontstr);
  92. txtEmail.setFont(fontstr);
  93. btnOk.setFont(fontstr);
  94. btnCancel.setFont(fontstr);
  95. btnClear.setFont(fontstr);
  96. lblUserName.setForeground(Color.BLACK);
  97. lblGender.setForeground(Color.BLACK);
  98. lblPassword.setForeground(Color.BLACK);
  99. lblAge.setForeground(Color.BLACK);
  100. lblConfirmPass .setForeground(Color.BLACK);
  101. lblEmail.setForeground(Color.BLACK);
  102. rbtnMale.setForeground(Color.BLACK);
  103. rbtnFemale.setForeground(Color.BLACK);
  104. rbtnMale.setBackground(Color.white);
  105. rbtnFemale.setBackground(Color.white);
  106. btnOk.setBackground(Color.ORANGE);
  107.     btnCancel.setBackground(Color.ORANGE);
  108.     btnClear.setBackground(Color.ORANGE);
  109. rbtnMale.setOpaque(false);   
  110. rbtnFemale.setOpaque(false);
  111. pnlRegister.add(lblUserName);
  112. pnlRegister.add(lblGender);
  113. pnlRegister.add(lblPassword);
  114. pnlRegister.add(lblConfirmPass);
  115. pnlRegister.add(lblEmail);
  116. pnlRegister.add(lblAge);
  117. pnlRegister.add(txtAge);
  118. pnlRegister.add(txtUserName);
  119. pnlRegister.add(txtEmail);
  120. pnlRegister.add(pwdUserPassword);
  121. pnlRegister.add(pwdConfirmPass);
  122. pnlRegister.add(btnOk);
  123. pnlRegister.add(btnCancel);
  124. pnlRegister.add(btnClear);
  125. pnlRegister.add(rbtnMale);
  126. pnlRegister.add(rbtnFemale);
  127. btngGender.add(rbtnMale);
  128.     btngGender.add(rbtnFemale);
  129.     
  130.     //设置背景图片
  131.     Icon logo = new ImageIcon("images\registerlogo.jpg");
  132.   logoPosition = new JLabel(logo);
  133. logoPosition.setBounds(0, 0, 360,78);
  134. pnlRegister.add(logoPosition);
  135.     
  136.     this.setSize(360,270);
  137. this.setVisible(true);
  138. this.setResizable(false);
  139. //将窗口定位在屏幕中央
  140.      scrnsize=toolkit.getScreenSize();
  141.      this.setLocation(scrnsize.width/2-this.getWidth()/2,
  142.                       scrnsize.height/2-this.getHeight()/2);
  143. Image img=toolkit.getImage("images\appico.jpg");
  144.         this.setIconImage(img);
  145. //三个按钮注册监听
  146. btnOk    .addActionListener(this);
  147. btnCancel.addActionListener(this);
  148. btnClear   .addActionListener(this);
  149. }  //构造方法结束
  150. //按钮监听响应
  151. public void actionPerformed(ActionEvent ae)
  152. {
  153. Object source=new Object();
  154.     source=ae.getSource();
  155.     if (source.equals(btnOk))      //"确定"按钮
  156.     {
  157.         register();
  158.     }
  159.     if (source.equals(btnCancel))  //"返回"按钮
  160.     {
  161.      new Login();
  162.      this.dispose();
  163.     }
  164.     if (source.equals(btnClear))     //"清空"按钮
  165.     {
  166.         txtUserName.setText("");
  167.         pwdUserPassword.setText("");
  168.         pwdConfirmPass.setText("");
  169.         txtAge.setText("");
  170.         txtEmail.setText("");
  171.     }
  172. }  //actionPerformed()结束
  173. //////////"确定"按钮事件响应//////////
  174. public void register()
  175. {
  176. //接受客户的详细资料
  177.         Register_Customer data=new Register_Customer();
  178.     data.custName     = txtUserName.getText();
  179. data.custPassword = pwdUserPassword.getText();
  180. data.age          = txtAge.getText();
  181. data.sex          = rbtnMale.isSelected()?"男":"女";
  182. data.email        = txtEmail.getText();
  183. //验证用户名是否为空
  184. if(data.custName.length()==0)
  185. {
  186.     JOptionPane.showMessageDialog(null,"用户名不能为空");
  187.             return;
  188. }
  189. //验证密码是否为空
  190. if(data.custPassword.length()==0)
  191. {
  192.     JOptionPane.showMessageDialog(null,"密码不能为空");
  193.             return;
  194. }
  195. //验证密码的一致性
  196. if(!data.custPassword.equals(pwdConfirmPass.getText()))
  197. {
  198.     JOptionPane.showMessageDialog(null,"密码两次输入不一致,请重新输入");
  199.             return;
  200. }
  201. //验证年龄是否为空
  202. if(data.age.length()==0)
  203. {
  204.     JOptionPane.showMessageDialog(null,"年龄不能为空");
  205.             return;
  206. }
  207. //验证年龄的合法性
  208. int age=Integer.parseInt(txtAge.getText());
  209. if (age<=0||age>100){
  210. JOptionPane.showMessageDialog(null,"年龄输入不合法");
  211. return;
  212. }
  213. //验证Email的正确性
  214. int Found_flag=0;    //判断标志
  215. for (int i=0;i<data.email.length();i++)
  216. {
  217.     if(data.email.charAt(i)=='@')
  218.     {
  219.         Found_flag++;
  220.     }
  221. }
  222. if(Found_flag!=1)
  223. {
  224.     JOptionPane.showMessageDialog(null,"电子邮箱格式不正确,请重新输入");
  225.             return;
  226. }
  227. try
  228. {
  229.     //连接到服务器
  230.     Socket toServer;
  231.        toServer = new Socket(strServerIp,1001);
  232.     ObjectOutputStream streamToServer=new ObjectOutputStream (toServer.getOutputStream());
  233.     //写客户详细资料到服务器socket
  234.     streamToServer.writeObject((Register_Customer)data);
  235.             //读来自服务器socket的登陆状态
  236.             BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
  237.             String status=fromServer.readLine();
  238.             //显示成功消息
  239.             JOptionPane op=new JOptionPane();
  240.             op.showMessageDialog(null,status);
  241.             if(status.equals(data.custName+"注册成功"))
  242.             {
  243.                 txtUserName.setText("");
  244.                 pwdUserPassword.setText("");
  245.                 pwdConfirmPass.setText("");
  246.                 txtAge.setText("");
  247.                 txtEmail.setText("");
  248.             }
  249.             
  250.             //关闭流对象
  251.     streamToServer.close();
  252.             fromServer.close();
  253.          }
  254.  catch(InvalidClassException e1)
  255.  {
  256.     JOptionPane.showMessageDialog(null,"类错误!");
  257.  }
  258.  catch(NotSerializableException e2)
  259.  {
  260. JOptionPane.showMessageDialog(null,"对象未序列化!");
  261.  }
  262.  catch(IOException e3)
  263.  {
  264.   JOptionPane.showMessageDialog(null,"不能写入到指定服务器!");
  265.  }
  266. }  //方法register()结束
  267. public static void main(String args[])
  268. {
  269. new Register("127.0.0.1");
  270. }
  271. }  //class Register 结束