一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JavaGUI模仿QQ聊天功能完整版

JavaGUI模仿QQ聊天功能完整版

2021-09-26 00:58KillerCodes Java教程

這篇文章主要為大家詳細介紹了JavaGUI模仿QQ聊天功能完整版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JavaGUI模仿QQ聊天功能完整代碼,供大家參考,具體內容如下

JavaGUI模仿QQ聊天功能完整版

ClientForm代碼:

?
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package GUISocket.chat.Client;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
 
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class ClientForm extends JFrame {
 
 private JPanel contentPane;
 DefaultListModel<String> itemUsers;
 private JTextField textIP;
 private JTextField textPort;
 public JTextField textUser;
 public JTextArea textLog;
 public JList listUser;
 public JTextArea textSend ;
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     ClientForm frame = new ClientForm();
     frame.setVisible(true);
     ClientMG.getClientMG().setClientForm(frame);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 public ClientForm() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 589, 607);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel label = new JLabel("配置信息");
  label.setBounds(10, 10, 54, 15);
  contentPane.add(label);
  
  JLabel lblIp = new JLabel("IP");
  lblIp.setBounds(10, 35, 27, 15);
  contentPane.add(lblIp);
  
  textIP = new JTextField();
  textIP.setText("192.168.1.2");
  textIP.setBounds(33, 35, 92, 21);
  contentPane.add(textIP);
  textIP.setColumns(10);
  
  JLabel label_1 = new JLabel("端口");
  label_1.setBounds(137, 35, 38, 15);
  contentPane.add(label_1);
  
  textPort = new JTextField();
  textPort.setText("8900");
  textPort.setBounds(168, 32, 66, 21);
  contentPane.add(textPort);
  textPort.setColumns(10);
  
  JLabel label_2 = new JLabel("用戶名");
  label_2.setBounds(255, 38, 54, 15);
  contentPane.add(label_2);
  
  textUser = new JTextField();
  textUser.setBounds(302, 35, 66, 21);
  contentPane.add(textUser);
  textUser.setColumns(10);
  
  JButton LOGIN = new JButton("登錄");
  LOGIN.setBounds(395, 34, 66, 23);
  contentPane.add(LOGIN);
  
  JButton btnClose = new JButton("關閉");
  btnClose.addActionListener(new BtnCloseActionListener());
  btnClose.setBounds(480, 31, 71, 23);
  contentPane.add(btnClose);
  
  JPanel panel = new JPanel();
  panel.setBounds(0, 10, 573, 61);
  contentPane.add(panel);
  panel.setLayout(null);
  
  JPanel panel_1 = new JPanel();
  panel_1.setBounds(0, 81, 573, 369);
  contentPane.add(panel_1);
  panel_1.setLayout(null);
  
  JLabel label_3 = new JLabel("聊天記錄");
  label_3.setBounds(10, 10, 54, 15);
  panel_1.add(label_3);
  
  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  scrollPane.setBounds(20, 35, 323, 324);
  panel_1.add(scrollPane);
  
  textLog = new JTextArea();
  textLog.setWrapStyleWord(true);
  textLog.setLineWrap(true);
  scrollPane.setViewportView(textLog);
  
  JLabel label_4 = new JLabel("在線用戶");
  label_4.setBounds(351, 10, 54, 15);
  panel_1.add(label_4);
  
  JScrollPane scrollPane_1 = new JScrollPane();
  scrollPane_1.setBounds(353, 35, 210, 324);
  panel_1.add(scrollPane_1);
  
  
  this.itemUsers=new DefaultListModel<String>();
  this.listUser=new JList(itemUsers);
  scrollPane_1.setViewportView(this.listUser);
  
  
  JPanel panel_2 = new JPanel();
  panel_2.setBounds(10, 449, 553, 119);
  contentPane.add(panel_2);
  panel_2.setLayout(null);
  
  JLabel label_5 = new JLabel("操作");
  label_5.setBounds(10, 10, 54, 15);
  panel_2.add(label_5);
  
  JScrollPane scrollPane_2 = new JScrollPane();
  scrollPane_2.setBounds(10, 22, 533, 64);
  panel_2.add(scrollPane_2);
  
  textSend = new JTextArea();
  textSend.setWrapStyleWord(true);
  textSend.setLineWrap(true);
  scrollPane_2.setViewportView(textSend);
  
  JButton button_1 = new JButton("群發");
  button_1.addActionListener(new Button_1ActionListener());
  button_1.setBounds(307, 86, 93, 23);
  panel_2.add(button_1);
  
  JButton sendMG = new JButton("發送");
  sendMG.addActionListener(new SendMGActionListener());
  sendMG.setBounds(432, 86, 93, 23);
  panel_2.add(sendMG);
  
  LOGIN.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    //連接服務器user
    String IP=textIP.getText().trim();
    int port=Integer.parseInt(textPort.getText().trim());
    String user=textUser.getText().trim();
    
    if(ClientMG.getClientMG().Connect(IP,port,user)) {
     ClientMG.getClientMG().setLogTxt("已經連接到服務器");
     
    }
    else {
     ClientMG.getClientMG().setLogTxt("連接服務器失敗");
    }
   }
  });
 }
 private class SendMGActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   
   //發送信息
   //1.獲取選擇的用戶名稱
   //2.發送給服務器端含有接收用戶信息的交互協議串
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String RecName=listUser.getSelectedValue().toString();
   String MSGinfo=textSend.getText().trim();
   
   String sMsg="MSG|"+SenderName+"|"+RecName+"|"+MSGinfo;
   ClientMG.getClientMG().getClientThd().sendMsg(sMsg);
   //將消息內容顯示到聊天記錄中
   //[發送者]
   //消息內容
   //清空發送消息框
   ClientMG.getClientMG().setLogTxt("[我]:");
   ClientMG.getClientMG().setLogTxt(MSGinfo);
   
   textSend.setText("");
  }
 }
 private class Button_1ActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //群發信息
   //1.獲取選擇的用戶名稱
   //2.發送給服務器端含有接收用戶信息的交互協議串
   //發送到服務器,MSG|SenderName|RecName|MSGInfo
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String RecName="ALL";
   String MSGinfo=textSend.getText().trim();
   
   String sMsg="MSG|"+SenderName+"|"+RecName+"|"+MSGinfo;
   ClientMG.getClientMG().getClientThd().sendMsg(sMsg);
   //將消息內容顯示到聊天記錄中
   //[發送者]
   //消息內容
   //清空發送消息框
   ClientMG.getClientMG().setLogTxt("[我]:");
   ClientMG.getClientMG().setLogTxt(MSGinfo);
   
   textSend.setText("");
  }
 }
 private class BtnCloseActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //向服務器發送線下信息,OFFLINE|username
   String SenderName=ClientMG.getClientMG().getClientThd().getName();
   String str="OFFLINE|"+SenderName;
   ClientMG.getClientMG().getClientThd().sendMsg(str);
   
   //清空在線用戶列表
   ClientMG.getClientMG().clearItem();
   //消息記錄中顯示斷開連接
   ClientMG.getClientMG().setLogTxt("已經斷開連接");
  }
 }
 
 
}

