java局域网聊天全部源码.docx

上传人:b****6 文档编号:4770862 上传时间:2022-12-08 格式:DOCX 页数:56 大小:33.94KB
下载 相关 举报
java局域网聊天全部源码.docx_第1页
第1页 / 共56页
java局域网聊天全部源码.docx_第2页
第2页 / 共56页
java局域网聊天全部源码.docx_第3页
第3页 / 共56页
java局域网聊天全部源码.docx_第4页
第4页 / 共56页
java局域网聊天全部源码.docx_第5页
第5页 / 共56页
点击查看更多>>
下载资源
资源描述

java局域网聊天全部源码.docx

《java局域网聊天全部源码.docx》由会员分享,可在线阅读,更多相关《java局域网聊天全部源码.docx(56页珍藏版)》请在冰豆网上搜索。

java局域网聊天全部源码.docx

java局域网聊天全部源码

packagecom.socket.tcp.basechat;

importjava.awt.BorderLayout;

importjava.awt.EventQueue;

importjava.awt.FlowLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JDialog;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

importjavax.swing.border.EmptyBorder;

/**

*登陆窗口。

*@authorluckystar

*

*/

publicclassLoginChatextendsJDialog{

privatefinalJPanelcontentPanel=newJPanel();

privateJTextFielduserNameField;

/**

*Launchtheapplication.b

*/

publicstaticvoidmain(String[]args){

try{

LoginChatdialog=newLoginChat();

dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

dialog.setVisible(true);

}catch(Exceptione){

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("OK");

buttonPane.add(okButton);

getRootPane().setDefaultButton(okButton);

}

{

JButtoncancelButton=newJButton("Cancel");

cancelButton.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventactionevent){

LoginChat.this.dispose();

}

});

cancelButton.setActionCommand("Cancel");

buttonPane.add(cancelButton);

}

}

}

}

packagecom.socket.tcp.basechat;

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.FlowLayout;

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.ActionEvent;

importjava.awt.event.ActionListener;

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.JButton;

importjavax.swing.JCheckBox;

importjavax.swing.JColorChooser;

importjavax.swing.JComboBox;

importjavax.swing.JDialog;

importjavax.swing.JFileChooser;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JList;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JScrollPane;

importjavax.swing.JTable;

importjavax.swing.JTextArea;

importjavax.swing.JTextPane;

importjavax.swing.ListSelectionModel;

importjavax.swing.border.EmptyBorder;

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个,多线程接收。

*@authorluckystar

*

*/

publicclassMainextendsJFrame{

privateJPanelcontentPane;

privateJTextPanesendPane=null;

privateJTextPanerecvPane=null;

privateJListonlineList=null;

privateJButtonsendFileBtn=null,recvFileBtn=null;

//发送和接收的进度条

//privateJProgressBarsendProgressBar=null,recvProgressBar=null;

//要发送的文件

privateListsendFiles=newArrayList();

//要接收的文件

privateListrecvFiles=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(){

try{

s=newSocket(InetAddress.getLocalHost(),8888);

oos=newObjectOutputStream(s.getOutputStream());

}catch(UnknownHostExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

//sendmsgtoserver

publicvoidsendMsg(MsgInfomi){

try{

oos.writeObject(mi);

oos.flush();

}catch(IOExceptione){

System.out.println("sendmsgfailed.");

e.printStackTrace();

}

}

//disconnecttoserverwhenexit.

publicvoiddisconnect(){

try{

if(oos!

=null)

oos.close();

if(s!

=null)

s.close();

}catch(IOExceptione){

System.out.println("exitchat.");

e.printStackTrace();

}

}

publicvoidinsertString(Stringstr,AttributeSetattributeset){

try{

doc.insertString(doc.getLength(),str,attributeset);

}catch(BadLocationExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

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;

}

@Override

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();

Listimgs=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+

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1