




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第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;//服務(wù)器啟動標(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)建一個面板
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();
}
}
}
});
}
//啟動服務(wù)器,創(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("服務(wù)器端口被占用");
System.exit(0);
}catch(IOExceptione){
e.printStackTrace();
System.out.println("服務(wù)器啟動失敗");
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());
//從服務(wù)器端向客戶端發(fā)送消息
if(socketArrayList.size()0){
IteratorSocketit=socketArrayList.iterator();
while(it.hasNext()){
Socketsoc=it.next();
if(soc.isClosed()){//當(dāng)客戶端某個socket已經(jīng)為關(guān)閉狀態(tài),移除此socket
it.remove();
continue;
}
//客戶端socket如果沒有關(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();
}
}
注意:要在自己的電腦上運行多次客戶端需要勾選如下選項
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)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 房屋整修合同協(xié)議書范本
- 工傷解除勞動合同協(xié)議書
- 運輸車輛維修合同協(xié)議書
- 發(fā)廊員工合同協(xié)議書
- 砌體合同協(xié)議書
- 雇傭工合同協(xié)議書
- 紡織品樣品制作流程詳解試題及答案
- 貼瓷磚安全合同協(xié)議書
- 飛鳥學(xué)堂測試題及答案
- 崗位合同協(xié)議書
- 自愿參加活動免責(zé)申明
- 交響音樂賞析智慧樹知到期末考試答案章節(jié)答案2024年西安交通大學(xué)
- DZ∕T 0248-2014 巖石地球化學(xué)測量技術(shù)規(guī)程(正式版)
- JTG-T 3331-07-2024 公路膨脹土路基設(shè)計與施工技術(shù)規(guī)范
- 護理研究中的偏倚及控制課件
- 醫(yī)院檢驗檢查管理制度
- 醫(yī)院手衛(wèi)生知識考試題庫100題(含答案)
- 管片基礎(chǔ)知識與管片選型
- 2024-2026年版中國運動康復(fù)產(chǎn)業(yè)白皮書
- 建筑設(shè)計防火要求規(guī)范2024修訂版
- 2021年武漢中考數(shù)學(xué)試題(附答案)
評論
0/150
提交評論