-
大小: 4KB文件類(lèi)型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-05-13
- 語(yǔ)言: Java
- 標(biāo)簽: JAVA??聊天室??網(wǎng)絡(luò)??課程設(shè)計(jì)??實(shí)驗(yàn)??
資源簡(jiǎn)介
(1)用Java圖形用戶(hù)界面編寫(xiě)聊天室服務(wù)器端和客戶(hù)端, 支持多個(gè)客戶(hù)端連接到一個(gè)服務(wù)器。每個(gè)客戶(hù)端能夠輸入賬號(hào)。
(2)可以實(shí)現(xiàn)群聊(聊天記錄顯示在所有客戶(hù)端界面)。
(3)完成好友列表在各個(gè)客戶(hù)端上顯示。
(4)可以實(shí)現(xiàn)私人聊天,用戶(hù)可以選擇某個(gè)其他用戶(hù),單獨(dú)發(fā)送信息。
(5)服務(wù)器能夠群發(fā)系統(tǒng)消息,能夠強(qiáng)行讓某些用戶(hù)下線(xiàn)。
(6)客戶(hù)端的上線(xiàn)下線(xiàn)要求能夠在其他客戶(hù)端上面實(shí)時(shí)刷新。

代碼片段和文件信息
package?聊天室;
import?java.net.*;
import?java.util.ArrayList;
import?java.io.*;
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
import?javax.swing.text.DefaultCaret;
public?class?Client?extends?Jframe?implements?RunnableActionListenerItemListener{
private?JTextArea??jta?=?new?JTextArea();
private?JTextField?jtf?=?new?JTextField();
private?JButton?jb=new?JButton(“發(fā)送消息“);
private?List?online?=?new?List();
private?String?nickName;
private?Socket?s?=?null;
private?PrintStream?ps?=?null;
private?BufferedReader?br?=?null;
????private?JScrollPane?jsp=new?JScrollPane(jta);
????private?String?msg?=new?String();
????private?ArrayList?siliaoname=new?ArrayList();
????private?String?msg2?=?new?String();
????private?String?ip;
????private?int?port;
????private?SocketAddress?addr;
????public?void?itemStateChanged(ItemEvent?e)?{
if(e.getSource()==online)
{
}
}
????
public?Client()?throws?Exception{
????jsp.setVerticalScrollBarPolicy(
???? JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
????online.addItemListener(this);
????online.addActionListener(this);
????getContentPane().add(jsp?BorderLayout.CENTER);
getContentPane().add(jtfBorderLayout.SOUTH);
getContentPane().add(onlineBorderLayout.EAST);
jtf.setBackground(Color.WHITE);
jta.setWrapstyleWord(true);
jta.setFont(new?Font(“微軟雅黑“Font.BOLD25));
jtf.setFont(new?Font(“微軟雅黑“Font.BOLD25));
jtf.addActionListener(this);
jta.setEditable(false);
jta.setLineWrap(true);
jb.setBounds(490?0?100?38);
jtf.add(jb);
jb.setVisible(true);
????????jb.addActionListener(this);
DefaultCaret?caret?=?(DefaultCaret)jta.getCaret();//跟著進(jìn)度條移動(dòng)
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
this.setSize(600600);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new?WindowAdapter(){
public?void?windowClosing(WindowEvent?e)?{
int?option=?JOptionPane.showConfirmDialog(
Client.this?“確定退出聊天室??“?“提示?“JOptionPane.YES_NO_OPTION);
if(option==JOptionPane.YES_OPTION)
if(e.getWindow()?==?Client.this)
{try?{
????????ps.println(“EXIT#“+?nickName);
System.exit(0);
}catch(Exception?exit)?{}
}
return?;
}
});
this.setVisible(true);
this.setLocationRelativeTo(null);
ip=“127.0.0.1“;
port=9999;
try?{
nickName?=?JOptionPane.showInputDialog(Client.this“請(qǐng)輸入您的賬號(hào)“);
while(nickName.equals(““))?{
JOptionPane.showMessageDialog(
Client.this?“賬號(hào)不能為空?“?“錯(cuò)誤?“JOptionPane.YES_NO_OPTION);
nickName?=?JOptionPane.showInputDialog(Client.this“請(qǐng)輸入您的賬號(hào)“);
}
}catch(Exception?e)?{
System.exit(0);
}
try?{
s?=?new?Socket();
addr=new?InetSocketAddress(ipport);
s.connect(addr1000);?
}catch(Exception?e)?{
JOptionPane.showMessageDialog(
Client.this?“服務(wù)器暫未開(kāi)放?“?“提示?“JOptionPane.YES_NO_OPTION);
System.exit(0);
}
ps?=?new?PrintStream(s.g
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????4503??2019-05-25?09:58??Server.java
?????文件????????7782??2019-05-25?09:58??Client.java
評(píng)論
共有 條評(píng)論