-
大小: 5.71 KB文件类型: .rar金币: 2下载: 0 次发布日期: 2024-08-29
- 语言: Java
- 标签:
资源简介
采用TCP SOCKET技术编写C/S模式的聊天室软件,聊天室具有群聊、私聊等功能,支持多个连接,支持消息边界,
代码片段和文件信息
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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.Socket;
import java.net.UnknownHostException;
public class ChatClient extends frame {
private TextArea msgShow; //显示聊天信息文本框
private List onlinePeople;////显示在线用户信息
private TextField msgContent;//聊天输入
private Button btnPost;
private String address = “127.0.0.1“;
public static final int PORT = 8842;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private Socket s = null;
private String nickname;
//用户界面
ChatClient() {
this.setBounds(300 100 500 300);
Panel p = new Panel(new BorderLayout());
Panel pTop = new Panel(new BorderLayout());
Panel pBottom = new Panel(new FlowLayout());
msgShow = new TextArea(14 50);
onlinePeople = new List(15 false);
pTop.add(msgShow BorderLayout.WEST);
pTop.add(onlinePeople BorderLayout.EAST);
msgContent = new TextField(60);
btnPost = new Button(“发送“);
msgContent.addKeyListener(new KeyMonitor());
btnPost.addActionListener(new ActionMonitor());
pBottom.add(msgContent);
pBottom.add(btnPost);
p.add(pTop BorderLayout.NORTH);
p.add(pBottom BorderLayout.SOUTH);
this.add(p);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() { //事件处理
public void windowClosing(WindowEvent e) {
sendMsg(nickname + “ 离开“);
setVisible(false);
System.exit(0);
}
});
}
class ActionMonitor implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
post();
}
}
class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
post();
}
}
}
//客户端发送消息并在自己的文本框上显示
public void post() {
String content = msgContent.getText().trim();//获取发送信息内容
String postPeople = onlinePeople.getSelectedItem();//获取被发送的用户
String postMsg = nickname + “!@~‘“ + postPeople + “!@~‘“ + content;//整合发送出去的数据
if (postPeople == null || “null“.equals(postPeople) || “所有人“.equals(postPeople)) {//判断发送用户为私聊或群发
this.msgShow.setText(msgShow.getText() + nickname + “群发:“ + content + “\n“);
} else {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5491 2008-12-08 21:05 java聊天程序\ChatClient.java
文件 2689 2008-12-08 20:44 java聊天程序\ChatServer.java
文件 5350 2008-12-08 22:45 java聊天程序\Client.java
文件 1781 2008-12-08 21:07 java聊天程序\Login.java
文件 2081 2008-12-08 21:13 java聊天程序\Receive.java
目录 0 2008-12-16 20:36 java聊天程序
----------- --------- ---------- ----- ----
17392 6
- 上一篇:《java程序设计》书附源代码
- 下一篇:用Java编写的扫雷游戏源代码
评论
共有 条评论