💕💕💕大家好,这是作业侠系列之Java实现模拟聊天程序,还是那句话,大家不想cv或者cv了跑不起来都可以来找我要源码。有任何问题有可以私聊我,大家觉得还行的话,期待你们的三连,这也是我创作的最大动力💕💕💕
往期源码回顾: 【Java】实现绘图板(完整版) 【C++】图书管理系统(完整板) 【Java】实现计算器(完整版) 【Python】实现爬虫,爬取天气情况并进行分析(完整版) 【Java】实现记事本(完整版) 【Java】实现多线程计算阶乘和(完整版)
上述源码我的大家有需要可自取,大家不想cv或者cv了跑不起来都可以来找我要源码。
设计思路:
(1)使用顶层容器JFrame。 (2)设置功能菜单并通过BorderLayout进行边框布局管理。 (3)设置相应按钮与文件编辑区。 (4)进行相应事件处理。 通过Socket网络访问方法,实现网络程序的设计。
运行后分为客户端和服务端 运行结果如下:
实现代码: 客户端UI:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientUI {
private JTextArea chatArea;
private JTextField messageField;
private Client client;
private JTextField adressField;
public ClientUI() {
JFrame frame = new JFrame("客户端");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setLayout(new BorderLayout());
chatArea = new JTextArea();
chatArea.setFont(new Font("微软雅黑",Font.BOLD,30));
frame.add(chatArea,BorderLayout.CENTER);
JPanel jPanel = new JPanel();
messageField = new JTextField();
messageField.setColumns(20);
messageField.setFont(new Font("微软雅黑",Font.BOLD,15));
JButton sentBtn = new JButton("发送");
sentBtn.addActionListener((e)->{
String msg = messageField.getText();
if(msg.equals("")) {
}
else {
chatArea.append("客户端: "+msg+'\n');
client.sendMessage(msg);
messageField.setText("");
}
});
jPanel.add(messageField);
jPanel.add(sentBtn);
frame.add(jPanel,BorderLayout.SOUTH);
JPanel northPan = new JPanel();
northPan.add(new JLabel("连接: "));
adressField = new JTextField();
adressField.setColumns(20);
JButton startBtn = new JButton("连接");
startBtn.addActionListener((e)->{
startConnect();
});
northPan.add(adressField);
northPan.add(startBtn);
frame.add(northPan,BorderLayout.NORTH);
frame.setVisible(true);
}
public void startConnect() {
if(adressField.getText().equals("")) {
adressField.setText("连接失败,请重输入");
return;
}
String address = adressField.getText();
String[] strs=address.split(":");
String host=strs[0];
int port=Integer.parseInt(strs[1]);
client = new Client(host,port,(msg)-> {
chatArea.append("服务端: "+msg+'\n');
});
}
public static void main(String[] args) {
new ClientUI();
}
}
客户端类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Client {
private Socket socket;
private InputStream in;
private OutputStream out;
private PrintWriter writer;
private BufferedReader bufferedReader;
private MessageAble messageAble;
public Client(String host,int port,MessageAble messageAble) {
this.messageAble = messageAble;
try {
socket = new Socket(host,port);
in = socket.getInputStream();
out = socket.getOutputStream();
writer = new PrintWriter(out);
bufferedReader = new BufferedReader(new InputStreamReader(in));
reciveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String msg) {
writer.println(msg);
writer.flush();
}
private void reciveMessage() {
new Thread(() -> {
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
messageAble.dealMessage(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
public void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端UI:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ServerUI {
private JTextArea chatArea;
private JTextField messageField;
private Server server;
private JTextField portField;
public ServerUI() {
JFrame frame = new JFrame("服务器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setLayout(new BorderLayout());
chatArea = new JTextArea();
chatArea.setFont(new Font("微软雅黑",Font.BOLD,30));
frame.add(chatArea,BorderLayout.CENTER);
JPanel jPanel = new JPanel();
messageField = new JTextField();
messageField.setColumns(20);
messageField.setFont(new Font("微软雅黑",Font.BOLD,15));
JButton sentBtn = new JButton("发送");
sentBtn.addActionListener((e)->{
String msg = messageField.getText();
if(msg.equals("")) {
}
else{
chatArea.append("服务端: "+msg+'\n');
server.sendMessage(msg);
messageField.setText("");
}
});
jPanel.add(messageField);
jPanel.add(sentBtn);
frame.add(jPanel,BorderLayout.SOUTH);
JPanel northPan = new JPanel();
northPan.add(new JLabel("监听端口"));
portField = new JTextField();
portField.setColumns(20);
JButton startBtn = new JButton("监听");
startBtn.addActionListener((e)->{
startServer();
});
northPan.add(portField);
northPan.add(startBtn);
frame.add(northPan,BorderLayout.NORTH);
frame.setVisible(true);
}
public void startServer() {
if(portField.getText().equals("")) {
portField.setText("监听失败,请重试");
return ;
}
int port =Integer.parseInt(portField.getText());
server = new Server(port,(msg)-> {
chatArea.append("客户端: "+msg+'\n');
});
}
public static void main(String[] args) {
new ServerUI();
}
}
服务端类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private InputStream in;
private OutputStream out;
private PrintWriter writer;
private BufferedReader bufferedReader;
private MessageAble messageAble;
public Server(int port,MessageAble messageAble) {
this.messageAble=messageAble;
try {
serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
in = socket.getInputStream();
out = socket.getOutputStream();
writer = new PrintWriter(out);
bufferedReader = new BufferedReader(new InputStreamReader(in));
reciveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String msg) {
writer.println(msg);
writer.flush();
}
private void reciveMessage() {
new Thread(() -> {
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
messageAble.dealMessage(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
public void close() {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
好了,今天就到这里吧,喜欢记得三连哦!
|