ClientMG代碼:

?
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
package GUISocket.chat.Client;
 
import java.net.Socket;
 
public class ClientMG {
 private static final ClientMG clientmg=new ClientMG();
 private ClientMG() {}
 public static ClientMG getClientMG() {
  return clientmg;
 }
 
 private ClientForm clientWin;
 public void setClientForm(ClientForm c) {
  clientWin=c;
 }
 
 public void setLogTxt(String str) {
  clientWin.textLog.append(str+"\r\n"); 
 }
 
 public void addItem(String user) {
  
  clientWin.itemUsers.addElement(user);
 }
 
 public void addItems(String[] users) {
  for(int i=0;i<users.length;i++) {
   clientWin.itemUsers.addElement(users[i]);
  }
 
 }
 //所有用戶列表清空
 public void clearItem() {
  clientWin.itemUsers.clear();
 }
 //刪除一個用戶
 public void removeItem(String str) {
  clientWin.itemUsers.removeElement(str);
 }
 
 SocketThread sthd;
 public boolean Connect(String IP,int port,String user) {
  Socket socket=null;
  try {
   socket=new Socket(IP,port);
   sthd=new SocketThread(socket, user);
   sthd.start();
   return true;
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   return false;
  }
 }
 
 public SocketThread getClientThd() {
  return sthd;
 }
}

SocketThread代碼:

?
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
package GUISocket.chat.Client;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
 
public class SocketThread extends Thread{
 BufferedReader br=null;
 PrintWriter pw=null;
 Socket socket=null;
 
 public SocketThread(Socket socket,String user){
  super(user);//登錄時用的用戶名
  this.socket=socket;
 }
 
