回 帖 发 新 帖 刷新版面

主题:有谁愿意帮我检查一下我的聊天室哪里错了??

上个月写了一个简单的聊天室,但功能不全,只能用客户端对客户端的形式进行对话,在服务器中不能发信息,但当进行客户之间谈话时,就偶尔出现了收不到信息的错误,我花了很多时间也找不到错误的所在.........如果大家愿意的话,请帮我看看究竟是怎么回事,谢谢了!


//服务器端程序:
//11开头表示新加入了聊天用户;22开头表示公聊;33开头表示私聊

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

public class Server1 extends JFrame
{
        JTextArea JT2;   // "聊天记录"那个文本域
        JTextArea JT3;     // "发送信息"那个文本域
        JButton B1;             // "发送"按钮
        JButton B2;      // "删除"按钮
        final JCheckBox JC;        //"发送给所有用户"复选框
        TitledBorder TB1;            //"参与者"边框
        TitledBorder TB2;            //"聊天记录"边框
        TitledBorder TB3;            //"发送信息"边框
        Socket s;                            //客户端套接字
        BufferedReader BR;        
        PrintWriter PW;
        boolean bool=false;
        boolean flag=true;
        //String line=System.getProperty("line.separator");         //起到换行作用
        HashMap map=new HashMap();                                                     //用来储存socket和用户名
        JList userList=new JList();                                                     //用来显示参与者
        DefaultListModel ListModel=new DefaultListModel();     
        Vector ve=new Vector();                                                             //用来储存用户名
        
        public Server1(String str)
        {
                super(str);
                this.getContentPane().setLayout(null);                     //设置主窗口的布局管理器为空
                //创建组件
                JT2=new JTextArea(60,30);
              JT2.setCaretPosition(JT2.getText().length());
                JT3=new JTextArea(60,30);
                B1=new JButton("发送");
                B2=new JButton("删除");
                JC=new JCheckBox("发送给所有用户");
                TB1=BorderFactory.createTitledBorder("参与者");
                TB2=BorderFactory.createTitledBorder("聊天记录");
                TB3=BorderFactory.createTitledBorder("发送信息");
                
                //创建面板
                JPanel JP1=new JPanel();
                JPanel JP2=new JPanel();
                JPanel JP3=new JPanel();
                JPanel JP4=new JPanel();
                JPanel JP5=new JPanel();
                
                //布置JP1面板
                JP1.setLayout(new GridLayout(1,1));
                userList.setModel(ListModel);
                JScrollPane JS=new JScrollPane(userList);            //创建滚动条
                JS.setBorder(TB1);                                                        //添加边框
                JP1.add(JS);                                                            
                JP1.setBounds(7,10,150,425);                                    //设置JP1面板的大小和坐标
                this.getContentPane().add(JP1);                                //先用getContentPane()返回主窗口的面板,再在此面板中添加JP1
                
                //布置JP2面板
                JP2.add(JC);
                JP2.setBounds(30,432,150,50);
                this.getContentPane().add(JP2);
                
                 //布置JP3面板
                JP3.setLayout(new GridLayout(1,1));
                JT2.setEditable(false);                                                //设置JT2文本域不可编辑
                JScrollPane JSP2=new JScrollPane(JT2);
                JSP2.setBorder(TB2);
                JP3.add(JSP2);
                JP3.setBounds(160,10,380,250);
                this.getContentPane().add(JP3);
                
                //布置JP4面板
                JP4.setLayout(new GridLayout(1,1));
                JScrollPane JSP3=new JScrollPane(JT3);
                JSP3.setBorder(TB3);
                JP4.add(JSP3);
                JP4.setBounds(160,258,380,177);
                this.getContentPane().add(JP4);
                
                //布置JP5面板
                JLabel j=new JLabel("   ");                                             //此标签只起分隔B1和B2的作用
                JP5.add(B1);
                JP5.add(j);
                JP5.add(B2);
                JP5.setBounds(350,430,200,35);
                this.getContentPane().add(JP5);
                this.pack();                                                                         //用来整理组件的空隙
                B1.addMouseListener(new action());                         //为B1添加监听器,"acion"是一个类,在下面定义了
                JC.addActionListener(new ActionListener()                //为JC添加监听器
                                                         {
                                                                     public void actionPerformed(ActionEvent e) 
                                                                     {
                                                                             bool=JC.isSelected();                //用来标志此复选按钮是否被打勾
                                                                     }
                                                         });
        }
        public void receive()                //接收用户信息
        {
              try
                {
                        ServerSocket ss=new ServerSocket(8000);                //定义服务器套接字
                        while(flag)
                        {
                                s=ss.accept();                 //接收用户的请求,并创建一个客户端套接字
                                work w=new work(s);    //创建线程 w 
                                w.start();                         //启动线程
                      }
                } 
                catch(Exception e)
                {
                        e.printStackTrace();    
              }
        }
        class action extends MouseAdapter                    //自定义监听器
        {   
                public void mousePressed(MouseEvent e)        //鼠标按下所以触发的事件 
                {
                        String str=JT3.getText();                        //获取"发送信息"文本域的内容
                        PW.println(str);                                        //向对应的输出流输出str
                        JT3.setText("");                                        //设置"发送信息"文本域为空
                }
        }

回复列表 (共1个回复)

沙发


问题已解决

我来回复

您尚未登录,请登录后再回复。点此登录或注册