java局域网聊天全部源码Word文档格式.docx
《java局域网聊天全部源码Word文档格式.docx》由会员分享,可在线阅读,更多相关《java局域网聊天全部源码Word文档格式.docx(56页珍藏版)》请在冰豆网上搜索。
e.printStackTrace();
}
}
*Createthedialog.
publicLoginChat(){
setLocationByPlatform(true);
setBounds(100,100,450,114);
getContentPane().setLayout(newBorderLayout());
contentPanel.setBorder(newEmptyBorder(5,5,5,5));
getContentPane().add(contentPanel,BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabellabel=newJLabel("
\u7528\u6237\u540D\uFF1A"
);
label.setBounds(10,10,54,15);
contentPanel.add(label);
userNameField=newJTextField();
userNameField.setBounds(75,7,349,21);
contentPanel.add(userNameField);
userNameField.setColumns(10);
JPanelbuttonPane=newJPanel();
buttonPane.setLayout(newFlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane,BorderLayout.SOUTH);
{
JButtonokButton=newJButton("
OK"
okButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventactionevent){
Stringtxt=userNameField.getText();
if(txt==null||"
"
.equals(txt)){
JOptionPane.showMessageDialog(LoginChat.this,"
请输入用户名"
"
提示"
JOptionPane.WARNING_MESSAGE);
userNameField.requestFocus();
return;
}
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
Mainframe=newMain(userNameField
.getText());
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
LoginChat.this.dispose();
}
});
okButton.setActionCommand("
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
JButtoncancelButton=newJButton("
Cancel"
cancelButton.addActionListener(newActionListener(){
cancelButton.setActionCommand("
buttonPane.add(cancelButton);
}
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GraphicsEnvironment;
importjava.awt.Image;
importjava.awt.MenuItem;
importjava.awt.Panel;
importjava.awt.PopupMenu;
importjava.awt.datatransfer.DataFlavor;
importjava.awt.datatransfer.Transferable;
importjava.awt.datatransfer.UnsupportedFlavorException;
importjava.awt.dnd.DnDConstants;
importjava.awt.dnd.DropTarget;
importjava.awt.dnd.DropTargetDragEvent;
importjava.awt.dnd.DropTargetDropEvent;
importjava.awt.dnd.DropTargetEvent;
importjava.awt.dnd.DropTargetListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.BufferedInputStream;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.RandomAccessFile;
import.InetAddress;
import.ServerSocket;
import.Socket;
import.UnknownHostException;
importjava.util.ArrayList;
importjava.util.List;
importjavax.imageio.ImageIO;
importjavax.swing.DefaultComboBoxModel;
importjavax.swing.ImageIcon;
importjavax.swing.JCheckBox;
importjavax.swing.JColorChooser;
importjavax.swing.JComboBox;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JList;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;
importjavax.swing.JTextArea;
importjavax.swing.JTextPane;
importjavax.swing.ListSelectionModel;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importjavax.swing.filechooser.FileFilter;
importjavax.swing.table.TableModel;
importjavax.swing.text.AttributeSet;
importjavax.swing.text.BadLocationException;
importjavax.swing.text.Document;
importjavax.swing.text.SimpleAttributeSet;
importjavax.swing.text.StyleConstants;
*实现群、私聊、发送文字、图片、文件。
*文件支持单次同时发送7个,多线程接收。
publicclassMainextendsJFrame{
privateJPanelcontentPane;
privateJTextPanesendPane=null;
privateJTextPanerecvPane=null;
privateJListonlineList=null;
privateJButtonsendFileBtn=null,recvFileBtn=null;
//发送和接收的进度条
//privateJProgressBarsendProgressBar=null,recvProgressBar=null;
//要发送的文件
privateList<
String>
sendFiles=newArrayList<
();
//要接收的文件
recvFiles=newArrayList<
//文件发送进度
privateintfileLen=0;
//chatuseTCPprotocol
privateSockets=null;
privateObjectOutputStreamoos=null;
privateDocumentdoc=null;
privateDocumentslDoc=null;
privateStringuser=null;
//是否勾选私聊
privatebooleanisSL=false;
//与哪个私聊
privateStringslUser=null;
//私聊面板
privateJTextPanesltextPane=null;
privateFontf=null;
//字体对话框返回的字体。
//发送和接受消息面板的样式
privateSimpleAttributeSetmsgAttrSet=newSimpleAttributeSet();
//显示发送人和时间的样式
privateSimpleAttributeSettipAttrSet=null;
privateJTabletable;
publicFontgetF(){
returnf;
publicvoidsetF(Fontf){
this.f=f;
*Launchtheapplication.
/*
*publicstaticvoidmain(String[]args){EventQueue.invokeLater(new
*Runnable(){publicvoidrun(){try{Mainframe=newMain();
*frame.setVisible(true);
}catch(Exceptione){e.printStackTrace();
}}
*});
//connecttoserver
publicvoidconnect(){
s=newSocket(InetAddress.getLocalHost(),8888);
oos=newObjectOutputStream(s.getOutputStream());
}catch(UnknownHostExceptione){
//TODOAuto-generatedcatchblock
}catch(IOExceptione){
//sendmsgtoserver
publicvoidsendMsg(MsgInfomi){
oos.writeObject(mi);
oos.flush();
System.out.println("
sendmsgfailed."
//disconnecttoserverwhenexit.
publicvoiddisconnect(){
if(oos!
=null)
oos.close();
if(s!
s.close();
exitchat."
publicvoidinsertString(Stringstr,AttributeSetattributeset){
doc.insertString(doc.getLength(),str,attributeset);
}catch(BadLocationExceptione){
publicvoidinsertImg(Imageimg){
recvPane.insertIcon(newImageIcon(img));
*Createtheframe.
publicMain(Stringuser){
this.user=user;
setTitle("
User"
+user);
FontAttrfa=newFontAttr(newFont("
楷体"
Font.PLAIN,12),Color.BLUE,
Color.WHITE);
tipAttrSet=fa.getAttributeSet();
setF(newFont("
Font.PLAIN,12));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,773,651);
setLocationRelativeTo(null);
contentPane=newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPanejscrollPane1=newJScrollPane();
jscrollPane1.setBounds(0,28,432,199);
contentPane.add(jscrollPane1);
recvPane=newJTextPane();
jscrollPane1.setViewportView(recvPane);
recvPane.setEditable(false);
doc=recvPane.getStyledDocument();
JButtonimgBtn=newJButton("
\u56FE\u7247"
imgBtn.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//选择图片
JFileChooserjfc=newJFileChooser();
jfc.setDialogTitle("
选择一张图片"
jfc.setFileFilter(newFileFilter(){
privateStringaccepts[]={"
jpg"
"
jpeg"
gif"
png"
"
bmp"
};
@Override
publicStringgetDescription(){
//TODOAuto-generatedmethodstub
returnnull;
publicbooleanaccept(Filearg0){
booleanb=false;
for(Strings:
accepts){
if(arg0.getName().endsWith(s)){
b=true;
break;
returnb;
intr=jfc.showOpenDialog(Main.this);
if(r==JFileChooser.APPROVE_OPTION){//确定
Filef=jfc.getSelectedFile();
List<
File>
imgs=newArrayList<
imgs.add(f);
MsgInfomi=newMsgInfo();
mi.setSender(Main.this.user);
mi.setImgs(imgs);
if(isSL){//私聊
mi.setUser(slUser);
//将图片添加到私聊窗口
try{
try{
slDoc.insertString(
slDoc.getLength(),
Main.this.user+