 public void run() {
  try {
   br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
   pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")));
   String sLOGIN="LOGIN|"+this.getName();
   sendMsg(sLOGIN);
   
   String str="";
   while((str=br.readLine())!=null) {
    String[] commands=str.split("\\|");
    if(commands[0].equals("USERLISTS")) {//USERLISTS|user1_user2_user3
     String[] sUsers=commands[1].split("\\_");
     ClientMG.getClientMG().addItems(sUsers);
    }
    else if(commands[0].equals("ADD")) {//ADD|UserName
     String sNewUser=commands[1];
     ClientMG.getClientMG().addItem(sNewUser);
    }
    else if(commands[0].equals("MSG")) {//格式 MSG|SenderName|MSGinfo
     String SenderName=commands[1];
     String MSGinfo=commands[2];
     //將消息內容顯示到聊天記錄中
     //[發送者]
     //消息內容
     ClientMG.getClientMG().setLogTxt("["+SenderName+"]");
     ClientMG.getClientMG().setLogTxt(MSGinfo);
    }
    else if(commands[0].equals("DEL")) {
     //3.處理下線用戶信息,DEL|username
     //刪除用戶列表中的username
     String sUser=commands[1];
     ClientMG.getClientMG().removeItem(sUser);
     ClientMG.getClientMG().setLogTxt(sUser+"下線了。");
    }
    else if(commands[0].equals("CLOSE")) { //CLOSE
     //1.處理CLOSE命令,界面顯示服務器關閉
     //2。清空在線用戶列表
     ClientMG.getClientMG().clearItem();
     ClientMG.getClientMG().setLogTxt("服務器關閉");
     //3.關閉ClientChat
     break;
    }
    
    //ClientMG.getClientMG().setLogTxt(str);
    
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }finally {
   try {
    if(pw!=null)
     pw.close();
    if(br!=null)
     br.close();
    if(socket!=null)
     socket.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
 }
 public void sendMsg(String str) {
  pw.println(str);
  pw.flush();
 }
}

ServerForm代碼:

?
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
package GUISocket.chat.Server;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
 
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
 
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class ServerForm extends JFrame {
 /**
  *
  */
 
 private JPanel contentPane;
 public JTextArea textLog;
 private JTextField textPort;
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     ServerForm frame = new ServerForm();
     frame.setVisible(true);
     ServerMG.getServerMG().setServerForm(frame);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 
 public ServerForm() {
  setTitle("多人聊天服務器");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 510, 566);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel lblNewLabel = new JLabel("配置信息");
  lblNewLabel.setBounds(20, 10, 54, 15);
  contentPane.add(lblNewLabel);
  
  JLabel label = new JLabel("端口:");
  label.setBounds(30, 34, 39, 15);
  contentPane.add(label);
  
  textPort = new JTextField();
  textPort.setText("8900");
  textPort.setBounds(65, 31, 66, 21);
  contentPane.add(textPort);
  textPort.setColumns(10);
  
  JButton btnStart = new JButton("開啟服務");
  btnStart.addActionListener(new BtnStartActionListener());
  btnStart.setBounds(180, 30, 93, 23);
  contentPane.add(btnStart);
  
  JButton btnClose = new JButton("關閉服務");
  btnClose.addActionListener(new BtnCloseActionListener());
  btnClose.setBounds(325, 30, 93, 23);
  contentPane.add(btnClose);
  
  JPanel panel = new JPanel();
  panel.setBounds(10, 10, 474, 54);
  contentPane.add(panel);
  panel.setLayout(null);
  
  JLabel label_1 = new JLabel("消息記錄");
  label_1.setBounds(10, 94, 54, 15);
  contentPane.add(label_1);
  
  JPanel panel_1 = new JPanel();
  panel_1.setBounds(0, 81, 474, 436);
  contentPane.add(panel_1);
  panel_1.setLayout(null);
  
  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(10, 41, 464, 368);
  panel_1.add(scrollPane);
  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  
  textLog = new JTextArea();
  textLog.setLineWrap(true);
  textLog.setWrapStyleWord(true);
  scrollPane.setViewportView(textLog);
  
  
  
 }
 private class BtnCloseActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   
  }
 }
 private class BtnStartActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   //開啟服務
   
   
   int port=Integer.parseInt(textPort.getText().trim());
   
   if(ServerMG.getServerMG().CreateServer(port)) {
 
    ServerMG.getServerMG().setLogTxt("服務器開啟...");
   }
   else {
    ServerMG.getServerMG().setLogTxt("服務器開啟失敗...");
   }
  }
 }
}

ServerListener代碼:

