java聊天程序

求用JAVA写一网络聊天程序:要求:聊天室服务器、聊天室客户端;客户端之间可以聊天。因为我是初级选手,想借此程序分析学习java,所以代码最好多一点注释...分数有限,望... 求用JAVA写一网络聊天程序:

要求:聊天室服务器、聊天室客户端;客户端之间可以聊天。
因为我是初级选手,想借此程序分析学习java,所以代码最好多一点注释...
分数有限,望大家不吝赐教!!
需要图形用户界面哦,最好用swing组件
服务器端:采用多线程,可以向客户广播:当前聊天室人数,客户名称列表,客户进入离开提示;
客户端:可以设定昵称,性别,客户间可以私聊
展开
 我来答
就是不敢告诉她
2008-05-08 · TA获得超过1015个赞
知道小有建树答主
回答量:249
采纳率:0%
帮助的人:145万
展开全部
服务器端:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Server extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
ServerSocket server = null;
Socket socket = null;BufferedReader instr =null;PrintWriter os=null ;
//Construct the frame
public Server() {
jbInit();

}
class MyThread extends Thread{//该线程负责接受数据
public void run(){
try{
while(true){
this.sleep(100);
instr= new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(instr.ready()){ //检查是否有数据
jTextArea1.append("客户端: "+instr.readLine()+"\n");
}
}
}catch(Exception ex){}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jButton1){
int port=Integer.parseInt(jTextField2.getText().trim());
listenClient(port);
}
if(e.getSource()==jButton2){
String s=this.jTextField3.getText().trim();
sendData(s);
}
}

private void listenClient(int port){//侦听
try{
if(jButton1.getText().trim().equals("侦听")){
server = new ServerSocket(port);
jButton1.setText("正在侦听...");
socket=server.accept();//等待,一直到客户端连接才望下执行
sendData("已经成功连接。。。");
jButton1.setText("正在聊天...");
jTextArea1.append("客户端已经连接到服务器\n");
MyThread t=new MyThread();
t.start();
}

}catch(Exception ex){
}
}
private void sendData(String s){//发送数据
try{
os= new PrintWriter(socket.getOutputStream());
os.println(s);
os.flush();
if(!s.equals("已经成功连接。。。"))
this.jTextArea1.append("Server:"+s+"\n");
}catch(Exception ex){
}
}
//Component initialization
private void jbInit() {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(540, 340));
this.setTitle("服务器");
jLabel2.setBounds(new Rectangle(22, 27, 72, 28));
jLabel2.setText("端口号");
jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
jTextField2.setBounds(new Rectangle(113, 27, 315, 24));

jButton1.setBounds(new Rectangle(440, 28, 73, 25));
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setActionCommand("jButton1");
jButton1.setText("侦听");
jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
jLabel3.setText("请输入信息");
jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
jTextField3.setBounds(new Rectangle(114, 60, 314, 24));
jTextField3.setText("");
jButton2.setText("发送");
jButton2.setActionCommand("jButton1");
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));

contentPane.add(jTextField2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField3, null);
contentPane.add(jButton2, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jLabel2, null);
jScrollPane1.getViewport().add(jTextArea1, null);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socket.close();
instr.close();
System.exit(0);
}catch(Exception ex){
}

}
});
}
public static void main(String arg[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Server frm=new Server();
frm.setVisible(true);

}
}

客户端:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Client extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField("127.0.0.1");
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
BufferedReader instr =null;
Socket socket = null;
PrintWriter os=null;
public Client() {
jbInit();
}
class MyThread extends Thread{
public void run(){
try{
os=new PrintWriter(socket.getOutputStream());
instr=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
this.sleep(100);
if(instr.ready()){
jTextArea1.append("服务器: "
+instr.readLine()+"\n");
}
}
}catch(Exception ex){
}

}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jButton1){
String ip=jTextField3.getText().trim();
int port=Integer.parseInt(jTextField2.getText().trim());
connectServer(ip,port);
}
if(e.getSource()==jButton2){
String s=this.jTextField3.getText().trim();
sendData(s);
}
}
private void connectServer(String ip,int port){//连接

try{
if(jButton1.getText().trim().equals("连接")){
jButton1.setText("连接服务器...");
socket=new Socket(ip,port);
jButton1.setText("正在聊天");

MyThread t=new MyThread();
t.start();
}
}catch(Exception ex){
}
}
private void sendData(String s){//发送数据
try{
os = new PrintWriter(socket.getOutputStream());
os.println(s);
os.flush();
this.jTextArea1.append("Server:"+s+"\n");

}catch(Exception ex){
}
}
private void jbInit() {
contentPane = (JPanel) this.getContentPane();
jLabel1.setFont(new java.awt.Font("宋体", 0, 14));
jLabel1.setText("服务器名称");
jLabel1.setBounds(new Rectangle(20, 22, 87, 28));
contentPane.setLayout(null);
this.setSize(new Dimension(540, 340));
this.setTitle("客户端");

jTextField1.setBounds(new Rectangle(114, 26, 108, 24));
jLabel2.setBounds(new Rectangle(250, 25, 72, 28));
jLabel2.setText("端口号");
jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
jTextField2.setBounds(new Rectangle(320, 27, 108, 24));

jButton1.setBounds(new Rectangle(440, 28, 73, 25));
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setActionCommand("jButton1");
jButton1.setText("连接");
jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
jLabel3.setText("请输入信息");
jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
jTextField3.setBounds(new Rectangle(114, 60, 314, 24));

jButton2.setText("发送");
jButton2.setActionCommand("jButton1");
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));

