首页 > 代码库 > javase基础学完可以做什么,javase实现局域网聊天室

javase基础学完可以做什么,javase实现局域网聊天室

javase基础学完可以做什么,javase实现局域网聊天室

包含内容:基础语法,面向对象,多线程,IO流,GUI编程,网络编程(udp)

实现功能:局域网群聊,局域网群发文件(还不太完善,只能传输小体积的文本文件)

由于本人也是刚开始学java,而且掌握的不太好,所以代码可能有些臃肿。请谅解

登录界面

技术分享

群聊界面

技术分享

代码写一个java文件里了,不太方便阅读。

  1 package test;  2   3 import java.awt.*;  4 import java.awt.event.*;  5 import java.io.*;  6 import java.net.*;  7 import javax.swing.*;  8 import javax.swing.filechooser.*;  9  10 public class Chatdemo2 11 { 12     private static JFrame loginjf; 13     public static void main(String[] args) 14     { 15         loginView(); 16     } 17     private static void loginView() 18     { 19         loginjf = new JFrame("输入自己的名字"); 20         final JTextField nametext = new JTextField(10); 21         loginjf.setLayout(new FlowLayout()); 22         JButton jb = new JButton("登录"); 23         loginjf.add(nametext); 24         loginjf.add(jb); 25         nametext.addActionListener(new ActionListener() 26         { 27             public void actionPerformed(ActionEvent e) 28             { 29                 if(nametext.getText().equals("")) 30                 { 31                     JDialog jdl=new JDialog(loginjf,"提示",true); 32                     jdl.setBounds(500, 200, 400, 150); 33                     jdl.setLayout(new FlowLayout()); 34                     JLabel jl1=new JLabel("用户名不能为空"); 35                     jdl.add(jl1); 36                     jdl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 37                     jdl.setVisible(true); 38                 }else { 39                     String name = nametext.getText(); 40                     loginjf.dispose(); 41                     new MainFrame(name); 42                 } 43             } 44         }); 45         jb.addActionListener(new ActionListener() 46         { 47  48             public void actionPerformed(ActionEvent e) 49             { 50                 if(nametext.getText().equals("")) 51                 { 52                     JDialog jdl=new JDialog(loginjf,"提示",true); 53                     jdl.setBounds(500, 200, 400, 150); 54                     jdl.setLayout(new FlowLayout()); 55                     JLabel jl1=new JLabel("用户名不能为空"); 56                     jdl.add(jl1); 57                     jdl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 58                     jdl.setVisible(true); 59                 }else { 60                     String name = nametext.getText(); 61                     loginjf.dispose(); 62                     new MainFrame(name); 63                 } 64             } 65         }); 66         loginjf.setBounds(450, 300, 350, 100); 67         loginjf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 68         loginjf.setVisible(true); 69     } 70 } 71 class MainFrame 72 { 73     private final String name; 74     private DatagramSocket sendSocket; 75     private DatagramSocket receSocket; 76     private DatagramSocket fileReceSocket; 77     private JTextArea jta; 78     private JTextField jtf; 79     private String localip; 80     private JFrame jFrame; 81     private File file; 82     private String datass; 83  84     public MainFrame(String s) 85     { 86         this.name = s; 87         initView(); 88         new Thread() 89         { 90             public void run() 91             { 92                 mygetmsg(); 93             } 94         }.start(); 95         new Thread() 96         { 97             public void run() 98             { 99                 mygetfile();100             }101         }.start();102     }103 104     private void initView()105     {106         try107         {108             sendSocket = new DatagramSocket(10002);109             receSocket = new DatagramSocket(10003);110             fileReceSocket = new DatagramSocket(10004);111             112             localip = InetAddress.getLocalHost().getHostAddress();113             System.out.println(localip);114             String[] strings = localip.split("\\.");115             String radioip = strings[0] + "." + strings[1] + ".255"+ ".255";116             System.out.println(radioip);117             118             jFrame = new JFrame("局域网聊天室1.0");119             jFrame.setBounds(450, 80, 500, 600);120             jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);121             JMenuBar jmb = new JMenuBar();122             JMenu jm = new JMenu("文件");123             JMenu jm1=new JMenu("学习");124             JMenu jm2=new JMenu("关于");125             JMenuItem jmi1 = new JMenuItem("群发文件");126             JMenuItem jmi2 = new JMenuItem("群发文件说明");127             JMenuItem jmi3 = new JMenuItem("我要自学java");128             JMenuItem jmi4 = new JMenuItem("百度一下");129             JMenuItem jmi5 = new JMenuItem("关于作者");130 131             jmb.add(jm);132             jmb.add(jm1);133             jmb.add(jm2);134             135             jm.add(jmi1);136             jm.add(jmi2);137             jm1.add(jmi3);138             jm1.add(jmi4);139             jm2.add(jmi5);140             jFrame.setJMenuBar(jmb);141             jta = new JTextArea();142             JScrollPane jsp = new JScrollPane(jta);143             jtf = new JTextField(10);144             jta.setEditable(false);145             jFrame.add(jsp, BorderLayout.CENTER);146             jFrame.add(jtf, BorderLayout.SOUTH);147             jFrame.setVisible(true);148             149             String loginOk = "【"+ name +"】上线了" + "                      (*^__^*)系统提示(*^__^*)";150             byte[] b = loginOk.getBytes();151             DatagramPacket loginpack = new DatagramPacket(b, b.length,InetAddress.getByName("255.255.255.255"), 10003);152             sendSocket.send(loginpack);153             154             jmi1.addActionListener(new FileListen(sendSocket,jFrame,name));155             jmi2.addActionListener(new ActionListener()156             {157                 public void actionPerformed(ActionEvent e)158                 {159                     JDialog jdl=new JDialog(jFrame,"提示",true);160                     jdl.setBounds(500, 200, 400, 150);161                     jdl.setLayout(new FlowLayout());162                     JLabel jl1=new JLabel("注意:本程序为UDP传输实现,可能会丢失数据!~~~~(>_<)~~~~");163                     JLabel jl2=new JLabel("       所以群发文件时候只能群发文本文档! ~~~~(>_<)~~~~");164                     JLabel jl3=new JLabel("       其他文件发送出去可能会成乱码文件! ~~~~(>_<)~~~~");165                     jdl.add(jl1);166                     jdl.add(jl2);167                     jdl.add(jl3);168                     jdl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);169                     jdl.setVisible(true);170                 }});171             jmi3.addActionListener(new ActionListener()172             {173                 public void actionPerformed(ActionEvent e)174                 {175                     JDialog jdl=new JDialog(jFrame,"O(∩_∩)O",true);176                     jdl.setBounds(600, 200, 170, 150);177                     jdl.setLayout(new FlowLayout());178                     JLabel jl1=new JLabel("百度是最好的老师!");179                     JLabel jl2=new JLabel("快点开百度学习吧!");180                     JLabel jl3=new JLabel(" O(∩_∩)O哈哈~ ");181                     jdl.add(jl1);182                     jdl.add(jl2);183                     jdl.add(jl3);184                     jdl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);185                     jdl.setVisible(true);186                 }});187             jmi4.addActionListener(new ActionListener()188             {189                 public void actionPerformed(ActionEvent e)190                 {191                     try192                     {193                         Runtime.getRuntime().exec("explorer http://www.baidu.com");194                     } catch (Exception e1)195                     {196                         e1.printStackTrace();197                     }198                 }});199             jmi5.addActionListener(new ActionListener()200             {201                 public void actionPerformed(ActionEvent e)202                 {203                     JDialog jdl=new JDialog(jFrame,"(*^__^*)",true);204                     jdl.setBounds(600, 200, 170, 150);205                     jdl.setLayout(new FlowLayout());206                     JLabel jl1=new JLabel("QQ:       835476090");207                     JLabel jl2=new JLabel("Designer:  a * 大火");208                     JLabel jl3=new JLabel("teacher:    张大鹏");209                     jdl.add(jl1);210                     jdl.add(jl2);211                     jdl.add(jl3);212                     jdl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);213                     jdl.setVisible(true);214                 }});215             jtf.addActionListener(new MsgListen(sendSocket,jtf,name));216             jFrame.addWindowListener(new WindowClose(sendSocket,name));217         } catch (Exception e)218         {219             e.printStackTrace();220         }221     }222     private void mygetmsg()223     {224         try225         {226             while (true)227             {228                 byte[] buf = new byte[1024];229                 DatagramPacket dp = new DatagramPacket(buf, buf.length);230                 receSocket.receive(dp);231                 String data = http://www.mamicode.com/new String(dp.getData(), 0, dp.getLength());232                 233                 jta.append(data + "\r\n");234             }235         } catch (Exception e)236         {237             e.printStackTrace();238         }239     }240     private void mygetfile()241     {242         FileSystemView fsv = FileSystemView.getFileSystemView();243         File com = fsv.getHomeDirectory();244         String deskpath = com.getPath();245         try246         {247             while (true)248             {        249                 byte[] buf = new byte[1024*1024];250                 DatagramPacket dp = new DatagramPacket(buf, buf.length);251                 fileReceSocket.receive(dp);        252                 String senderip=dp.getAddress().getHostAddress();253                 String data = http://www.mamicode.com/new String(dp.getData(), 0, dp.getLength());254                 String datacopy=data;255                 data = http://www.mamicode.com/data.replace("铮标记", "");256                 File filedir=new File(deskpath+"/"+senderip);257                 file= new File(filedir,data);    258                 if (datacopy.contains("大火标记"))259                 {260                     datass=data;261                     System.out.println(datass);262                     if (!filedir.exists())263                     {264                         System.out.println(filedir.mkdirs());265                     }266                     if (!file.exists())267                     {268                         System.out.println(file.createNewFile());269                     }270                 }else271                 {272                     file= new File(filedir,datass);273                     FileOutputStream fos=new FileOutputStream(file);274                     fos.write(data.getBytes());275                     fos.close();276                     /*也可以277                     RandomAccessFile raf = new RandomAccessFile(file, "rw");278                     raf.seek(0);279                     for (int b = 0; b < i; b++)280                     {281                         raf.readLine();282                     }283                     raf.write(data.getBytes());284                     raf.close();285                     i++;*/286                 }287             }288         } catch (Exception e)289         {290             e.printStackTrace();291         }292     }293 }294 class MsgListen implements ActionListener295 {296     private DatagramSocket sendSocket;297     private JTextField jtf;298     private String name;299     public MsgListen(DatagramSocket sendSocket,JTextField jtf,String name)300     {301         this.sendSocket=sendSocket;302         this.jtf=jtf;303         this.name=name;304     }305     public void actionPerformed(ActionEvent e)306     {307         String string = jtf.getText();308         jtf.setText("");309         String string1=name+":"+string;310         byte[] buf = string1.getBytes();311         try312         {313             DatagramPacket data = http://www.mamicode.com/new DatagramPacket(buf,314                     buf.length, InetAddress315                             .getByName("255.255.255.255"), 10003);316             sendSocket.send(data);317         } catch (Exception e1)318         {319             e1.printStackTrace();320         }321     }322 }323 class FileListen implements ActionListener324 {325     private DatagramSocket sendSocket;    326     private JFrame jFrame;327     private String name;328     public FileListen(DatagramSocket sendSocket,JFrame jFrame,String name)329     {330         this.sendSocket=sendSocket;331         this.jFrame=jFrame;332         this.name=name;333     }334     public void actionPerformed(ActionEvent e)335     {336         try337         {338             FileDialog fileDialog = new FileDialog(jFrame, "对话框"); // 菜单项被选中时就打开一个对话框,让用户选择一个文件339             fileDialog.setVisible(true); // 对话框显示出来340             if (fileDialog.getFile() != null341                     && fileDialog.getDirectory() != null)342             {343                 String fname = fileDialog.getFile();344                 String url = fileDialog.getDirectory();345                 String urlname = url + fname;346                 File file1 = new File(urlname);347                 String filename1=fname+"大火标记";348                 byte[] filename = filename1.getBytes();349                 DatagramPacket filenamepack = new DatagramPacket(filename, filename.length, InetAddress.getByName("255.255.255.255"), 10004);350                 sendSocket.send(filenamepack);351                 FileInputStream fr=new FileInputStream(file1);352                 byte[] buf= new byte[1024*500];353                 int len;354                 while ((len = fr.read(buf)) != -1)355                 {356                     DatagramPacket filedatapack = new DatagramPacket(buf,len, InetAddress.getByName("255.255.255.255"), 10004);357                     sendSocket.send(filedatapack);358                 }359                 fr.close();360                 String sendOk = "【"+ name +"】给大家发了个文件,你们看下收到没" + "       (*^__^*)系统提示(*^__^*)";361                 byte[] b = sendOk.getBytes();362                 DatagramPacket senOkpack = new DatagramPacket(b, b.length,InetAddress.getByName("255.255.255.255"), 10003);363                 sendSocket.send(senOkpack);364             }365         } catch (Exception e1)366         {367             e1.printStackTrace();368         }369     }370 }371 class WindowClose extends WindowAdapter372 {373     private DatagramSocket sendSocket;    374     private String name;375     public WindowClose(DatagramSocket sendSocket,String name)376     {377         this.sendSocket=sendSocket;378         this.name=name;379     }380     public void windowClosing(WindowEvent e)381     {382         try383         {384             String offline = "【"+ name +"】下线了" + "                      (*^__^*)系统提示(*^__^*)";385             byte[] b = offline.getBytes();386             DatagramPacket offlinepack = new DatagramPacket(b, b.length,InetAddress.getByName("255.255.255.255"), 10003);387             sendSocket.send(offlinepack);388         } catch (Exception e1)389         {390             e1.printStackTrace();391         }392     }393 }

 

javase基础学完可以做什么,javase实现局域网聊天室