1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java网络编程-简易聊天室源码分享

Java网络编程-简易聊天室源码分享

时间:2020-09-24 06:24:51

相关推荐

Java网络编程-简易聊天室源码分享

6月8日更新,版本 v1.1

更新内容:

(1)应广大朋友要求,增加了滚动条

(2)同时修改了窗口的名称

(3)添加了一些注释

后续更新内容:考虑加一个群聊,目前好像是不行的,多开客户端会发生错误

代码放在下面了

同样需要先打开服务端,再打开客户端!

服务器端

import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .ServerSocket;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;/*** 服务器端窗口,先运行这个*/public class GUIServer {public static void main(String[] args) {// 创建窗体JFrame f = new JFrame("JAVA版简易聊天室-服务器端");f.setLayout(null);// 设置大小和位置f.setSize(400, 300);f.setLocation(100, 200);// 发送按钮JButton b = new JButton("发送");b.setBounds(290, 220, 80, 30);f.add(b);// 下侧聊天输入框JTextField tf = new JTextField();tf.setBounds(10, 220, 260, 30);f.add(tf);// 上侧聊天内容JTextArea ta = new JTextArea();// 设置为不可编辑ta.setEditable(false);// 文字比控件的宽度还长时会自动换行ta.setLineWrap(true);// 在单词边界换行,而不是粗暴的直接在字符边界换行ta.setWrapStyleWord(true);// 增加滚动条JScrollPane sp = new JScrollPane(ta);sp.setBounds(10, 10, 360, 200);sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);f.add(sp);f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);f.setVisible(true);try {// 设置服务器的端口为8888ServerSocket ss = new ServerSocket(8888);// 设置为接收模式Socket s = ss.accept();// 发送按钮的点击事件b.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取输入文本String text = tf.getText();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);ta.append(now +"\r\n我:" + text + "\r\n");ta.setCaretPosition(ta.getText().length());// 设置输入框为空tf.setText("");// 发送信息try {DataOutputStream dos = new DataOutputStream(s.getOutputStream());dos.writeUTF(text);} catch (IOException e1) {e1.printStackTrace();}}});// 接收信息线程new Thread() {@Overridepublic void run() {while (true) {try {// 获取其他用户的输入DataInputStream dis = new DataInputStream(s.getInputStream());String text = dis.readUTF();String ip = s.getInetAddress().getHostAddress();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);// 添加到页面上ta.append(now + "\r\n" + ip + ":" + text + "\r\n");ta.setCaretPosition(ta.getText().length());} catch (IOException e) {e.printStackTrace();}}}}.start();} catch (IOException e) {e.printStackTrace();}}}

客户机端

import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;/*** 客户端程序*/public class GUIClient {public static void main(String[] args) {// 创建窗体JFrame f = new JFrame("JAVA版简易聊天室-客户机端");f.setLayout(null);// 设置大小和位置f.setSize(400, 300);f.setLocation(100, 200);// 发送按钮JButton b = new JButton("发送");b.setBounds(290, 220, 80, 30);f.add(b);// 下侧聊天输入框JTextField tf = new JTextField();tf.setBounds(10, 220, 260, 30);f.add(tf);// 上侧聊天内容JTextArea ta = new JTextArea();// 设置为不可编辑ta.setEditable(false);// 文字比控件的宽度还长时会自动换行ta.setLineWrap(true);// 在单词边界换行,而不是粗暴的直接在字符边界换行ta.setWrapStyleWord(true);// 增加滚动条JScrollPane sp = new JScrollPane(ta);sp.setBounds(10, 10, 360, 200);sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);f.add(sp);f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);f.setVisible(true);try {// 设置连接服务器的地址为本机,端口为8888Socket s = new Socket("127.0.0.1", 8888);b.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取输入文本String text = tf.getText();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);ta.append(now +"\r\n我:" + text + "\r\n");// 设置输入框为空tf.setText("");// 发送信息try {DataOutputStream dos = new DataOutputStream(s.getOutputStream());dos.writeUTF(text);} catch (IOException e1) {e1.printStackTrace();}}});// 接收信息线程new Thread() {@Overridepublic void run() {while (true) {try {// 获取其他用户的输入DataInputStream dis = new DataInputStream(s.getInputStream());String text = dis.readUTF();String ip = s.getInetAddress().getHostAddress();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);// 添加到页面上ta.append(now + "\r\n" + ip + ":" + text + "\r\n");} catch (IOException e) {e.printStackTrace();}}}}.start();} catch (IOException e) {e.printStackTrace();}}}

原贴

简易的聊天小程序,在使用时请先开启服务器程序,再运行客户端程序

package socket;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .ServerSocket;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;/*** 服务器端窗口,先运行这个*/public class GUIServer {public static void main(String[] args) {// 创建窗体JFrame f = new JFrame("服务器");f.setLayout(null);// 设置大小和位置f.setSize(400, 300);f.setLocation(100, 200);JButton b = new JButton("发送");b.setBounds(290, 220, 80, 30);f.add(b);JTextField tf = new JTextField();tf.setBounds(10, 220, 260, 30);f.add(tf);JTextArea ta = new JTextArea();ta.setBounds(10, 10, 360, 200);f.add(ta);f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);f.setVisible(true);try {ServerSocket ss = new ServerSocket(8888);Socket s = ss.accept();b.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取输入文本String text = tf.getText();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);ta.append(now +"\r\n我:" + text + "\r\n");ta.setCaretPosition(ta.getText().length());// 设置输入框为空tf.setText("");// 发送信息try {DataOutputStream dos = new DataOutputStream(s.getOutputStream());dos.writeUTF(text);} catch (IOException e1) {e1.printStackTrace();}}});// 接收信息线程new Thread() {@Overridepublic void run() {while (true) {try {// 获取其他用户的输入DataInputStream dis = new DataInputStream(s.getInputStream());String text = dis.readUTF();String ip = s.getInetAddress().getHostAddress();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);// 添加到页面上ta.append(now + "\r\n" + ip + ":" + text + "\r\n");ta.setCaretPosition(ta.getText().length());} catch (IOException e) {e.printStackTrace();}}}}.start();} catch (IOException e) {e.printStackTrace();}}}

package socket;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;/*** 客户端程序*/public class GUIClient {public static void main(String[] args) {// 创建窗体JFrame f = new JFrame("客户端");f.setLayout(null);// 设置大小和位置f.setSize(400, 300);f.setLocation(100, 200);JButton b = new JButton("发送");b.setBounds(290, 220, 80, 30);f.add(b);JTextField tf = new JTextField();tf.setBounds(10, 220, 260, 30);f.add(tf);JTextArea ta = new JTextArea();ta.setBounds(10, 10, 360, 200);f.add(ta);f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);f.setVisible(true);try {Socket s = new Socket("127.0.0.1", 8888);b.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取输入文本String text = tf.getText();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);ta.append(now +"\r\n我:" + text + "\r\n");// 设置输入框为空tf.setText("");// 发送信息try {DataOutputStream dos = new DataOutputStream(s.getOutputStream());dos.writeUTF(text);} catch (IOException e1) {e1.printStackTrace();}}});// 接收信息线程new Thread() {@Overridepublic void run() {while (true) {try {// 获取其他用户的输入DataInputStream dis = new DataInputStream(s.getInputStream());String text = dis.readUTF();String ip = s.getInetAddress().getHostAddress();Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");String now = sdf.format(date);// 添加到页面上ta.append(now + "\r\n" + ip + ":" + text + "\r\n");} catch (IOException e) {e.printStackTrace();}}}}.start();} catch (IOException e) {e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。