本文实例为大家分享了java实现简单聊天室的具体代码,供大家参考,具体内容如下
先整理下思路:
1.创建一个通信服务端,传入端口号和相关的流后等待客户端连接,并初始化图形界面。
2.创建一个jframe,用于写出聊天的界面,这里界面名称由其他类传入。
3.把客户端创建的方法写进jframe(当然这里很粗糙的方法)
4.设置按钮的监听事件,发送消息和离线功能
首先创建一个服务端的类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import java.io.ioexception;
import java.net.serversocket;
import java.net.socket;
import com.jero.jframe.chatsocketjframe;
//创建服务端
public class chatserversocket{
//构造初始化方法
public chatserversocket(string name, int port) throws ioexception{
//创建socket服务连接对象
serversocket serversocket = new serversocket(port);
//等待客户端连接
socket client = serversocket.accept();
//初始化聊天界面
try {
new chatsocketjframe(name,client);
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
//关闭流
serversocket.close();
}
public static void main(string[] args) throws exception{
new chatserversocket( "服务端" , 8001 ); //启动服务端,指定网名和端口
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
package com.jero.jframe;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.inetaddress;
import java.net.socket;
import java.net.unknownhostexception;
import javax.swing.jframe;
import javax.swing.jpanel;
import javax.swing.jscrollpane;
import javax.swing.border.emptyborder;
import com.jero.socket.chatserversocket;
import javax.swing.jtextpane;
import javax.swing.jtextfield;
import javax.swing.jbutton;
import javax.swing.jtextarea;
public class chatsocketjframe extends jframe implements actionlistener {
private socket socket;
private string name;
private jbutton bt_send,bt_leave,bt_login;
private jtextarea textarea;
private jpanel contentpane;
private jtextfield textfield;
private printwriter cout;
/**
* launch the application.
* @throws exception
* @throws ioexception
* @throws unknownhostexception
*/
public static void main(string[] args) throws unknownhostexception, ioexception, exception {
//实例化界面以及客户端
new chatsocketjframe( "客户端" , "127.0.0.1" , 8001 );
}
/**
* create the frame.
*/
public chatsocketjframe(string name, socket socket) throws exception, ioexception {
//聊天室界面
super ( "聊天室" +name+ " " +inetaddress.getlocalhost()+ " " +socket.getport());
setdefaultcloseoperation(jframe.exit_on_close);
setbounds( 100 , 100 , 733 , 519 );
this .textarea = new jtextarea();
this .textarea.seteditable( false );
this .getcontentpane().add( new jscrollpane( this .textarea));
jpanel panel = new jpanel();
this .getcontentpane().add(panel, "south" );
this .textfield = new jtextfield( 16 );
panel.add( this .textfield);
bt_send = new jbutton( "发送" );
panel.add(bt_send);
bt_send.addactionlistener( new actionlistener() {
@override
public void actionperformed(actionevent e) {
// todo auto-generated method stub
system.out.println( "按钮已点击" );
cout.println(name+ "说: " +textfield.gettext()+ "\n" );
textarea.append( "我说: " +textfield.gettext()+ "\n" );
textfield.settext( "" );
}
});
bt_leave = new jbutton( "离线" );
panel.add(bt_leave);
bt_leave.addactionlistener( new actionlistener() {
@override
public void actionperformed(actionevent e) {
// todo auto-generated method stub
textarea.append( "我离线" );
cout.println(name+ "离线\n" + "bye" );
bt_leave.setenabled( false );
bt_send.setenabled( false );
}
});
this .setvisible( true );
//开始创建通信
this .socket = socket;
this .name = name;
//获得socket的输出流并立即刷新
this .cout = new printwriter(socket.getoutputstream(), true );
//把网名传给对方
this .cout.println(name);
//获得socket对象的输入流,创建字符输入流
bufferedreader buff = new bufferedreader( new inputstreamreader(socket.getinputstream()));
//接受对方的网名
string aline = buff.readline();
//显示对方发送的内容
textarea.append( "连接" +aline+ "成功!!\n" );
aline = buff.readline();
//如果内容不为空并且对方不离线,则相互传送消息
while (aline!= null &&!aline.equals( "bye" )){
textarea.append(aline+ "\r\n" );
aline = buff.readline();
}
//当对方离线时,关闭所有的流
buff.close();
cout.close();
socket.close();
//把按钮设置为不可操作
bt_send.setenabled( false );
bt_leave.setenabled( false );
}
/**
* @wbp.parser.constructor
*/
public chatsocketjframe(string name,string host, int port) throws unknownhostexception, ioexception, exception{
//创建客户端
this (name, new socket(host,port));
}
@override
public void actionperformed(actionevent e) {
// todo auto-generated method stub
//可以把动作监听写进此函数,这个就不演示了
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/JEROCHAN/article/details/78226970