?
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
package GUISocket.chat.Server;
 
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerListener extends Thread{
 Socket socket=null;
 ServerSocket server=null;
 public ServerListener(ServerSocket server) {
  this.server=server;
 }
 public void run() {
  try {
   while(true) {
    socket=server.accept();
    ServerMG.getServerMG().setLogTxt("客戶端: "+socket); 
    new SocketThread(socket).start();
   }
   
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 
}

ServerMG代碼:

?
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package GUISocket.chat.Server;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
 
import javax.swing.JTextArea;
 
public class ServerMG {
 private static final ServerMG servermg=new ServerMG();
 private ServerMG() {}
 public static ServerMG getServerMG() {
  return servermg;
  
 }
 
 //主界面的操作
 private ServerForm serverWin;
 
 //將窗體對象注冊到管理類當中
 public void setServerForm(ServerForm s) {
  serverWin=s;
 }
 //設置主界面
 public void setLogTxt(String str) {
  serverWin.textLog.append(str+"\r\n");
 }
 
 private ServerSocket server;
 public boolean CreateServer(int port) {
  try {  
   server=new ServerSocket(port);
   new ServerListener(server).start();
   return true;
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   return false;
  }
 }
 
 public void CloseServer() {
  try {
   
   //1.向所有在線用戶發送關閉服務器的信息,CLOSE
   sendClosetoAll();
   //2.遍歷Arraylist將其中的每一個ServerChat關閉
   closeAllThread();
   //3.ArrayList要清空
   clearList();
   //4.關閉ServerListener
   //5.關閉ServerSocket
   server.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 
 //ArrayList操作
 ArrayList<SocketThread> a1OnlineList=new ArrayList<>();//存放所有和
 public synchronized void addList(SocketThread sc) {
  //限制重名
  a1OnlineList.add(sc);
 }
 
 public void clearList() {
  a1OnlineList.clear();
 }
 
 public synchronized void removeList(SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(s.equals(sc)) {
    a1OnlineList.remove(sc);
    break;
   }
  }
 }
 
 //信息的管理
 public void getOnlineNames(SocketThread sc) {
  //非第1次登錄時,得到所有的在線用戶
  if(a1OnlineList.size()>0) {
   String sUsers="";//給客戶端,USERLISTS|user1_user2_user3
   for(int i=0;i<a1OnlineList.size();i++) {
    SocketThread s=a1OnlineList.get(i);
    sUsers+=s.getName()+"_";
   }
   sc.sendMsg("USERLISTS|"+sUsers);
  }
  
 }
 public void sendNewUsertoAll(SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.sendMsg("ADD|"+sc.getName());
  }
 }
 //通過Mame用戶名查找目標
 public SocketThread getSocketThreadByName(String sName) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(s.getName().equals(sName)) {
    return s;
   }
  }
  return null;
 }
 
 //發送給所有人,但是要排除自身
 public void sendMsgtoAll(String sMsg,SocketThread sc) {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(!s.equals(sc)) {
    s.sendMsg(sMsg);
   }
  }
 }
 
 public void sendOfflieUsertoAll(SocketThread sc) {
  //向所有的其他在線用戶發送用戶下線信息,DE|username
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   if(!s.equals(sc)) {
    s.sendMsg("DEL|"+sc.getName());
   }
  }
 }
 
 public void sendClosetoAll() {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.sendMsg("CLOSE");
  }
 }
 
 public void closeAllThread() {
  for(int i=0;i<a1OnlineList.size();i++) {
   SocketThread s=a1OnlineList.get(i);
   s.closeChat();
  }
 }
 
 
}

SocketThread代碼:

?
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
package GUISocket.chat.Server;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
 
public class SocketThread extends Thread{
 BufferedReader br=null;
 PrintWriter pw=null;
 Socket socket=null;
 public SocketThread(Socket socket) {
  this.socket = socket;
 }
 
