首页>代码>java swing通过socket模拟多人聊天>/chat/src/chatclient/ChatFrame.java
package chatclient;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class ChatFrame extends JFrame {
    private Socket s;
    private String clientName;
    private PrintWriter out;	
	
    JScrollPane srpList = new JScrollPane();
    JList lstUsers = new JList();
    JScrollPane srpChat = new JScrollPane();
    JList lstChat = new JList();
    JTextField txtMessage = new JTextField();
    JComboBox cmbType = new JComboBox();
    JButton btnSend = new JButton();


    public ChatFrame(Socket s, String clientName) {
        try {
            this.s = s;
            this.clientName = clientName;
            this.setTitle("欢迎" + clientName + "来到本聊天室");
            jbInit();
        } catch (Exception exception) {

        }
    }

    private void jbInit() throws Exception {
        lstChat.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        lstChat.setModel(new DefaultListModel());
        lstUsers.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        lstUsers.setModel(new DefaultListModel());
        getContentPane().setLayout(null);
        Border border = BorderFactory.createEtchedBorder(Color.white, new Color(170, 170, 170));
        srpList.setBorder(new TitledBorder(border, "用户列表",
                                           TitledBorder.ABOVE_TOP,
                                           TitledBorder.CENTER,
                                           new Font("Dialog", Font.PLAIN, 12)));
        srpList.setBounds(new Rectangle(18, 14, 104, 314));
        srpChat.setBounds(new Rectangle(137, 16, 327, 309));
        srpChat.setBorder(new TitledBorder(border, "聊天记录",
                                           TitledBorder.ABOVE_TOP,
                                           TitledBorder.CENTER,
                                           new Font("Dialog", Font.PLAIN, 12)));
        cmbType.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        cmbType.setBounds(new Rectangle(344, 333, 115, 28));
        Vector vc = new Vector();
        btnSend.setBounds(new Rectangle(344, 372, 114, 28));
        btnSend.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        btnSend.setText("发送");
        btnSend.addActionListener(new ChatFrame_btnSend_actionAdapter(this));
        txtMessage.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        vc.add("群聊");
        vc.add("私聊");
        cmbType.setBorder(new LineBorder(Color.GRAY));
        cmbType.setModel(new DefaultComboBoxModel(vc));
        this.getContentPane().add(srpList);
        txtMessage.setText("");
        txtMessage.setBounds(new Rectangle(22, 333, 305, 28));
        this.getContentPane().add(txtMessage);
        this.getContentPane().add(srpChat);
        this.getContentPane().add(cmbType);
        this.getContentPane().add(btnSend);
        srpChat.getViewport().add(lstChat);
        srpList.getViewport().add(lstUsers);
        lstUsers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        new ChatThread(s).start();

    }

//发送消息
    public void btnSend_actionPerformed(ActionEvent e) {
        String text = txtMessage.getText().trim();
        txtMessage.setText("");
        if (text.equals("")) {
            JOptionPane.showMessageDialog(null, "信息不能为空!");
        } else {
            //获得发送消息的类型
            int index = cmbType.getSelectedIndex();
            if (index == 0) {
                out.println(clientName + ":all:" + text);
            } else {
                int item = lstUsers.getSelectedIndex();
                if (item == -1) {
                    JOptionPane.showMessageDialog(null, "私聊必须选择一个用户");
                } else {
                    String name=lstUsers.getSelectedValue().toString();
                    if(name.equals(clientName))
                    {
                        JOptionPane.showMessageDialog(null,"您不能和您自己私聊");
                    }else
                    {
                        ((DefaultListModel)lstChat.getModel()).addElement(clientName+"对"+name+"说:"+text);
                        out.println(clientName + ":" + item + ":" + text);
                    }
                }
            }
        }
    }


//发起聊天会话线程
    class ChatThread extends Thread {
        private Socket s;
        public ChatThread(Socket s) {
            this.s = s;
        }

        public void run() {
            try {
                out = new PrintWriter(s.getOutputStream(), true);
                BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                //同时启动一个接受信息的线程
		new GetThread(br).start();
                out.println(clientName);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }


//等候接受信息的线程
    class GetThread extends Thread {
        private BufferedReader br;
        public GetThread(BufferedReader br) {
            this.br = br;
        }

        public void run() {
            try {
                while (true) {
                    String msg = br.readLine();
                    //判断是否服务器已经关闭,或者已经被服务器T出来
                    if (msg == null) {
                        break;
                    }
                    //判断服务器发送过来的内容是什么类型?
                    if(msg.startsWith("list:"))
                    {
                        DefaultListModel  dlm=new DefaultListModel();;
                        String list=msg.substring(5);
                        StringTokenizer st=new StringTokenizer(list,"[,]");
                        while(st.hasMoreTokens())
                        {
                           dlm.addElement(st.nextToken().trim());
                        }
                        lstUsers.setModel(dlm);
                    }else
                    {
                        ((DefaultListModel) lstChat.getModel()).addElement(msg);
                    }
                }
            } catch (Exception ex) {
            } finally {
                JOptionPane.showMessageDialog(null, "<html>与主机失去联系!发生错误的原因可能有:<br>①您的昵称已经存在,请更改<br>②服务器已经关闭,请等待服务器重新启动<br>③您被管理员踢出来了!请注意遵守聊天室规则</html>");
                new Start();
                ChatFrame.this.dispose();
            }
        }
    }

}


class ChatFrame_btnSend_actionAdapter implements ActionListener {
    private ChatFrame adaptee;
    ChatFrame_btnSend_actionAdapter(ChatFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnSend_actionPerformed(e);
    }
}
最近下载更多
1690356080  LV38 2024年4月23日
CL200228  LV4 2023年5月4日
oyonogul  LV1 2022年11月21日
youwuzuichen  LV11 2022年9月8日
huevnn  LV5 2022年6月16日
bearloadprogress  LV7 2022年5月12日
liys1234  LV9 2022年4月22日
装了磁铁的果冻  LV1 2022年1月7日
9843637  LV9 2021年12月14日
tttyyyytt  LV2 2021年6月22日
最近浏览更多
微信网友_7298640909209600  LV2 2024年12月22日
qwertasdfgkwuejwjwjw  LV1 2024年6月27日
周鸣郝  LV2 2024年5月26日
1690356080  LV38 2024年4月23日
krispeng  LV14 2024年4月15日
lalalla159  LV3 2023年12月2日
Sutnuf 2023年11月27日
暂无贡献等级
13161895  LV1 2023年7月4日
2017143155  LV12 2023年6月24日
链路预测 2023年6月19日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友