设计好友列表框功能,思路——
1、当客户端成功登陆后,则客户端把成功登陆信息发送给服务端,
2、由服务端将接收到来自各个成功登陆的客户端的用户信息添加进好友列表,
3、每当有成功登陆的用户就向各个客户端发送完整好友列表
4、好友列表窗要一直死循环着等待接收服务端不断发来的好友列表信息
注意:登陆窗退出时不要关闭socket
聊天窗退出时不要关闭socket
重新整合服务端各种服务到server 类中,只要服务端一开,即可接收客户端的各种请求(登陆、注册、聊天等)
1.3版客户端代码做了登陆成功后释放自身,打开好友列表窗后,还要把成功登陆的信息发送给服务端功能
代码如下:
package com.swift.frame; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ConnectException; import java.net.Socket; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.TitledBorder; import com.swift.util.Center; public class LoginDialog extends JDialog { private static final long serialVersionUID = 1L; private JPasswordField passwordField_2; private JPasswordField passwordField_1; private JTextField textField_2; private JTextField textField; String request = null; String response = null; Socket s; DataOutputStream dos; DataInputStream dis; public static void main(String args[]) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { e1.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { LoginDialog dialog = new LoginDialog(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public LoginDialog() { super(); setResizable(false); setTitle("在线聊天登录框"); getContentPane().setLayout(null); setBounds(100, 100, 427, 301);// 注册时高度为578,不注册是301 // 设置窗口居中 this.setLocation(Center.getPoint(this.getSize())); final JTextField textField_1 = new JTextField(); textField_1.setBounds(148, 90, 192, 42); getContentPane().add(textField_1); final JLabel label = new JLabel(); label.setText("帐 号"); label.setBounds(76, 102, 66, 18); getContentPane().add(label); final JLabel label_1 = new JLabel(); label_1.setText("密 码"); label_1.setBounds(76, 167, 66, 18); getContentPane().add(label_1); final JPasswordField passwordField = new JPasswordField(); passwordField.setBounds(148, 155, 192, 42); getContentPane().add(passwordField); final JButton button_1 = new JButton(); button_1.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { String phone = new String(textField_1.getText()).trim(); String password = new String(passwordField.getPassword()).trim(); if (!phone.equals("") && !password.equals("")) { try { request = "login"; dos.writeUTF(request); response = dis.readUTF(); if (response.equals("login")) { dos.writeUTF(phone); dos.writeUTF(password); String flag=dis.readUTF(); if(flag.equals("success")) { javax.swing.JOptionPane.showMessageDialog(LoginDialog.this, "登录成功"); //把登陆成功的用户发送给服务端,作为好友列表信息 dos.writeUTF(phone); dos.flush(); //登陆窗消失 LoginDialog.this.dispose(); //好友列表窗出现 new FriendsFrame(phone,s); }else if(flag.equals("fail")) { javax.swing.JOptionPane.showMessageDialog(LoginDialog.this, "登录失败"); } } } catch (IOException e1) { e1.printStackTrace(); } } else { javax.swing.JOptionPane.showMessageDialog(LoginDialog.this, "用户名密码必须填写..."); return; } } }); button_1.setText("登录"); button_1.setBounds(230, 222, 106, 36); getContentPane().add(button_1); final JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { if (LoginDialog.this.getHeight() == 301) { LoginDialog.this.setSize(427, 578); } else { LoginDialog.this.setSize(427, 301); } // 设置窗口不断居中 LoginDialog.this.setLocation(Center.getPoint(LoginDialog.this.getSize())); } }); button.setText("注册"); button.setBounds(76, 222, 106, 36); getContentPane().add(button); final JPanel panel = new JPanel(); panel.setLayout(null); panel.setBorder(new TitledBorder(null, "注册用户", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null)); panel.setBounds(10, 278, 401, 226); getContentPane().add(panel); final JLabel label_2 = new JLabel(); label_2.setBounds(44, 41, 65, 18); label_2.setText("手机号:"); panel.add(label_2); textField = new JTextField(); textField.setBounds(115, 35, 225, 30); panel.add(textField); final JButton button_2 = new JButton(); button_2.setText("发送验证"); button_2.setBounds(243, 75, 97, 30); panel.add(button_2); textField_2 = new JTextField(); textField_2.setBounds(115, 104, 95, 30); panel.add(textField_2); final JLabel label_3 = new JLabel(); label_3.setText("验证码:"); label_3.setBounds(44, 110, 65, 18); panel.add(label_3); passwordField_1 = new JPasswordField(); passwordField_1.setBounds(115, 143, 231, 30); panel.add(passwordField_1); passwordField_2 = new