1、java局域网聊天全部源码package com.socket.tcp.basechat;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JO
2、ptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;/* * 登陆窗口。 * author lucky star * */public class LoginChat extends JDialog private final JPanel contentPanel = new JPanel(); private JTextField userNameField; /* * Launch the application.b */ publi
3、c static void main(String args) try LoginChat dialog = new LoginChat(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); catch (Exception e) e.printStackTrace(); /* * Create the dialog. */ public LoginChat() setLocationByPlatform(true); setBounds(100, 100, 450, 114
4、); getContentPane().setLayout(new BorderLayout(); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); JLabel label = new JLabel(u7528u6237u540DuFF1A); label.setBounds(10, 10, 54, 15); contentPanel.add(label); user
5、NameField = new JTextField(); userNameField.setBounds(75, 7, 349, 21); contentPanel.add(userNameField); userNameField.setColumns(10); JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT); getContentPane().add(buttonPane, BorderLayout.SOUTH); JButton okButton = new
6、JButton(OK); okButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent actionevent) String txt = userNameField.getText(); if (txt = null | .equals(txt) JOptionPane.showMessageDialog(LoginChat.this, 请输入用户名,提示,JOptionPane.WARNING_MESSAGE); userNameField.requestFocus(); re
7、turn; EventQueue.invokeLater(new Runnable() public void run() try Main frame = new Main(userNameField .getText(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); LoginChat.this.dispose(); ); okButton.setActionCommand(OK); buttonPane.add(okButton); getRootPane().setDefaultButton(o
8、kButton); JButton cancelButton = new JButton(Cancel); cancelButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent actionevent) LoginChat.this.dispose(); ); cancelButton.setActionCommand(Cancel); buttonPane.add(cancelButton); package com.socket.tcp.basechat;import java
9、.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.Image;import java.awt.MenuItem;import java.awt.Panel;import java.awt.PopupMenu;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable
10、;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetEvent;import java.awt.dnd.DropTargetListener;import java.awt.event.Ac
11、tionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedInputStream;import java.i
12、o.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.RandomAccessFile;import .InetAddress;import .ServerSocket;import .Socket;import .UnknownHo
13、stException;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import javax.swing.DefaultComboBoxModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JColorChooser;import javax.swing.JComboBox;import javax.swing.JDia
14、log;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextPane;import javax.sw
15、ing.ListSelectionModel;import javax.swing.border.EmptyBorder;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.filechooser.FileFilter;import javax.swing.table.TableModel;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;im
16、port javax.swing.text.Document;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;/* * 实现群、私聊、发送文字、图片、文件。 * 文件支持单次同时发送7个,多线程接收。 * author lucky star * */public class Main extends JFrame private JPanel contentPane; private JTextPane sendPane = null; private JTextPane rec
17、vPane = null; private JList onlineList = null; private JButton sendFileBtn = null, recvFileBtn = null; / 发送和接收的进度条 / private JProgressBar sendProgressBar = null, recvProgressBar = null; / 要发送的文件 private List sendFiles = new ArrayList(); / 要接收的文件 private List recvFiles = new ArrayList(); / 文件发送进度 pri
18、vate int fileLen = 0; / chat use TCP protocol private Socket s = null; private ObjectOutputStream oos = null; private Document doc = null; private Document slDoc = null; private String user = null; / 是否勾选私聊 private boolean isSL = false; / 与哪个私聊 private String slUser = null; / 私聊面板 private JTextPane
19、sltextPane = null; private Font f = null; / 字体对话框返回的字体。 / 发送和接受消息面板的样式 private SimpleAttributeSet msgAttrSet = new SimpleAttributeSet(); / 显示发送人和时间的样式 private SimpleAttributeSet tipAttrSet = null; private JTable table; public Font getF() return f; public void setF(Font f) this.f = f; /* * Launch the
20、 application. */ /* * public static void main(String args) EventQueue.invokeLater(new * Runnable() public void run() try Main frame = new Main(); * frame.setVisible(true); catch (Exception e) e.printStackTrace(); * ); */ / connect to server public void connect() try s = new Socket(InetAddress.getLoc
21、alHost(), 8888); oos = new ObjectOutputStream(s.getOutputStream(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); / send msg to server public void sendMsg(MsgInfo mi) try oos.writeObje
22、ct(mi); oos.flush(); catch (IOException e) System.out.println(send msg failed.); e.printStackTrace(); / disconnect to server when exit. public void disconnect() try if (oos != null) oos.close(); if (s != null) s.close(); catch (IOException e) System.out.println(exit chat.); e.printStackTrace(); publ
23、ic void insertString(String str, AttributeSet attributeset) try doc.insertString(doc.getLength(), str, attributeset); catch (BadLocationException e) / TODO Auto-generated catch block e.printStackTrace(); public void insertImg(Image img) recvPane.insertIcon(new ImageIcon(img); /* * Create the frame.
24、*/ public Main(String user) this.user = user; setTitle(User + user); FontAttr fa = new FontAttr(new Font(楷体, Font.PLAIN, 12), Color.BLUE, Color.WHITE); tipAttrSet = fa.getAttributeSet(); setF(new Font(楷体, Font.PLAIN, 12); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 773, 651);
25、 setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane jscrollPane1 = new JScrollPane(); jscrollPane1.setBounds(0, 28, 432, 199); contentPane.add(jscrollPane1); recvPane = new
26、JTextPane(); jscrollPane1.setViewportView(recvPane); recvPane.setEditable(false); doc = recvPane.getStyledDocument(); JButton imgBtn = new JButton(u56FEu7247); imgBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) / 选择图片 JFileChooser jfc = new JFileChooser(); jf
27、c.setDialogTitle(选择一张图片); jfc.setFileFilter(new FileFilter() private String accepts = jpg, jpeg, gif, png, bmp ; Override public String getDescription() / TODO Auto-generated method stub return null; Override public boolean accept(File arg0) boolean b = false; for (String s : accepts) if (arg0.getNa
28、me().endsWith(s) b = true; break; return b; ); int r = jfc.showOpenDialog(Main.this); if (r = JFileChooser.APPROVE_OPTION) / 确定 File f = jfc.getSelectedFile(); List imgs = new ArrayList(); imgs.add(f); MsgInfo mi = new MsgInfo(); 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