 public void run() {
  try {
   br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
   pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")));
   String str="";
   while((str=br.readLine())!=null) {//循環響應客戶的發送信息//接受客戶端發過來的信息
    String [] commands=str.split("\\|");
    
    if(commands[0].equals("LOGIN")) {//解析登錄請求,格式,LOGIN|UserName
     String sUSER=commands[1];
     this.setName(sUSER);//將用戶名信息放入Threadname中
     //1.得到所有在線用戶信息名稱,發回客戶端:USERLISTS|user1_user2_user3
     ServerMG.getServerMG().getOnlineNames(this);
     //2.將當前登錄用戶的信息(用戶名),發送給已經在線的其他用戶,ADD|userName
     
     ServerMG.getServerMG().sendNewUsertoAll(this);
     //3.將當前登錄的Socket信息放入ArrayList中
     ServerMG.getServerMG().addList(this);
     
    }
    else if(commands[0].equals("MSG")) {//格式:MSG|SenderName|RecName|MSGoinfo
     String SenderName=commands[1];
     String RecName=commands[2];
     String MSGinfo=commands[3];
     //群聊
     if(RecName.equals("ALL")) {
      String sMsg="MSG!"+SenderName+"|"+MSGinfo;//格式:MSG|SenderName|MSGinfo
      ServerMG.getServerMG().sendMsgtoAll(sMsg,this);
      ServerMG.getServerMG().setLogTxt(SenderName+"發送信息["+MSGinfo+"]到所有人。");
     }
     //私聊
      else {
       //通過RecName用戶名查找,找到目標SocketThread
       SocketThread sc=ServerMG.getServerMG().getSocketThreadByName(RecName);
       if(sc!=null) {
        //目標對象發送信息,MSG|SenderName|MSGinfo
        String sMsg="MSG!"+SenderName+"|"+MSGinfo;
        sc.sendMsg(sMsg);
        
        //寫入信息日志
        ServerMG.getServerMG().setLogTxt(SenderName+"發送信息["+MSGinfo+"]到"+RecName);
      }
      
     }
    }
    
    else if(commands[0].equals("OFFLINE")) {
     //1.創建OFFLINE
     String sUser=commands[1];//獲取下線的用戶名
     //2.向所有的其他在線用戶發送用戶下線信息,DEL|username
     ServerMG.getServerMG().sendOfflieUsertoAll(this);
     //3.清除ArrayList中的當前用戶信息()socketChat
     ServerMG.getServerMG().removeList(this);
     
     //用戶下線需后要退出監聽用的while循環
     ServerMG.getServerMG().setLogTxt(sUser+"下線了");
     break;
    }
    
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   try {
    if(pw!=null)
     pw.close();
    if(br!=null)
     br.close();
    if(socket!=null)
     socket.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
 }
 public void closeChat() {
  try {
   if(pw!=null)
    pw.close();
   if(br!=null)
    br.close();
   if(socket!=null)
    socket.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public void sendMsg(String str) {
  pw.println(str);
  pw.flush();
 }
}

運行結果如下:

JavaGUI模仿QQ聊天功能完整版

JavaGUI模仿QQ聊天功能完整版

缺點:只能發單行信息,如發多行,需用到base4.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/aaassslll147/article/details/106254714

延伸 · 閱讀

精彩推薦
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經有好久沒有升過級了。升級完畢重啟之后,突然發現好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程Java實現搶紅包功能

    Java實現搶紅包功能

    這篇文章主要為大家詳細介紹了Java實現搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發現了對于集合操作轉換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關于Java8中S...

    阿杜7472021-02-04
  • Java教程xml與Java對象的轉換詳解

    xml與Java對象的轉換詳解

    這篇文章主要介紹了xml與Java對象的轉換詳解的相關資料,需要的朋友可以參考下...

    Java教程網2942020-09-17
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java BufferWriter寫文件寫不進去或缺失數據的解決

    Java BufferWriter寫文件寫不進去或缺失數據的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數據的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關于小米推送Java代碼,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩中求8032021-07-12
主站蜘蛛池模板: 日本xxx片免费高清在线 | 無码一区中文字幕少妇熟女网站 | 午夜神器老司机高清无码 | 国产91一区二区在线播放不卡 | 手机在线观看伦理片 | 国产成人久久精品推最新 | 欧美另类bbbxxxxx另类 | 国产馆在线观看免费的 | 国产亚洲女在线线精品 | 久久亚洲电影www电影网 | 微福利92合集 | 亚洲成人网导航 | 草溜短视频 | 精品国产一区二区三区久久久蜜臀 | 91在线精品老司机免费播放 | 无限在线观看视频大全免费高清 | 国产黄色大片网站 | 欧美日韩亚洲第一区在线 | 超h高h肉h文武侠 | 国产短视频精品一区二区三区 | 小早川怜子息梦精在线播放 | 国产精品免费久久久久影院 | 午夜精品在线 | 白丝美女用胸伺候主人 | 国产精品1区2区 | 亚洲狠狠婷婷综合久久蜜桃 | a级免费观看| 驯服有夫之妇HD中字日本 | 亚洲老头与老太hd | 日本不卡视频免费的 | 人人干国产 | 久99久热只有精品国产99 | 国产真实乱子伦xxxxchina | 黑人巨大videosjapan高清 黑人好大 | 鬼吹灯天星术在线高清观看 | 午夜久久影院 | 俄罗斯处女| 国产人妖ts在线视频网 | 久久中文字幕亚洲 | 欧美视频久久 | 91亚洲成人|