contentPane.add(jLabel1, null);
contentPane.add(jTextField1, null);
contentPane.add(jLabel2, null);
contentPane.add(jTextField2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField3, null);
contentPane.add(jButton2, null);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1, null);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socket.close();instr.close();os.close();System.exit(0);
}catch(Exception ex){
}

}
});
}
public static void main(String arg[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Client frm=new Client();
frm.setVisible(true);
}
}

先启动服务器端,侦听端口,再启动客户端,就行了
黑屋的寂寞
2015-12-23 · TA获得超过3339个赞
知道小有建树答主
回答量:747
采纳率:82%
帮助的人:336万
展开全部

服务器端:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Server extends JFrame implements ActionListener{
  JPanel contentPane;
  JLabel jLabel2 = new JLabel();
  JTextField jTextField2 = new JTextField("4700");
  JButton jButton1 = new JButton();
  JLabel jLabel3 = new JLabel();
  JTextField jTextField3 = new JTextField();
  JButton jButton2 = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  ServerSocket server = null;
  Socket socket = null;BufferedReader instr =null;PrintWriter os=null ;
  //Construct the frame
  public Server() {
     jbInit();
     
  }
  class MyThread extends Thread{//该线程负责接受数据
  public void run(){
   try{
     while(true){
     this.sleep(100);
     instr= new BufferedReader(new InputStreamReader(socket.getInputStream()));
        if(instr.ready()){ //检查是否有数据    
           jTextArea1.append("客户端:  "+instr.readLine()+"\n");
        }
        }  
      }catch(Exception ex){}
  }
 }
   public void actionPerformed(ActionEvent e){
    if(e.getSource()==jButton1){
      int port=Integer.parseInt(jTextField2.getText().trim());
       listenClient(port);
    }
    if(e.getSource()==jButton2){
     String s=this.jTextField3.getText().trim();
       sendData(s); 
    }
  }
  
  private void listenClient(int port){//侦听
    try{  
         if(jButton1.getText().trim().equals("侦听")){
            server = new ServerSocket(port);
            jButton1.setText("正在侦听...");
           socket=server.accept();//等待,一直到客户端连接才望下执行
           sendData("已经成功连接。。。");
           jButton1.setText("正在聊天...");
           jTextArea1.append("客户端已经连接到服务器\n");
           MyThread t=new MyThread();
           t.start();
         }
       
     }catch(Exception ex){
    }
  }
  private void sendData(String s){//发送数据
     try{
      os= new PrintWriter(socket.getOutputStream());
      os.println(s);
   os.flush();
   if(!s.equals("已经成功连接。。。"))
     this.jTextArea1.append("Server:"+s+"\n");
    }catch(Exception ex){
    }
  }
  //Component initialization
  private void jbInit() {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(540, 340));
    this.setTitle("服务器");
    jLabel2.setBounds(new Rectangle(22, 27, 72, 28));
    jLabel2.setText("端口号");
    jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
    jTextField2.setBounds(new Rectangle(113, 27, 315, 24));
    
    jButton1.setBounds(new Rectangle(440, 28, 73, 25));
    jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton1.setBorder(BorderFactory.createEtchedBorder());
    jButton1.setActionCommand("jButton1");
    jButton1.setText("侦听");
    jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
    jLabel3.setText("请输入信息");
    jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
    jTextField3.setBounds(new Rectangle(114, 60, 314, 24));
    jTextField3.setText("");
    jButton2.setText("发送");
    jButton2.setActionCommand("jButton1");
    jButton2.setBorder(BorderFactory.createEtchedBorder());
    jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton2.setBounds(new Rectangle(440, 58, 73, 25));
    jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));
    contentPane.add(jTextField2, null);
    contentPane.add(jButton1, null);
    contentPane.add(jLabel3, null);
    contentPane.add(jTextField3, null);
    contentPane.add(jButton2, null);
    contentPane.add(jScrollPane1, null);
    contentPane.add(jLabel2, null);
    jScrollPane1.getViewport().add(jTextArea1, null);
       jButton1.addActionListener(this);
     jButton2.addActionListener(this);
     this.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
       try{
        socket.close();
        instr.close();
        System.exit(0);
       }catch(Exception ex){
       }
       
      }
     });
  }
  public static void main(String arg[]){
    JFrame.setDefaultLookAndFeelDecorated(true);
    Server frm=new Server();
    frm.setVisible(true);
   
  }
}



