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

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. /**
  8.  * <p>Title: HappyChat聊天系统登录程序</p>
  9.  * <p>Description: 根据指定的服务器地址、用户名和密码登录聊天服务器</p>
  10.  * <p>Copyright: Copyright (c) 2006</p>
  11.  * <p>Filename: Login.java</p>
  12.  * @author 刘志成
  13.  * @version 1.0
  14.  */
  15. public class  Login extends JFrame  implements ActionListener
  16. {
  17. JPanel  pnlLogin;
  18.   JButton  btnLogin,btnRegister,btnExit;
  19. JLabel  lblServer,lblUserName,lblPassword,lblLogo;
  20. JTextField  txtUserName,txtServer;
  21. JPasswordField pwdPassword;
  22. String  strServerIp;
  23.    //用于将窗口定位
  24. Dimension scrnsize;
  25. Toolkit toolkit=Toolkit.getDefaultToolkit();
  26. //构造方法               
  27. public Login()
  28. {
  29. super("登录[HappyChat]聊天室");
  30. pnlLogin=new JPanel();
  31. this.getContentPane().add(pnlLogin);
  32. lblServer=new JLabel("服务器(S):");
  33. lblUserName=new JLabel("用户名(U):");
  34. lblPassword=new JLabel("口  令(P):");
  35. txtServer=new JTextField(20);
  36. txtServer.setText("127.0.0.1");
  37. txtUserName=new JTextField(20);
  38. pwdPassword=new JPasswordField(20);
  39. btnLogin=new JButton("登录(L)");
  40. btnLogin.setToolTipText("登录到服务器");
  41. btnLogin.setMnemonic('L');
  42. btnRegister=new JButton("注册(R)");
  43. btnRegister.setToolTipText("注册新用户");
  44. btnRegister.setMnemonic('R');
  45. btnExit=new JButton("退出(X)");
  46. btnExit.setToolTipText("退出系统");
  47. btnExit.setMnemonic('X');
  48. /*  该布局采用手动布局           *
  49.  * setBounds设置组件位置        *
  50.  *  setFont设置字体、字型、字号  *
  51.  * setForeground设置文字的颜色  *
  52.  *  setBackground设置背景色      *
  53.  *  setOpaque将背景设置为透明    */
  54. pnlLogin.setLayout(null);  //组件用手动布局
  55. pnlLogin.setBackground(new Color(52,130,203));
  56. lblServer.setBounds(50,100,100,30);
  57. txtServer.setBounds(150,100,120,25);
  58. lblUserName    .setBounds(50,130,100,30);
  59. txtUserName    .setBounds(150,130,120,25);
  60. lblPassword    .setBounds(50,160,100,30);
  61. pwdPassword.setBounds(150,160,120,25);
  62. btnLogin       .setBounds(50,200,80,25);
  63. btnRegister     .setBounds(130,200,80,25);
  64. btnExit        .setBounds(210,200,80,25);
  65. Font fontstr=new Font("宋体",Font.PLAIN,12);
  66. lblServer.setFont(fontstr);
  67. txtServer.setFont(fontstr);
  68. lblUserName    .setFont(fontstr);
  69. txtUserName    .setFont(fontstr);
  70. lblPassword    .setFont(fontstr);
  71. pwdPassword.setFont(fontstr);
  72. btnLogin.setFont(fontstr);
  73. btnRegister.setFont(fontstr);
  74. btnExit.setFont(fontstr);
  75. lblUserName.setForeground(Color.BLACK);
  76. lblPassword.setForeground(Color.BLACK);
  77. btnLogin.setBackground(Color.ORANGE);
  78. btnRegister.setBackground(Color.ORANGE);
  79. btnExit.setBackground(Color.ORANGE);
  80. pnlLogin.add(lblServer);
  81. pnlLogin.add(txtServer);
  82. pnlLogin.add(lblUserName);
  83. pnlLogin.add(txtUserName);
  84. pnlLogin.add(lblPassword);
  85. pnlLogin.add(pwdPassword);
  86. pnlLogin.add(btnLogin);
  87. pnlLogin.add(btnRegister);
  88. pnlLogin.add(btnExit);
  89. //设置背景图片
  90. Icon logo1 = new ImageIcon("images\loginlogo.jpg");
  91.   lblLogo = new JLabel(logo1);
  92. lblLogo.setBounds(0,0,340,66);
  93. pnlLogin.add(lblLogo);
  94.         //设置登录窗口
  95.         setResizable(false);
  96. setSize(340,260);
  97. setVisible(true);
  98.     scrnsize=toolkit.getScreenSize();
  99.      setLocation(scrnsize.width/2-this.getWidth()/2,
  100.                       scrnsize.height/2-this.getHeight()/2);
  101.      Image img=toolkit.getImage("images\appico.jpg");
  102.         setIconImage(img);
  103. //三个按钮注册监听
  104. btnLogin  .addActionListener(this);
  105. btnRegister.addActionListener(this);
  106. btnExit   .addActionListener(this);
  107. }  //构造方法结束
  108. //按钮监听响应
  109. public void actionPerformed(ActionEvent ae)
  110. {
  111. Object source=ae.getSource();
  112. if (source.equals(btnLogin))
  113. {
  114.     //判断用户名和密码是否为空
  115.      if(txtUserName.getText().equals("") || pwdPassword.getText().equals(""))
  116.     {
  117.     JOptionPane op1=new JOptionPane();
  118.                 op1.showMessageDialog(null,"用户名或密码不能为空");
  119.             }
  120.             else
  121.     {
  122.     strServerIp=txtServer.getText();
  123.     login();
  124.     }
  125. }
  126. if (source.equals(btnRegister))
  127. {
  128.     strServerIp=txtServer.getText();
  129.     this.dispose();
  130.     new Register(strServerIp);
  131. }
  132. if (source == btnExit)
  133. {
  134.     System.exit(0);
  135. }
  136. }  //actionPerformed()结束
  137. //////////登录事件响应方法//////////
  138. public void login()
  139. {
  140.     //接受客户的详细资料
  141.         Customer data=new Customer();
  142.     data.custName=txtUserName.getText();
  143. data.custPassword = pwdPassword.getText();
  144. try
  145. {
  146. //连接到服务器
  147.     Socket toServer;
  148.        toServer = new Socket(strServerIp,1001);
  149.     ObjectOutputStream streamToServer=new ObjectOutputStream (toServer.getOutputStream());
  150.     //写客户详细资料到服务器socket
  151.     streamToServer.writeObject((Customer)data);           
  152.             //读来自服务器socket的登录状态
  153.             BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
  154.             String status=fromServer.readLine();
  155.             if (status.equals("登录成功"))
  156.             {
  157. new ChatRoom((String)data.custName,strServerIp);
  158.                 this.dispose();
  159.                 //关闭流对象
  160.         streamToServer.close();
  161.                 fromServer.close();
  162.                 toServer.close();
  163.             }
  164.             else
  165.             {
  166.                 JOptionPane.showMessageDialog(null,status);
  167.                //关闭流对象
  168.         streamToServer.close();
  169.                 fromServer.close();
  170.                 toServer.close();
  171.             }
  172.          }
  173.          catch(ConnectException e1)
  174.          {
  175.           JOptionPane.showMessageDialog(null,"未能建立到指定服务器的连接!");
  176.          }
  177.  catch(InvalidClassException e2)
  178.  {
  179.     JOptionPane.showMessageDialog(null,"类错误!");
  180.  }
  181.  catch(NotSerializableException e3)
  182.  {
  183. JOptionPane.showMessageDialog(null,"对象未序列化!");
  184.  }
  185.  catch(IOException e4)
  186.  {
  187.   JOptionPane.showMessageDialog(null,"不能写入到指定服务器!");
  188.  }
  189. }   //login()结束
  190. public static void main(String args[])
  191. {
  192. new Login();
  193. }
  194. }  //Class Login结束