




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第javaGUI實現(xiàn)多人聊天功能本文實例為大家分享了javaGUI實現(xiàn)多人聊天的具體代碼,供大家參考,具體內(nèi)容如下
packagecom.ff.chat.chatserver.frame;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.EOFException;
importjava.io.IOException;
import.BindException;
import.ServerSocket;
import.Socket;
importjava.util.ArrayList;
importjava.util.Iterator;
publicclassServerFrameextendsJFrame{
JTextAreamsgArea;
ArrayListSocketsocketArrayList=newArrayList();
booleanserverFlag=true;//服務器啟動標志
StringBuffersb=newStringBuffer();
ServerSocketserverSocket;
//創(chuàng)建服務器端的顯示窗口
publicvoidcreatFrame(){
this.setTitle("聊天室-服務器端");
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//創(chuàng)建一個面板
JPaneljp=newJPanel(newBorderLayout());
//中間面板
JPanelcenterPanel=newJPanel();
msgArea=newJTextArea(30,40);
msgArea.setEditable(false);
JScrollPanejsp=newJScrollPane(msgArea);
centerPanel.add(jsp);
jp.add(centerPanel);
this.add(jp);
this.setVisible(true);
this.addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
intres=JOptionPane.showConfirmDialog(null,"確定要關閉服務器嗎?",
"操作提示",JOptionPane.OK_CANCEL_OPTION);
if(res==0){
try{
dispose();
serverSocket.close();
}catch(IOExceptionioException){
ioException.printStackTrace();
}
}
}
});
}
//啟動服務器,創(chuàng)建ServerSocket
publicvoidstartServer(){
try{
//創(chuàng)建ServerSocket
serverSocket=newServerSocket(9998);
System.out.println("等待客戶端連接");
//循環(huán)監(jiān)聽客戶端連接
while(serverFlag){
if(serverSocket.isClosed()){
serverFlag=false;
break;
}
Socketsocket=serverSocket.accept();
System.out.println("客戶端連接成功");
socketArrayList.add(socket);
//為每一個客戶端開啟一個線程,監(jiān)聽客戶端發(fā)來的消息
newServerThread(socket).start();
}
}catch(BindExceptionb){
b.printStackTrace();
System.out.println("服務器端口被占用");
System.exit(0);
}catch(IOExceptione){
e.printStackTrace();
System.out.println("服務器啟動失敗");
serverFlag=false;
}
}
//創(chuàng)建一個內(nèi)部類,開啟一個線程,接收消息
classServerThreadextendsThread{
Socketsocket;
DataInputStreamin;
DataOutputStreamout;
booleanclientFlag=true;
publicServerThread(Socketsocket){
this.socket=socket;
try{
this.in=newDataInputStream(socket.getInputStream());
}catch(IOExceptione){
e.printStackTrace();
}
}
@Override
publicvoidrun(){
//監(jiān)聽接收客戶端發(fā)送的消息
while(clientFlag){
if(socket.isClosed()){
break;
}
try{
Stringmsg=in.readUTF();//讀到客戶端發(fā)送的消息
sb.append(msg+"\n");
System.out.println(sb);
msgArea.setText(sb.toString());
//從服務器端向客戶端發(fā)送消息
if(socketArrayList.size()0){
IteratorSocketit=socketArrayList.iterator();
while(it.hasNext()){
Socketsoc=it.next();
if(soc.isClosed()){//當客戶端某個socket已經(jīng)為關閉狀態(tài),移除此socket
it.remove();
continue;
}
//客戶端socket如果沒有關閉,向客戶端發(fā)送消息
out=newDataOutputStream(soc.getOutputStream());
out.writeUTF(sb.toString());
out.flush();
}
}
}catch(EOFExceptionef){
System.out.println("ip為:"+socket.getInetAddress()+"的客戶端下線了");
clientFlag=false;
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
packagecom.ff.chat.chatserver.frame;
publicclassServerRun{
publicstaticvoidmain(String[]args){
ServerFrameserverFrame=newServerFrame();
serverFrame.creatFrame();
serverFrame.startServer();
}
}
注意:要在自己的電腦上運行多次客戶端需要勾選如下選項
1.登錄界面
packagecom.ff.chat.chatclient.frame;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.IOException;
import.Socket;
publicclassLoginFrameextendsJFrame{
JTextFieldaccountField=null;
//創(chuàng)建窗口
publicvoidcreatFrame(){
this.setTitle("聊天窗口");
this.setSize(400,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
/*
*創(chuàng)建一個4行1列的面板
**/
JPaneljp=newJPanel(newGridLayout(4,1));
//歡迎登陸面板
JPanelwelcomePanel=newJPanel();
JLabelwelcomLabel=newJLabel("歡迎登陸");
welcomePanel.add(welcomLabel);
//賬號面板
JPanelaccountPanel=newJPanel();
JLabelaccountLabel=newJLabel("賬號");
accountField=newJTextField(15);
accountPanel.add(accountLabel);
accountPanel.add(accountField);
//密碼面板
JPanelpasswordPanel=newJPanel();
JLabelpasswordLabel=newJLabel("密碼");
JPasswordFieldpasswordField=newJPasswordField(15);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
//登錄按鈕面板
JPanelbtnPanel=newJPanel();
JButtonloginBtn=newJButton("登錄");
JButtonregBtn=newJButton("注冊");
btnPanel.add(loginBtn);
btnPanel.add(regBtn);
jp.add(welcomePanel);
jp.add(accountPanel);
jp.add(passwordPanel);
jp.add(btnPanel);
this.add(jp);
this.setVisible(true);
//組件綁定事件監(jiān)聽
loginBtn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//獲得賬號密碼
Stringaccount=accountField.getText();
Stringpassword=newString(passwordField.getPassword());
if(account.length()==0){
JOptionPane.showMessageDialog(null,"賬號不能為空",
"操作提示",JOptionPane.WARNING_MESSAGE);
return;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 制定儲備糧輪換管理辦法
- 工藝安全智能化管理辦法
- 服務站管理暫行辦法條例
- 南陽市物業(yè)管理暫行辦法
- 展期貸款管理辦法銀監(jiān)局
- 銷售大區(qū)印章管理辦法
- 金融支付機構管理辦法
- 臨汾市培訓經(jīng)費管理辦法
- 松原市城市管理暫行辦法
- 防疫倉庫物資管理辦法
- 鉀礦項目投資可行性研究分析報告(2024-2030版)
- 新疆高校畢業(yè)生三支一扶計劃招募考試真題2025
- 2024年煤礦安全規(guī)程
- 成分輸血專題知識
- 高壓氧艙技師試題及答案
- 網(wǎng)絡安全等級保護等級測評方案模板
- 2025年1月國家開放大學專科《辦公室管理》期末紙質考試試題及答案
- 康師傅采購流程
- 2025年復合膜袋項目可行性研究報告
- 離職賠償協(xié)商協(xié)議書(2025年)
- 2024年度半導體生產(chǎn)工人勞動合同范本3篇
評論
0/150
提交評論