客户端:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Client extends JFrame implements ActionListener{
  JPanel contentPane;
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField("127.0.0.1");
  JLabel jLabel2 = new JLabel();
  JTextField jTextField2 = new JTextField("4700");
  JButton jButton1 = new JButton();
  JLabel jLabel3 = new JLabel();
  JTextField jTextField3 = new JTextField();
  JButton jButton2 = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  BufferedReader instr =null;
  Socket socket = null;
  PrintWriter os=null;
  public Client() {
      jbInit();
  }
  class MyThread extends Thread{
  public void run(){
   try{
    os=new PrintWriter(socket.getOutputStream());
       instr=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    while(true){
    this.sleep(100);
    if(instr.ready()){     
      jTextArea1.append("服务器: "
        +instr.readLine()+"\n");
    }
   }  
   }catch(Exception ex){
   }
   
  }
 }
  public void actionPerformed(ActionEvent e){
    if(e.getSource()==jButton1){
     String ip=jTextField3.getText().trim();
      int port=Integer.parseInt(jTextField2.getText().trim());
       connectServer(ip,port);
    }
    if(e.getSource()==jButton2){
     String s=this.jTextField3.getText().trim();
       sendData(s); 
    }
  }
  private void connectServer(String ip,int port){//连接
    
    try{  
         if(jButton1.getText().trim().equals("连接")){
            jButton1.setText("连接服务器...");
            socket=new Socket(ip,port);
           jButton1.setText("正在聊天");
         
     MyThread t=new MyThread();
     t.start();
           }      
     }catch(Exception ex){
    }
  }
  private void sendData(String s){//发送数据
     try{
      os = new PrintWriter(socket.getOutputStream());
      os.println(s);
   os.flush();
   this.jTextArea1.append("Server:"+s+"\n");
   
    }catch(Exception ex){
    }
  }
  private void jbInit() {
    contentPane = (JPanel) this.getContentPane();
    jLabel1.setFont(new java.awt.Font("宋体", 0, 14));
    jLabel1.setText("服务器名称");
    jLabel1.setBounds(new Rectangle(20, 22, 87, 28));
    contentPane.setLayout(null);
    this.setSize(new Dimension(540, 340));
    this.setTitle("客户端");
 
    jTextField1.setBounds(new Rectangle(114, 26, 108, 24));
    jLabel2.setBounds(new Rectangle(250, 25, 72, 28));
    jLabel2.setText("端口号");
    jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
    jTextField2.setBounds(new Rectangle(320, 27, 108, 24));
    
    jButton1.setBounds(new Rectangle(440, 28, 73, 25));
    jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton1.setBorder(BorderFactory.createEtchedBorder());
    jButton1.setActionCommand("jButton1");
    jButton1.setText("连接");
    jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
    jLabel3.setText("请输入信息");
    jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
    jTextField3.setBounds(new Rectangle(114, 60, 314, 24));
 
    jButton2.setText("发送");
    jButton2.setActionCommand("jButton1");
    jButton2.setBorder(BorderFactory.createEtchedBorder());
    jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton2.setBounds(new Rectangle(440, 58, 73, 25));
    jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));
    contentPane.add(jLabel1, null);
    contentPane.add(jTextField1, null);
    contentPane.add(jLabel2, null);
    contentPane.add(jTextField2, null);
    contentPane.add(jButton1, null);
    contentPane.add(jLabel3, null);
    contentPane.add(jTextField3, null);
    contentPane.add(jButton2, null);
    contentPane.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jTextArea1, null);
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);
    this.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
       try{
        socket.close();instr.close();os.close();System.exit(0);
           }catch(Exception ex){
       }
       
      }
     });
  }
  public static void main(String arg[]){
    JFrame.setDefaultLookAndFeelDecorated(true);
    Client  frm=new Client();
    frm.setVisible(true);
  }
}



先启动服务器端,侦听端口,再启动客户端,就可以使用了。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
usernamepassword
2015-12-11 · TA获得超过106个赞
知道答主
回答量:52
采纳率:0%
帮助的人:28.4万
展开全部
主要用到socket,多线程,IO,集合等技术。具体有什么不懂的可以问我。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式