仿QQ聊天軟件及源碼java版

 

一直以來,很多java的同志們都沒有一個完整資料來參考。把源碼貼出來,大家共享!

  1. import java.awt.BorderLayout;  
  2. import java.awt.Component;  
  3. import java.awt.Dialog;  
  4. import java.awt.Dimension;  
  5. import java.awt.FlowLayout;  
  6. import java.awt.GridBagConstraints;  
  7. import java.awt.GridBagLayout;  
  8. import java.awt.GridLayout;  
  9. import java.awt.Image;  
  10. import java.awt.Insets;  
  11. import java.awt.MenuItem;  
  12. import java.awt.PopupMenu;  
  13. import java.awt.Rectangle;  
  14. import java.awt.SystemTray;  
  15. import java.awt.Toolkit;  
  16. import java.awt.TrayIcon;  
  17. import java.awt.TrayIcon.MessageType;  
  18. import java.awt.event.ActionEvent;  
  19. import java.awt.event.ActionListener;  
  20. import java.awt.event.ComponentAdapter;  
  21. import java.awt.event.ComponentEvent;  
  22. import java.awt.event.MouseAdapter;  
  23. import java.awt.event.MouseEvent;  
  24. import java.awt.event.WindowAdapter;  
  25. import java.awt.event.WindowEvent;  
  26. import java.io.File;  
  27. import java.io.FileInputStream;  
  28. import java.io.FileNotFoundException;  
  29. import java.io.FileOutputStream;  
  30. import java.io.IOException;  
  31. import java.net.DatagramPacket;  
  32. import java.net.DatagramSocket;  
  33. import java.net.InetAddress;  
  34. import java.net.SocketException;  
  35. import java.net.UnknownHostException;  
  36. import java.util.Date;  
  37. import java.util.Scanner;  
  38. import java.util.Stack;  
  39. import java.util.prefs.Preferences;  
  40.   
  41. import javax.swing.BoxLayout;  
  42. import javax.swing.ImageIcon;  
  43. import javax.swing.JButton;  
  44. import javax.swing.JDialog;  
  45. import javax.swing.JFrame;  
  46. import javax.swing.JLabel;  
  47. import javax.swing.JList;  
  48. import javax.swing.JMenuItem;  
  49. import javax.swing.JOptionPane;  
  50. import javax.swing.JPanel;  
  51. import javax.swing.JPasswordField;  
  52. import javax.swing.JPopupMenu;  
  53. import javax.swing.JProgressBar;  
  54. import javax.swing.JScrollPane;  
  55. import javax.swing.JTabbedPane;  
  56. import javax.swing.JTextField;  
  57. import javax.swing.JToggleButton;  
  58. import javax.swing.ScrollPaneConstants;  
  59. import javax.swing.SwingConstants;  
  60. import javax.swing.UIManager;  
  61. import javax.swing.border.BevelBorder;  
  62. import javax.swing.border.Border;  
  63. import javax.swing.border.EmptyBorder;  
  64. import javax.swing.border.TitledBorder;  
  65. import javax.swing.tree.DefaultMutableTreeNode;  
  66. import javax.swing.tree.DefaultTreeModel;  
  67. import javax.swing.tree.TreePath;  
  68.   
  69.   
  70. public class EQ extends JDialog{  
  71.     private JTextField ipEndTField;  
  72.     private JTextField ipStartTField;  
  73.     private JTextField userNameTField;//用戶框   
  74.     private JPasswordField passwordTField;//密碼框   
  75.     private JTextField placardPathTField;//placard:公告   
  76.     private JTextField updatePathTField;//更新路徑   
  77.     private JTextField pubPathTField;  
  78.     public static EQ frame = null;  
  79.     private ChatTree chatTree;  
  80.     private JPopupMenu popupMenu;//右鍵菜單   
  81.     private JTabbedPane tabbedPane;//分頁面板   
  82.     private JToggleButton searchUserButton;//搜索用戶按鈕   
  83.     private JProgressBar progressBar;//滾動條   
  84.     private JList faceList;  
  85.     private JButton selectInterfaceOKButton;  
  86.     private DatagramSocket ss;//數據報插座   
  87.     private final JLabel stateLabel;  
  88.     private static String user_dir;  
  89.     private static File localFile;  
  90.     private static File netFile;  
  91.     private String netFilePath;  
  92.     private JButton messageAlertButton;  
  93.     private Stack<String> messageStack;////Stack 類表示後進先出(LIFO)的對象堆棧。   
  94.     private ImageIcon messageAlertIcon;  
  95.     private ImageIcon messageAlertNullIcon;  
  96.     private Rectangle location;  
  97.     public static TrayIcon trayicon;  
  98.     private Dao dao;  
  99.     //Preferences.systeRoot:返回系統的根首選項節點   Preferences:參數選擇   
  100.     public final static Preferences preferences = Preferences.systemRoot();  
  101.     private JButton userInfoButton;  
  102.     //----------------------------------------------------------------------------------   
  103.     public static void main(String[] args){  
  104.         try{  
  105.             String laf = preferences.get("lookAndFeel""java默認");  
  106.             if(laf.indexOf("當前系統") > -1){  
  107.                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
  108.             }  
  109.             EQ frame = new EQ();  
  110.             frame.setVisible(true);  
  111.             frame.SystemTrayInitial();// 初始化系統欄   
  112.             frame.server();  
  113.             frame.checkPlacard();  
  114.         }catch(Exception e){  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118.     //----------------------------------------------------------------------------------   
  119.     public EQ(){  
  120.         super(new JFrame());  
  121.         frame = this;  
  122.         dao = Dao.getDao();  
  123.         location = dao.getLocation();  
  124.         setTitle("EQ聊天");  
  125.         setBounds(location);  
  126.         progressBar = new JProgressBar();  
  127.         //BevelBorder:該類實現簡單的雙線斜面邊框。Bevel:斜面     lowered:凹入斜面類型。   
  128.         progressBar.setBorder(new BevelBorder(BevelBorder.LOWERED));  
  129.         tabbedPane = new JTabbedPane();  
  130.         popupMenu = new JPopupMenu();  
  131.         chatTree = new ChatTree(this);  
  132.         user_dir = System.getProperty("user.dir");// 程序執行路徑用於系統更新   
  133.         localFile = new File(user_dir + File.separator + "EQ.jar");// 本地EQ文件   
  134.         stateLabel = new JLabel();// 狀態欄標籤   
  135.         this.addWindowListener(new FrameWindowListener());// 添加窗體監視器   
  136.         this.addComponentListener(new ComponentAdapter(){  
  137.             public void componentResized(final ComponentEvent e){  
  138.                 saveLocation();  
  139.             }  
  140.             public void componentMoved(final ComponentEvent e){  
  141.                 saveLocation();  
  142.             }  
  143.         });  
  144.         try{// 啓動通訊服務端口   
  145.             ss = new DatagramSocket(1111);  
  146.         }catch(SocketException e2){  
  147.             if(e2.getMessage().startsWith("Address already in use")){  
  148.                 showMessageDialog("服務端口被佔用,或者本軟件已經運行。");  
  149.             }  
  150.             System.exit(0);  
  151.         }  
  152.         {// 初始化公共信息按鈕   
  153.             messageAlertIcon = new ImageIcon(EQ.class.getResource("/image/messageAlert.gif"));  
  154.             messageAlertNullIcon = new ImageIcon(EQ.class.getResource("/image/messageAlertNull20.gif"));  
  155.             messageStack = new Stack<String>();  
  156.             messageAlertButton = new JButton();  
  157.             messageAlertButton.setHorizontalAlignment(SwingConstants.RIGHT);  
  158.             messageAlertButton.setContentAreaFilled(false);//不填充內容區域   
  159.             final JPanel BannerPanel = new JPanel();  
  160.             BannerPanel.setLayout(new BorderLayout());  
  161.             this.add(BannerPanel,BorderLayout.NORTH);  
  162.             userInfoButton = new JButton();  
  163.             BannerPanel.add(userInfoButton,BorderLayout.WEST);  
  164.             userInfoButton.setMargin(new Insets(0,0,0,10));//Margin:邊距    insets:插入    
  165.             initUserInfoButton();// 初始化本地用戶頭像按鈕   
  166.             BannerPanel.add(messageAlertButton,BorderLayout.CENTER);  
  167.             messageAlertButton.addActionListener(new ActionListener(){  
  168.   
  169.                 @Override  
  170.                 public void actionPerformed(ActionEvent arg0) {  
  171.                     if(!messageStack.empty()){  
  172.                         showMessageDialog(messageStack.pop());//堆棧頂部的對象(Vector 對象中的最後一項)。    
  173.                     }  
  174.                 }  
  175.             });  
  176.             messageAlertButton.setIcon(messageAlertIcon);  
  177.             showMessageBar();  
  178.         }  
  179.         this.add(tabbedPane,BorderLayout.CENTER);  
  180.         tabbedPane.setTabPlacement(SwingConstants.LEFT);//設置此選項卡窗格的選項卡布局   
  181.         ImageIcon userTicon = new ImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft.PNG"));  
  182.         tabbedPane.addTab(null,userTicon,createUserList(),"用戶列表");  
  183.         ImageIcon sysOTicon = new ImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft2.PNG"));  
  184.         tabbedPane.addTab(null, sysOTicon, createSysToolPanel(), "系統操作");  
  185.         ImageIcon sysSTicon = new ImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft3.png"));  
  186.         tabbedPane.addTab(null, sysSTicon, createSysSetPanel(), "系統設置");  
  187.         this.setAlwaysOnTop(true);//總在頂部   
  188.     }  
  189.     //----------------------------------------------------------------------------------   
  190.     private JScrollPane createSysSetPanel(){//系統設置面板   
  191.         final JPanel sysSetPanel = new JPanel();  
  192.         JScrollPane scrollPane = new JScrollPane(sysSetPanel);  
  193.         sysSetPanel.setLayout(new BoxLayout(sysSetPanel,BoxLayout.Y_AXIS));  
  194.         scrollPane.setBorder(new EmptyBorder(0,0,0,0));  
  195.         final JPanel sysPathPanel = new JPanel();  
  196.         sysPathPanel.setMaximumSize(new Dimension(600,200));  
  197.         sysPathPanel.setBorder(new TitledBorder("系統路徑"));//title - 邊框應顯示的標題   
  198.         sysPathPanel.setLayout(new GridLayout(0,1));  
  199.         sysSetPanel.add(sysPathPanel);  
  200.         sysPathPanel.add(new JLabel("程序升級路徑"));  
  201.         updatePathTField = new JTextField(preferences.get("updatePath""請輸入路徑"));  
  202.         sysPathPanel.add(updatePathTField);  
  203.         sysPathPanel.add(new JLabel("系統公告路徑:"));  
  204.         placardPathTField = new JTextField(preferences.get("placardPath","請輸入路徑"));  
  205.         sysPathPanel.add(placardPathTField);  
  206.         sysPathPanel.add(new JLabel("公共程序路徑:"));  
  207.         pubPathTField = new JTextField(preferences.get("pubPath""請輸入路徑"));  
  208.         sysPathPanel.add(pubPathTField);  
  209.         final JButton pathOKButton = new JButton("確定");  
  210.         pathOKButton.setActionCommand("sysOK");  
  211.         pathOKButton.addActionListener(new SysSetPanelOKListener());  
  212.         sysSetPanel.add(pathOKButton);  
  213.         final JPanel loginPanel = new JPanel();  
  214.         loginPanel.setMaximumSize(new Dimension(60090));  
  215.         loginPanel.setBorder(new TitledBorder("登錄升級服務器"));  
  216.         final GridLayout gridLayout_1 = new GridLayout(01);  
  217.         gridLayout_1.setVgap(5);  
  218.         loginPanel.setLayout(gridLayout_1);  
  219.         sysSetPanel.add(loginPanel);  
  220.         final JPanel panel_7 = new JPanel();  
  221.         panel_7.setLayout(new BoxLayout(panel_7, BoxLayout.X_AXIS));  
  222.         loginPanel.add(panel_7);  
  223.         panel_7.add(new JLabel("用戶名:"));  
  224.         userNameTField = new JTextField(preferences.get("username""請輸入用戶名"));  
  225.         panel_7.add(userNameTField);  
  226.         final JPanel panel_8 = new JPanel();  
  227.         panel_8.setLayout(new BoxLayout(panel_8, BoxLayout.X_AXIS));  
  228.         loginPanel.add(panel_8);  
  229.         panel_8.add(new JLabel("密 碼:"));  
  230.         passwordTField = new JPasswordField("*****");  
  231.         panel_8.add(passwordTField);  
  232.         final JButton loginOKButton = new JButton("確定");  
  233.         sysSetPanel.add(loginOKButton);  
  234.         loginOKButton.setActionCommand("loginOK");  
  235.         loginOKButton.addActionListener(new SysSetPanelOKListener());  
  236.         final JPanel ipPanel = new JPanel();  
  237.         final GridLayout gridLayout_2 = new GridLayout(01);  
  238.         gridLayout_2.setVgap(5);  
  239.         ipPanel.setLayout(gridLayout_2);  
  240.         ipPanel.setMaximumSize(new Dimension(60090));  
  241.         ipPanel.setBorder(new TitledBorder("IP搜索範圍"));  
  242.         sysSetPanel.add(ipPanel);  
  243.         final JPanel panel_5 = new JPanel();  
  244.         panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.X_AXIS));  
  245.         ipPanel.add(panel_5);  
  246.         panel_5.add(new JLabel("起始IP:"));  
  247.         ipStartTField = new JTextField(preferences.get("ipStart""192.168.0.1"));  
  248.         panel_5.add(ipStartTField);  
  249.         final JPanel panel_6 = new JPanel();  
  250.         panel_6.setLayout(new BoxLayout(panel_6, BoxLayout.X_AXIS));  
  251.         ipPanel.add(panel_6);  
  252.         panel_6.add(new JLabel("終止IP:"));  
  253.         ipEndTField = new JTextField(preferences.get("ipEnd""192.168.1.255"));  
  254.         panel_6.add(ipEndTField);  
  255.         final JButton ipOKButton = new JButton("確定");  
  256.         ipOKButton.setActionCommand("ipOK");  
  257.         ipOKButton.addActionListener(new SysSetPanelOKListener());  
  258.         sysSetPanel.add(ipOKButton);  
  259.         return scrollPane;  
  260.     }  
  261.     //----------------------------------------------------------------------------------   
  262.     private JScrollPane createUserList(){// 用戶列表面板   
  263.         JScrollPane scrollPane = new JScrollPane();  
  264.         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
  265.         addUserPopup(chatTree,getPopupMenu());// 爲用戶添加彈出菜單   
  266.         scrollPane.setViewportView(chatTree);  
  267.         scrollPane.setBorder(new EmptyBorder(0,0,0,0));  
  268.         chatTree.addMouseListener(new ChatTreeMouseListener());  
  269.           
  270.         return scrollPane;  
  271.     }  
  272.     //----------------------------------------------------------------------------------   
  273.     private JScrollPane createSysToolPanel() {// 系統工具面板   
  274.         JPanel sysToolPanel = new JPanel(); // 系統工具面板   
  275.         sysToolPanel.setLayout(new BorderLayout());  
  276.         JScrollPane sysToolScrollPanel = new JScrollPane();  
  277.         sysToolScrollPanel  
  278.                 .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
  279.         sysToolScrollPanel.setBorder(new EmptyBorder(0000));  
  280.         sysToolScrollPanel.setViewportView(sysToolPanel);  
  281.         sysToolPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));  
  282.         JPanel interfacePanel = new JPanel();  
  283.         sysToolPanel.add(interfacePanel, BorderLayout.NORTH);  
  284.         interfacePanel.setLayout(new BorderLayout());  
  285.         interfacePanel.setBorder(new TitledBorder("界面選擇-再次啓動生效"));  
  286.         faceList = new JList(new String[]{"當前系統""java默認"});  
  287.         interfacePanel.add(faceList);  
  288.         faceList.setBorder(new BevelBorder(BevelBorder.LOWERED));  
  289.         final JPanel interfaceSubPanel = new JPanel();  
  290.         interfaceSubPanel.setLayout(new FlowLayout());  
  291.         interfacePanel.add(interfaceSubPanel, BorderLayout.SOUTH);  
  292.         selectInterfaceOKButton = new JButton("確定");  
  293.         selectInterfaceOKButton.addActionListener(new ActionListener() {  
  294.             public void actionPerformed(ActionEvent e) {  
  295.                 preferences.put("lookAndFeel", faceList.getSelectedValue().toString());  
  296.                 JOptionPane.showMessageDialog(EQ.this"重新運行本軟件後生效");  
  297.             }  
  298.         });  
  299.         interfaceSubPanel.add(selectInterfaceOKButton);  
  300.   
  301.         JPanel searchUserPanel = new JPanel(); // 用戶搜索面板   
  302.         sysToolPanel.add(searchUserPanel);  
  303.         searchUserPanel.setLayout(new BorderLayout());  
  304.         final JPanel searchControlPanel = new JPanel();  
  305.         searchControlPanel.setLayout(new GridLayout(01));  
  306.         searchUserPanel.add(searchControlPanel, BorderLayout.SOUTH);  
  307.         final JList searchUserList = new JList(new String[]{"檢測用戶列表"});// 新添加用戶列表   
  308.         final JScrollPane scrollPane_2 = new JScrollPane(searchUserList);  
  309.         scrollPane_2.setDoubleBuffered(true);  
  310.         searchUserPanel.add(scrollPane_2);  
  311.         searchUserList.setBorder(new BevelBorder(BevelBorder.LOWERED));  
  312.         searchUserButton = new JToggleButton();  
  313.         searchUserButton.setText("搜索新用戶");  
  314.         searchUserButton.addActionListener(new SearchUserActionListener(searchUserList));  
  315.         searchControlPanel.add(progressBar);  
  316.         searchControlPanel.add(searchUserButton);  
  317.         searchUserPanel.setBorder(new TitledBorder("搜索用戶"));  
  318.   
  319.         final JPanel sysUpdatePanel = new JPanel();  
  320.         sysUpdatePanel.setOpaque(false);  
  321.         sysUpdatePanel.setLayout(new GridBagLayout());  
  322.         sysUpdatePanel.setBorder(new TitledBorder("系統操作"));  
  323.         sysToolPanel.add(sysUpdatePanel, BorderLayout.SOUTH);  
  324.         final JButton sysUpdateButton = new JButton("系統更新");  
  325.         final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();  
  326.         gridBagConstraints_1.gridx = 0;  
  327.         gridBagConstraints_1.gridy = 0;  
  328.         sysUpdatePanel.add(sysUpdateButton, gridBagConstraints_1);  
  329.         sysUpdateButton.addActionListener(new SysUpdateListener());// 添加系統更新事件   
  330.         final JLabel updateLabel = new JLabel("最近更新:");  
  331.         final GridBagConstraints updateLabelLayout = new GridBagConstraints();  
  332.         updateLabelLayout.gridy = 1;  
  333.         updateLabelLayout.gridx = 0;  
  334.         sysUpdatePanel.add(updateLabel, updateLabelLayout);  
  335.         final JLabel updateDateLabel = new JLabel();// 程序更新日期標籤   
  336.         Date date = new Date(localFile.lastModified());  
  337.         String dateStr = String.format("%tF %<tr", date);  
  338.         updateDateLabel.setText(dateStr);  
  339.         final GridBagConstraints updateDateLayout = new GridBagConstraints();  
  340.         updateDateLayout.gridy = 2;  
  341.         updateDateLayout.gridx = 0;  
  342.         sysUpdatePanel.add(updateDateLabel, updateDateLayout);  
  343.         final JLabel updateStaticLabel = new JLabel("更新狀態:");  
  344.         final GridBagConstraints updateStaticLayout = new GridBagConstraints();  
  345.         updateStaticLayout.gridy = 3;  
  346.         updateStaticLayout.gridx = 0;  
  347.         sysUpdatePanel.add(updateStaticLabel, updateStaticLayout);  
  348.         final JLabel updateInfoLabel = new JLabel();// 版本信息標籤   
  349.         checkSysInfo(updateInfoLabel);// 調用檢測版本更新的方法   
  350.         final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();  
  351.         gridBagConstraints_5.gridy = 4;  
  352.         gridBagConstraints_5.gridx = 0;  
  353.         sysUpdatePanel.add(updateInfoLabel, gridBagConstraints_5);  
  354.         JPanel statePanel = new JPanel();  
  355.         add(statePanel, BorderLayout.SOUTH);  
  356.         statePanel.setLayout(new BorderLayout());  
  357.         statePanel.add(stateLabel);  
  358.         stateLabel.setText("總人數:" + chatTree.getRowCount());  
  359.         return sysToolScrollPanel;  
  360.     }  
  361.     //----------------------------------------------------------------------------------   
  362.     private void initUserInfoButton(){  
  363.         try{  
  364.             String ip = InetAddress.getLocalHost().getHostAddress();  
  365.             User user = dao.getUser(ip);  
  366.             userInfoButton.setIcon(user.getIconImg());  
  367.             userInfoButton.setText(user.getName());  
  368.             userInfoButton.setIconTextGap(JLabel.RIGHT);  
  369.             userInfoButton.setToolTipText(user.getTipText());  
  370.             userInfoButton.getParent().doLayout();  
  371.         }catch(UnknownHostException e1){  
  372.             e1.printStackTrace();  
  373.         }  
  374.     }  
  375.     //----------------------------------------------------------------------------------   
  376.     private void showMessageBar(){// 顯示公告信息按鈕的線程   
  377.         new Thread(new Runnable() {  
  378.             public void run() {  
  379.                 while (true) {  
  380.                     if (!messageStack.empty()) {  
  381.                         try {  
  382.                             messageAlertButton.setIcon(messageAlertNullIcon);  
  383.                             messageAlertButton.setPreferredSize(new Dimension(2020));  
  384.                             Thread.sleep(500);  
  385.                             messageAlertButton.setIcon(messageAlertIcon);  
  386.                             Thread.sleep(500);  
  387.                         } catch (InterruptedException e) {  
  388.                             e.printStackTrace();  
  389.                         }  
  390.                     } else {  
  391.                         try {  
  392.                             Thread.sleep(3000);  
  393.                         } catch (InterruptedException e) {  
  394.                             e.printStackTrace();  
  395.                         }  
  396.                     }  
  397.                 }  
  398.             }  
  399.         }).start();  
  400.     }  
  401.     //----------------------------------------------------------------------------------   
  402.     private void checkSysInfo(final JLabel updateInfo){  
  403.         new Thread(new Runnable(){  
  404.             public void run(){  
  405.                 String info = "";  
  406.                 while(true){  
  407.                     try{  
  408.                         netFilePath = preferences.get("updatePath""EQ.jar");  
  409.                         if(netFilePath.equals("EQ.jar")){  
  410.                             info = "<html><center><font color=red><b>無法登錄</b><br>未設置升級路徑</font></center></html>";  
  411.                             updateInfo.setText(info);  
  412.                             continue;  
  413.                         }  
  414.                         netFile = new File(netFilePath);  
  415.                         if(netFile.exists()){  
  416.                             Date netDate = new Date(netFile.lastModified());  
  417.                             if(!localFile.exists()){  
  418.                                 info = "<html><font color=blue>本地程序位置出錯!</font></html>";  
  419.                             }else{  
  420.                                 Date localDate = new Date(localFile.lastModified());  
  421.                                 if(netDate.after(localDate)){  
  422.                                     info = "<html><font color=blue>網絡上有最新程序!</font></html>";  
  423.                                     pushMessage(info);  
  424.                                 }else{  
  425.                                     info = "<html><font color=green>現在是最新程序!</font></html>";  
  426.                                 }  
  427.                             }  
  428.                         }else{  
  429.                             info = "<html><center><font color=red><b>無法訪問</b><br>升級路徑</font></center></html>";  
  430.                         }  
  431.                         updateInfo.setText(info);  
  432.                         Thread.sleep(5*1000);  
  433.                     }catch(InterruptedException e){  
  434.                         e.printStackTrace();  
  435.                     }  
  436.                 }  
  437.             }  
  438.         }).start();  
  439.     }  
  440.     //----------------------------------------------------------------------------------   
  441.     class SearchUserActionListener implements ActionListener{  
  442.         private final JList list;  
  443.         SearchUserActionListener(JList list){  
  444.             this.list = list;  
  445.         }  
  446.   
  447.         @Override  
  448.         public void actionPerformed(ActionEvent e) {  
  449.             // TODO Auto-generated method stub   
  450.             if(searchUserButton.isSelected()){  
  451.                 searchUserButton.setText("停止搜索");  
  452.                 new Thread(new Runnable(){  
  453.                     public void run(){  
  454.                         Resource.searchUsers(chatTree, progressBar,list, searchUserButton);  
  455.                     }  
  456.                 }).start();  
  457.             }else{  
  458.                 searchUserButton.setText("搜索新用戶");  
  459.             }  
  460.               
  461.         }  
  462.     }  
  463.     //----------------------------------------------------------------------------------   
  464.     class SysSetPanelOKListener implements ActionListener{  
  465.   
  466.         @Override  
  467.         public void actionPerformed(ActionEvent e) {  
  468.             // TODO Auto-generated method stub   
  469.             String command = e.getActionCommand();  
  470.             if(command.equals("sysOK")){  
  471.                 String updatePath = updatePathTField.getText();  
  472.                 String placardPath = placardPathTField.getText();  
  473.                 String pubPath = pubPathTField.getText();  
  474.                 preferences.put("updatePath", updatePath); // 設置系統升級路徑   
  475.                 preferences.put("placardPath", placardPath);// 設置系統公告路徑   
  476.                 preferences.put("pubPath", pubPath); // 設置公共程序路徑   
  477.                 JOptionPane.showMessageDialog(EQ.this"系統設置保存完畢");  
  478.             }  
  479.             if (command.equals("loginOK")) {  
  480.                 String username = userNameTField.getText();  
  481.                 String password = new String(passwordTField.getPassword());  
  482.                 preferences.put("username", username); // 設置系統升級路徑   
  483.                 preferences.put("password", password);// 設置系統公告路徑   
  484.                 JOptionPane.showMessageDialog(EQ.this"登錄設置保存完畢");  
  485.             }  
  486.             if (command.equals("ipOK")) {  
  487.                 String ipStart = ipStartTField.getText();  
  488.                 String ipEnd = ipEndTField.getText();  
  489.                 try {  
  490.                     InetAddress.getByName(ipStart);  
  491.                     InetAddress.getByName(ipEnd);  
  492.                 } catch (UnknownHostException e1) {  
  493.                     JOptionPane.showMessageDialog(EQ.this"IP地址格式錯誤");  
  494.                     return;  
  495.                 }  
  496.                 preferences.put("ipStart", ipStart); // 設置系統升級路徑   
  497.                 preferences.put("ipEnd", ipEnd);// 設置系統公告路徑   
  498.                 JOptionPane.showMessageDialog(EQ.this"IP設置保存完畢");  
  499.             }  
  500.         }  
  501.     }  
  502.     //----------------------------------------------------------------------------------   
  503.     private final class SysUpdateListener implements ActionListener{// 系統更新事件   
  504.   
  505.         @Override  
  506.         public void actionPerformed(final ActionEvent e) {  
  507.             // TODO Auto-generated method stub   
  508.             String username = preferences.get("username"null);  
  509.             String password = preferences.get("password"null);  
  510.             if(username == null || password == null){  
  511.                 pushMessage("未設置登錄升級服務器的用戶名或密碼");  
  512.                 return;  
  513.             }  
  514.             Resource.loginPublic(username, password);  
  515.             updateProject();  
  516.         }  
  517.     }  
  518.     //----------------------------------------------------------------------------------   
  519.     private class ChatTreeMouseListener extends MouseAdapter{// 用戶列表的監聽器   
  520.         public void mouseClicked(final MouseEvent e){  
  521.             if (e.getClickCount() == 2) {  
  522.                 TreePath path = chatTree.getSelectionPath();  
  523.                 if (path == null)  
  524.                     return;  
  525.                 DefaultMutableTreeNode node = (DefaultMutableTreeNode) path  
  526.                         .getLastPathComponent();  
  527.                 User user = (User) node.getUserObject();  
  528.                 try {  
  529.                     TelFrame.getInstance(ss, new DatagramPacket(new byte[0], 0,  
  530.                             InetAddress.getByName(user.getIp()), 1111),  
  531.                             chatTree);  
  532.                 } catch (IOException e1) {  
  533.                     e1.printStackTrace();  
  534.                 }  
  535.             }  
  536.         }  
  537.     }  
  538.     //----------------------------------------------------------------------------------   
  539.     private void server(){// 服務器啓動方法   
  540.         new Thread(new Runnable(){  
  541.             public void run(){  
  542.                 while(true){  
  543.                     if(ss != null){  
  544.                         byte[] buf = new byte[4096];  
  545.                         DatagramPacket dp = new DatagramPacket(buf,buf.length);  
  546.                         try{  
  547.                             ss.receive(dp);  
  548.                         }catch(IOException e){  
  549.                             e.printStackTrace();  
  550.                         }  
  551.                         TelFrame.getInstance(ss,dp,chatTree);  
  552.                     }  
  553.                 }  
  554.             }  
  555.         }).start();  
  556.     }  
  557.     //----------------------------------------------------------------------------------   
  558.     private void addUserPopup(Component component, final JPopupMenu popup){// 添加用戶彈出菜單   
  559.         component.addMouseListener(new MouseAdapter(){  
  560.             public void mousePressed(MouseEvent e){  
  561.                 if(e.isPopupTrigger()){  
  562.                     showMenu(e);  
  563.                 }  
  564.             }  
  565.             public void mouseReleased(MouseEvent e) {  
  566.                 if (e.isPopupTrigger())  
  567.                     showMenu(e);  
  568.             }  
  569.             private void showMenu(MouseEvent e){  
  570.                 if(chatTree.getSelectionPaths() == null){  
  571.                     popupMenu.getComponent(0).setEnabled(false);  
  572.                     popupMenu.getComponent(2).setEnabled(false);  
  573.                     popupMenu.getComponent(3).setEnabled(false);  
  574.                     popupMenu.getComponent(4).setEnabled(false);  
  575.                     popupMenu.getComponent(5).setEnabled(false);  
  576.                 }else{  
  577.                     if(chatTree.getSelectionPaths().length < 2){  
  578.                         popupMenu.getComponent(3).setEnabled(false);  
  579.                     }else {  
  580.                         popupMenu.getComponent(3).setEnabled(true);  
  581.                     }  
  582.                     popupMenu.getComponent(0).setEnabled(true);  
  583.                     popupMenu.getComponent(2).setEnabled(true);  
  584.                     popupMenu.getComponent(4).setEnabled(true);  
  585.                     popupMenu.getComponent(5).setEnabled(true);  
  586.                 }  
  587.                 popup.show(e.getComponent(), e.getX(), e.getY());  
  588.             }  
  589.         });  
  590.     }  
  591.     //----------------------------------------------------------------------------------   
  592.     private void saveLocation(){// 保存主窗體位置的方法   
  593.         location = getBounds();  
  594.         dao.updateLocation(location);  
  595.     }  
  596.     //----------------------------------------------------------------------------------   
  597.     protected JPopupMenu getPopupMenu(){// 創建用戶彈出菜單   
  598.         if (popupMenu == null) {  
  599.             popupMenu = new JPopupMenu();  
  600.             popupMenu.setOpaque(false);  
  601.         }  
  602.         final JMenuItem rename = new JMenuItem();  
  603.         popupMenu.add(rename);  
  604.         rename.addActionListener(new RenameActionListener());  
  605.         rename.setText("更名");  
  606.         final JMenuItem addUser = new JMenuItem();  
  607.         addUser.addActionListener(new AddUserActionListener());  
  608.         popupMenu.add(addUser);  
  609.         addUser.setText("添加用戶");  
  610.         final JMenuItem delUser = new JMenuItem();  
  611.         delUser.addActionListener(new delUserActionListener());  
  612.         popupMenu.add(delUser);  
  613.         delUser.setText("刪除用戶");  
  614.         final JMenuItem messagerGroupSend = new JMenuItem();  
  615.         messagerGroupSend  
  616.                 .addActionListener(new messagerGroupSendActionListener());  
  617.         messagerGroupSend.setText("信使羣發");  
  618.         popupMenu.add(messagerGroupSend);  
  619.         final JMenuItem accessComputerFolder = new JMenuItem("訪問主機資源");  
  620.         accessComputerFolder.setActionCommand("computer");  
  621.         popupMenu.add(accessComputerFolder);  
  622.         accessComputerFolder  
  623.                 .addActionListener(new accessFolderActionListener());  
  624.         final JMenuItem accessPublicFolder = new JMenuItem();  
  625.         popupMenu.add(accessPublicFolder);  
  626.         accessPublicFolder.setOpaque(false);  
  627.         accessPublicFolder.setText("訪問公共程序");  
  628.         accessPublicFolder.setActionCommand("public");  
  629.         accessPublicFolder.addActionListener(new accessFolderActionListener());  
  630.         return popupMenu;  
  631.     }  
  632.     //----------------------------------------------------------------------------------   
  633.     private void updateProject(){// 程序更新方法   
  634.         netFilePath = preferences.get("updatePath""EQ.jar");  
  635.         if(netFilePath.equals("EQ.jar")){  
  636.             pushMessage("未設置升級路徑");  
  637.             return;  
  638.         }  
  639.         netFile = new File(netFilePath);  
  640.         localFile = new File(user_dir + File.separator + "EQ.jar");  
  641.         if (localFile != null && netFile != null && netFile.exists()  
  642.                 && localFile.exists()) {  
  643.             Date netDate = new Date(netFile.lastModified());  
  644.             Date localDate = new Date(localFile.lastModified());  
  645.             if (netDate.after(localDate)) {  
  646.                 new Thread(new Runnable() {  
  647.                     public void run() {  
  648.                         try {  
  649.                             Dialog frameUpdate = new UpdateFrame();  
  650.                             frameUpdate.setVisible(true);  
  651.                             Thread.sleep(2000);  
  652.                             FileInputStream fis = new FileInputStream(netFile);  
  653.                             FileOutputStream fout = new FileOutputStream(  
  654.                                     localFile);  
  655.                             int len = fis.available();  
  656.                             if (len > 0) {  
  657.                                 byte[] data = new byte[len];  
  658.                                 if (fis.read(data) > 0) {  
  659.                                     fout.write(data);  
  660.                                 }  
  661.                             }  
  662.                             fis.close();  
  663.                             fout.close();  
  664.                             frameUpdate.setVisible(false);  
  665.                             frameUpdate = null;  
  666.                             showMessageDialog("更新完畢,請重新啓動程序。");  
  667.                         } catch (Exception e) {  
  668.                             e.printStackTrace();  
  669.                         }  
  670.                     }  
  671.                 }).start();  
  672.             } else {  
  673.                 showMessageDialog("已經是最新的程序了。");  
  674.             }  
  675.         }  
  676.     }  
  677.     //----------------------------------------------------------------------------------   
  678.     private void checkPlacard() { // 檢測公告信息方法   
  679.         String placardDir = preferences.get("placardPath"null);  
  680.         if (placardDir == null) {  
  681.             pushMessage("未設置公告路徑");  
  682.             return;  
  683.         }  
  684.         File placard = new File(placardDir);  
  685.         try {  
  686.             if (placard.exists() && placard.isFile()) {  
  687.                 StringBuilder placardStr = new StringBuilder();  
  688.                 Scanner sc = new Scanner(new FileInputStream(placard));  
  689.                 while (sc.hasNextLine()) {  
  690.                     placardStr.append(sc.nextLine());  
  691.                 }  
  692.                 pushMessage(placardStr.toString());  
  693.             }  
  694.         } catch (FileNotFoundException e) {  
  695.             pushMessage("公告路徑錯誤,或公告文件不存在");  
  696.         }  
  697.     }  
  698.     //----------------------------------------------------------------------------------   
  699.     public void setStatic(String str){// 設置狀態欄信息   
  700.         if(stateLabel != null){  
  701.             stateLabel.setText(str);  
  702.         }  
  703.     }  
  704.       
  705.     public void pushMessage(String info){// 堆壓信息   
  706.         if(!messageStack.contains(info)){  
  707.             messageStack.push(info);  
  708.         }  
  709.     }  
  710.       
  711.     private void showMessageDialog(String message){  
  712.         JOptionPane.showMessageDialog(this, message);  
  713.     }  
  714.     //----------------------------------------------------------------------------------   
  715.     private String showInputDialog(String str){// 顯示輸入對話框   
  716.         String newName = JOptionPane.showInputDialog(this,  
  717.                 "<html>輸入<font color=red>" + str + "</font>的新名字</html>");  
  718.         return newName;  
  719.     }  
  720.     private class accessFolderActionListener implements ActionListener {// 訪問資源   
  721.         public void actionPerformed(final ActionEvent e) {  
  722.             TreePath path = chatTree.getSelectionPath();  
  723.             if (path == null)  
  724.                 return;  
  725.             DefaultMutableTreeNode node = (DefaultMutableTreeNode) path  
  726.                     .getLastPathComponent();  
  727.             User user = (User) node.getUserObject();  
  728.             String ip = "\\\\"+user.getIp();  
  729.             String command = e.getActionCommand();  
  730.             if (command.equals("computer")) {  
  731.                 Resource.startFolder(ip);  
  732.             }  
  733.             if (command.equals("public")) {  
  734.                 String serverPaeh = preferences.get("pubPath"null);  
  735.                 if (serverPaeh == null) {  
  736.                     pushMessage("未設置公共程序路徑");  
  737.                     return;  
  738.                 }  
  739.                 Resource.startFolder(serverPaeh);  
  740.             }  
  741.         }  
  742.     }  
  743.   
  744.     private class RenameActionListener implements ActionListener {// 更名   
  745.         public void actionPerformed(final ActionEvent e) {  
  746.             TreePath path = chatTree.getSelectionPath();  
  747.             if (path == null)  
  748.                 return;  
  749.             DefaultMutableTreeNode node = (DefaultMutableTreeNode) path  
  750.                     .getLastPathComponent();  
  751.             User user = (User) node.getUserObject();  
  752.             String newName = showInputDialog(user.getName());  
  753.             if (newName != null && !newName.isEmpty()) {  
  754.                 user.setName(newName);  
  755.                 dao.updateUser(user);  
  756.                 DefaultTreeModel model = (DefaultTreeModel) chatTree.getModel();  
  757.                 model.reload();  
  758.                 chatTree.setSelectionPath(path);  
  759.                 initUserInfoButton();  
  760.             }  
  761.         }  
  762.     }  
  763.     private class FrameWindowListener extends WindowAdapter {  
  764.         public void windowClosing(final WindowEvent e) {// 系統關閉事件   
  765.             setVisible(false);  
  766.         }  
  767.     }  
  768.     private class AddUserActionListener implements ActionListener {  
  769.         public void actionPerformed(final ActionEvent e) {// 添加用戶   
  770.             String ip = JOptionPane.showInputDialog(EQ.this"輸入新用戶IP地址");  
  771.             if (ip != null)  
  772.                 chatTree.addUser(ip, "add");  
  773.         }  
  774.     }  
  775.     private class delUserActionListener implements ActionListener {  
  776.         public void actionPerformed(final ActionEvent e) {// 刪除用戶   
  777.             chatTree.delUser();  
  778.         }  
  779.     }  
  780.     private class messagerGroupSendActionListener implements ActionListener {// 信使羣發   
  781.         public void actionPerformed(final ActionEvent e) {  
  782.             String message = JOptionPane.showInputDialog(EQ.this"請輸入羣發信息",  
  783.                     "信使羣發", JOptionPane.INFORMATION_MESSAGE);  
  784.             if (message != null && !message.equals("")) {  
  785.                 TreePath[] selectionPaths = chatTree.getSelectionPaths();  
  786.                 Resource.sendGroupMessenger(selectionPaths, message);  
  787.             } else if (message != null && message.isEmpty()) {  
  788.                 JOptionPane.showMessageDialog(EQ.this"不能發送空信息!");  
  789.             }  
  790.         }  
  791.     }  
  792.     private void SystemTrayInitial() { // 系統欄初始化   
  793.         if (!SystemTray.isSupported()) // 判斷當前系統是否支持系統欄   
  794.             return;  
  795.         try {  
  796.             String title = "TT";  
  797.             String company = "成都市同道科技有限公司";  
  798.             SystemTray sysTray = SystemTray.getSystemTray();  
  799.             Image image = Toolkit.getDefaultToolkit().getImage(  
  800.                     EQ.class.getResource("/icons/sysTray.png"));// 系統欄圖標   
  801.             trayicon = new TrayIcon(image, title + "\n" + company, createMenu());  
  802.             trayicon.setImageAutoSize(true);  
  803.             trayicon.addActionListener(new SysTrayActionListener());  
  804.             sysTray.add(trayicon);  
  805.             trayicon.displayMessage(title, company, MessageType.INFO);  
  806.         } catch (Exception e) {  
  807.             e.printStackTrace();  
  808.         }  
  809.     }  
  810.     private PopupMenu createMenu() { // 創建系統欄菜單的方法   
  811.         PopupMenu menu = new PopupMenu();  
  812.         MenuItem exitItem = new MenuItem("退出");  
  813.         exitItem.addActionListener(new ActionListener() { // 系統欄退出事件   
  814.                     public void actionPerformed(ActionEvent e) {  
  815.                         System.exit(0);  
  816.                     }  
  817.                 });  
  818.         MenuItem openItem = new MenuItem("打開");  
  819.         openItem.addActionListener(new ActionListener() {// 系統欄打開菜單項事件   
  820.                     public void actionPerformed(ActionEvent e) {  
  821.                         if (!isVisible()) {  
  822.                             setVisible(true);  
  823.                             toFront();  
  824.                         } else  
  825.                             toFront();  
  826.                     }  
  827.                 });  
  828.   
  829.         // 系統欄的訪問服務器菜單項事件   
  830.         MenuItem publicItem = new MenuItem("訪問服務器");  
  831.         publicItem.addActionListener(new ActionListener() {  
  832.             public void actionPerformed(ActionEvent e) {  
  833.                 String serverPaeh = preferences.get("pubPath"null);  
  834.                 if (serverPaeh == null) {  
  835.                     pushMessage("未設置公共程序路徑");  
  836.                     return;  
  837.                 }  
  838.                 Resource.startFolder(serverPaeh);  
  839.             }  
  840.         });  
  841.         menu.add(publicItem);  
  842.         menu.add(openItem);  
  843.         menu.addSeparator();  
  844.         menu.add(exitItem);  
  845.         return menu;  
  846.     }  
  847.     class SysTrayActionListener implements ActionListener {// 系統欄雙擊事件   
  848.         public void actionPerformed(ActionEvent e) {  
  849.             setVisible(true);  
  850.             toFront();  
  851.         }  
  852.     }  
  853. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章