【汇智学堂】基于Swing TCP Socket的聊天程序
程序开发
2023-09-21 10:18:02
效果:
Client.java
/*** 服务器链接测试*/package com.huizhi;import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;public class Client {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubClientUi clientUi=new ClientUi();clientUi.chat();}
}class ClientUi extends JFrame {private JPanel jPanel;private JTextArea jTextArea;private JTextField jTextField;private JButton jButton;private static PrintWriter pw = null;private static BufferedReader br = null;private String stra;private boolean bl=false;public void chat() {jPanel = new JPanel(); //创建面板jTextField = new JTextField();jTextField.setColumns(20);jTextArea = new JTextArea();jTextArea.setColumns(40);jTextArea.setRows(40);jButton = new JButton("发送聊天内容"); //创建按钮jPanel.add(jTextField);jPanel.add(jButton); //面板中添加按钮jPanel.add(jTextArea);/*** 向JPanel添加FlowLayout布局管理器,* 将组件间的横向和纵向间隙都设置为20像素*/this.add(jPanel); //添加面板到容器this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(300,50, 800,800);//设置画布大小,this可以省略this.setTitle("雷雷的聊天程序");setVisible(true);try {Socket s = new Socket(InetAddress.getLocalHost(), 5500);pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));br = new BufferedReader(new InputStreamReader(s.getInputStream()));// 添加鼠标监听事件,当鼠标点击时触发jButton.addMouseListener(new MouseAdapter() {// 定义鼠标点击事件响应过程@Overridepublic void mouseClicked(MouseEvent e) {stra = jTextField.getText();//JTextArea使用append方法后,在后面添加rn即可自动换行jTextArea.append("客户端说:"+stra+"rn");pw.println(stra);pw.flush();jTextField.setText("");}});while (true) {String strb=br.readLine();//JTextArea使用append方法后,在后面添加rn即可自动换行jTextArea.append("服务器说:"+strb+"rn");}}catch (Exception e) {e.printStackTrace();}finally {try {br.close();pw.close();} catch (IOException e1) {e1.printStackTrace();}}}
}
Server.java
package com.huizhi;import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class Server {/*** @param args*/public static void main(String[] args) {ServerUi serverUi=new ServerUi();serverUi.chat();}
}class ServerUi extends JFrame {private JPanel jPanel;private JTextArea jTextArea;private JTextField jTextField;private JButton jButton;private String stra;private boolean bl=false;private static BufferedReader br=null;private static PrintWriter pw=null;private static ServerSocket ss;private static Socket s;public JFrame chat() {jPanel = new JPanel(); //创建面板jTextField = new JTextField();jTextField.setColumns(20);jTextArea = new JTextArea();jTextArea.setColumns(40);jTextArea.setRows(40);jButton = new JButton("发送聊天内容"); //创建按钮//设置LayoutjPanel.add(jTextArea);jPanel.add(jTextField);jPanel.add(jButton); //面板中添加按钮/*** 向JPanel添加FlowLayout布局管理器,* 将组件间的横向和纵向间隙都设置为20像素*/this.add(jPanel); //添加面板到容器this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(300,50, 800,800);//设置画布大小,this可以省略this.setTitle("服务器的聊天程序");setVisible(true);try {ss=new ServerSocket(5500);jTextArea.append("服务器正常启动。。。。"+"rn");s=ss.accept();//阻塞方法jTextArea.append("连接成功"+s.getRemoteSocketAddress()+"rn");br=new BufferedReader(new InputStreamReader(s.getInputStream()));pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));// 添加鼠标监听事件,当鼠标点击时触发jButton.addMouseListener(new MouseAdapter() {// 定义鼠标点击事件响应过程@Overridepublic void mouseClicked(MouseEvent e) {stra = jTextField.getText();//JTextArea使用append方法后,在后面添加rn即可自动换行jTextArea.append("服务端说:"+stra+"rn");pw.println(stra);pw.flush();jTextField.setText("");}});while(true){String string=br.readLine();jTextArea.append("客户端说:"+string+"rn");}} catch (Exception e1) {e1.printStackTrace();}try {pw.close();br.close();} catch (IOException e1) {//e1.printStackTrace();}return this;}
}
标签:
上一篇:
npm,cnpm安装实战(031)
下一篇:
相关文章
-
无相关信息