




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第javaGUI實(shí)現(xiàn)多人聊天功能本文實(shí)例為大家分享了javaGUI實(shí)現(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;//服務(wù)器啟動(dòng)標(biāo)志
StringBuffersb=newStringBuffer();
ServerSocketserverSocket;
//創(chuàng)建服務(wù)器端的顯示窗口
publicvoidcreatFrame(){
this.setTitle("聊天室-服務(wù)器端");
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//創(chuàng)建一個(gè)面板
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,"確定要關(guān)閉服務(wù)器嗎?",
"操作提示",JOptionPane.OK_CANCEL_OPTION);
if(res==0){
try{
dispose();
serverSocket.close();
}catch(IOExceptionioException){
ioException.printStackTrace();
}
}
}
});
}
//啟動(dòng)服務(wù)器,創(chuàng)建ServerSocket
publicvoidstartServer(){
try{
//創(chuàng)建ServerSocket
serverSocket=newServerSocket(9998);
System.out.println("等待客戶端連接");
//循環(huán)監(jiān)聽(tīng)客戶端連接
while(serverFlag){
if(serverSocket.isClosed()){
serverFlag=false;
break;
}
Socketsocket=serverSocket.accept();
System.out.println("客戶端連接成功");
socketArrayList.add(socket);
//為每一個(gè)客戶端開(kāi)啟一個(gè)線程,監(jiān)聽(tīng)客戶端發(fā)來(lái)的消息
newServerThread(socket).start();
}
}catch(BindExceptionb){
b.printStackTrace();
System.out.println("服務(wù)器端口被占用");
System.exit(0);
}catch(IOExceptione){
e.printStackTrace();
System.out.println("服務(wù)器啟動(dòng)失敗");
serverFlag=false;
}
}
//創(chuàng)建一個(gè)內(nèi)部類,開(kāi)啟一個(gè)線程,接收消息
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)聽(tīng)接收客戶端發(fā)送的消息
while(clientFlag){
if(socket.isClosed()){
break;
}
try{
Stringmsg=in.readUTF();//讀到客戶端發(fā)送的消息
sb.append(msg+"\n");
System.out.println(sb);
msgArea.setText(sb.toString());
//從服務(wù)器端向客戶端發(fā)送消息
if(socketArrayList.size()0){
IteratorSocketit=socketArrayList.iterator();
while(it.hasNext()){
Socketsoc=it.next();
if(soc.isClosed()){//當(dāng)客戶端某個(gè)socket已經(jīng)為關(guān)閉狀態(tài),移除此socket
it.remove();
continue;
}
//客戶端socket如果沒(méi)有關(guān)閉,向客戶端發(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();
}
}
注意:要在自己的電腦上運(yùn)行多次客戶端需要勾選如下選項(xiàng)
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)建一個(gè)4行1列的面板
**/
JPaneljp=newJPanel(newGridLayout(4,1));
//歡迎登陸面板
JPanelwelcomePanel=newJPanel();
JLabelwelcomLabel=newJLabel("歡迎登陸");
welcomePanel.add(welcomLabel);
//賬號(hào)面板
JPanelaccountPanel=newJPanel();
JLabelaccountLabel=newJLabel("賬號(hào)");
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("注冊(cè)");
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)聽(tīng)
loginBtn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//獲得賬號(hào)密碼
Stringaccount=accountField.getText();
Stringpassword=newString(passwordField.getPassword());
if(account.length()==0){
JOptionPane.showMessageDialog(null,"賬號(hào)不能為空",
"操作提示",JOptionPane.WARNING_MESSAGE);
return;
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中外購(gòu)物合同范例
- 價(jià)格保密協(xié)議合同范例
- 360勞務(wù)合同標(biāo)準(zhǔn)文本
- 產(chǎn)品長(zhǎng)期合作合同范例
- 兒童探視教育輔導(dǎo)與保障合同書(shū)
- 地鐵工程合同糾紛仲裁調(diào)解協(xié)議
- 博士科研崗位勞務(wù)合同及成果轉(zhuǎn)化協(xié)議
- 抖音賬號(hào)粉絲數(shù)據(jù)遷移及權(quán)益維護(hù)合同
- 企業(yè)合規(guī)培訓(xùn)及常年法律顧問(wèn)服務(wù)合同
- 個(gè)性化智能零售庫(kù)存管理系統(tǒng)定制開(kāi)發(fā)服務(wù)合同
- 蒸壓加氣混凝土墻板
- 豆腐乳市場(chǎng)洞察報(bào)告
- 遼寧省協(xié)作校2024-2025學(xué)年高二英語(yǔ)下學(xué)期期末考試試題
- 物業(yè)消防安全管理培訓(xùn)【共54張課件】
- JBT 12530.1-2015 塑料焊縫無(wú)損檢測(cè)方法 第1部分:通.用要求
- 墳?zāi)官?zèng)與合同范本
- Module 2 Unit 1 She's listening to the radio(教案)-2023-2024學(xué)年外研版(一起)英語(yǔ)二年級(jí)下冊(cè)
- Unit3 Lesson16 An Email Is Fast(教案 )冀教版(三起)英語(yǔ)五年級(jí)下冊(cè)
- 城市沿街建筑立面改造工程施工組織設(shè)計(jì)
- 三年級(jí)下冊(cè)美術(shù)教案- 第16課 生活與藝術(shù)中的花 ▏人美版
- 自愿參加活動(dòng)免責(zé)申明
評(píng)論
0/150
提交評(píng)論