分享

Java 客户端服务器程序 学习笔记

 醉三郎 2014-03-19
Java代码  收藏代码
  1. //Server.java  
  2.   
  3. package com.mulThread;  
  4.   
  5. import java.awt.*;  
  6. import java.awt.event.*;  
  7. import javax.swing.*;  
  8. import java.net.*;  
  9. import java.io.*;  
  10. import java.util.*;  
  11.   
  12. /** 
  13.  * 
  14.  * @E-mail:youngmaster.fly@gmail.com 
  15.  * 
  16.  * @author:young master 
  17.  *  
  18.  * @version 1.0 
  19.  *  
  20.  * @any questions,please reply to me for deeping improvements 
  21.  */  
  22.   
  23. public class Server extends JFrame {  
  24.     JPanel contentPane;  
  25.   
  26.     JMenuBar jMenuBar1 = new JMenuBar();  
  27.   
  28.     JMenu jMenuFile = new JMenu();  
  29.   
  30.     JMenuItem jMenuFileExit = new JMenuItem();  
  31.   
  32.     JMenu jMenuHelp = new JMenu();  
  33.   
  34.     JMenuItem jMenuHelpAbout = new JMenuItem();  
  35.   
  36.     JLabel statusBar = new JLabel();  
  37.   
  38.     BorderLayout borderLayout1 = new BorderLayout();  
  39.   
  40.     JPanel jPanel1 = new JPanel();  
  41.   
  42.     BorderLayout borderLayout2 = new BorderLayout();  
  43.   
  44.     JLabel jLabel1 = new JLabel();  
  45.   
  46.     static java.awt.List jList1 = new java.awt.List(13);  
  47.   
  48.     JScrollPane scrollpane = new JScrollPane(jList1);  
  49.   
  50.     // 以下为网络相关变量  
  51.     static Vector clients = new Vector(10); // 用vector向量数组存储连接客户变量  
  52.   
  53.     static ServerSocket server = null// 建立服务器socket  
  54.   
  55.     static int active_connects = 0// 用来存储目前连接的客户数  
  56.   
  57.     static Socket socket = null// 用来存储一个套接字连接  
  58.   
  59.     // chatServer main method  
  60.     public static void main(String[] args) {  
  61.         try {  
  62.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.         Server chatServer1 = new Server(); // 实例化一个chatServer类  
  67.         chatServer1.show();  
  68.         System.out.println("Server starting ...");  
  69.   
  70.         try {  
  71.             server = new ServerSocket(2525); // 使用端口2525初始化服务器套接字  
  72.         } catch (IOException e) {  
  73.             System.out.println("Error:" + e);  
  74.         }  
  75.         while (true) {  
  76.   
  77.             if (clients.size() < 5// 当客户数小于5个时开始连接  
  78.             {  
  79.                 try {  
  80.                     socket = server.accept(); // 用来存储连接上的客户socket  
  81.                     if (socket != null) {  
  82.                         System.out.println(socket + "连接"); // 在控制台打印客户连接信息  
  83.   
  84.                     }  
  85.                 } catch (IOException e) {  
  86.                     System.out.println("Error:" + e);  
  87.                 }  
  88.                 int i = 0;  
  89.   
  90.                 do {  
  91.   
  92.                     Client c = new Client(socket); // 定义并实例化一个Client线程类,一个就对应一个客户连接  
  93.                     clients.addElement(c); // 加入clients数组中  
  94.                     if (checkName(c)) // 调用checkName方法验证c的合法性  
  95.                     {  
  96.                         int connum = ++chatServer1.active_connects; // 定义connum来存储活动连接数  
  97.                         String constr = "目前有" + connum + "客户相连"// 在状态栏里显示连接数  
  98.                         chatServer1.statusBar.setText(constr);  
  99.                         Client listdata = (Client) clients.elementAt(i); // 将连接客户的socket信息存储进listdata数组  
  100.   
  101.                         chatServer1.jList1.addItem(listdata.ip + "连接", i); // 将客户socket信息写入list框  
  102.                         c.start(); // 启动线程  
  103.                         notifyRoom(); // 用notifyRoom方法来监视聊天室连接变化  
  104.                         // 不断改变clients数组并刷新客户端信息  
  105.   
  106.                     } else {  
  107.                         // 如果名字不合法  
  108.                         c.ps.println("TAKEN");  
  109.   
  110.                         disconnect(c);  
  111.   
  112.                     }  
  113.                     i++;  
  114.                     break;  
  115.   
  116.                 } while (i < clients.size());  
  117.   
  118.             } else // 如果clients数组超过了5个  
  119.             {  
  120.                 try {  
  121.                     Thread.sleep(200);  
  122.                 } catch (InterruptedException e) {  
  123.                 }  
  124.             }  
  125.         }// end of while  
  126.     }// end of main method  
  127.   
  128.     /** Construct the frame */  
  129.     public Server() // chatServer类的构造器用来初始化一些UI信息  
  130.     {  
  131.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  132.         try {  
  133.             jbInit();  
  134.         } catch (Exception e) {  
  135.             e.printStackTrace();  
  136.         }  
  137.     }  
  138.   
  139.     /** Component initialization */  
  140.     private void jbInit() throws Exception {  
  141.         // setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your  
  142.         // Icon]")));  
  143.         contentPane = (JPanel) this.getContentPane();  
  144.         contentPane.setLayout(borderLayout1);  
  145.         this.setSize(new Dimension(400300));  
  146.         this.setTitle("简易聊天服务器端");  
  147.         statusBar.setText("目前的连接数为:");  
  148.   
  149.         jMenuFile.setText("File");  
  150.         jMenuFileExit.setText("Exit");  
  151.         jMenuFileExit.addActionListener(new ActionListener() {  
  152.             public void actionPerformed(ActionEvent e) {  
  153.                 jMenuFileExit_actionPerformed(e);  
  154.             }  
  155.         });  
  156.         jMenuHelp.setText("Help");  
  157.         jMenuHelpAbout.setText("About");  
  158.         jMenuHelpAbout.addActionListener(new ActionListener() {  
  159.             public void actionPerformed(ActionEvent e) {  
  160.                 jMenuHelpAbout_actionPerformed(e);  
  161.             }  
  162.         });  
  163.   
  164.         jPanel1.setLayout(borderLayout2);  
  165.         jLabel1.setText("服务器端连接客户");  
  166.         jMenuFile.add(jMenuFileExit);  
  167.         jMenuHelp.add(jMenuHelpAbout);  
  168.         jMenuBar1.add(jMenuFile);  
  169.         jMenuBar1.add(jMenuHelp);  
  170.         this.setJMenuBar(jMenuBar1);  
  171.         contentPane.add(statusBar, BorderLayout.SOUTH);  
  172.         contentPane.add(jPanel1, BorderLayout.CENTER);  
  173.         jPanel1.add(jLabel1, BorderLayout.NORTH);  
  174.         jPanel1.add(scrollpane, BorderLayout.CENTER);  
  175.   
  176.     }// end of jbinit  
  177.   
  178.     /** File | Exit action performed */  
  179.     public void jMenuFileExit_actionPerformed(ActionEvent e) // 实现退出菜单方法  
  180.     {  
  181.         sendClients(new StringBuffer("QUIT")); // 向客户端发送断开连接信息  
  182.         closeAll(); // 调用closeAll方法关闭所有连接  
  183.         System.exit(0);  
  184.     }  
  185.   
  186.     /** Help | About action performed */  
  187.     public void jMenuHelpAbout_actionPerformed(ActionEvent e) // 实现about对话框,意义不大,可以去掉  
  188.     {  
  189.         AboutChat dlg = new AboutChat(this);  
  190.         Dimension dlgSize = dlg.getPreferredSize();  
  191.         Dimension frmSize = getSize();  
  192.         Point loc = getLocation();  
  193.         dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,  
  194.                 (frmSize.height - dlgSize.height) / 2 + loc.y);  
  195.         dlg.setModal(true);  
  196.         dlg.show();  
  197.     }  
  198.   
  199.     /** Overridden so we can exit when window is closed */  
  200.     protected void processWindowEvent(WindowEvent e) // 实现关闭服务器程序要进行的操作  
  201.     {  
  202.         super.processWindowEvent(e);  
  203.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {  
  204.             jMenuFileExit_actionPerformed(null);  
  205.         }  
  206.     }  
  207.   
  208.     /* 以下实现各种方法 */  
  209.     public static void notifyRoom() // 用来监视连接信息,不断刷新clients数组并刷新客户端用户列表信息  
  210.     {  
  211.         StringBuffer people = new StringBuffer("PEOPLE");  
  212.         for (int i = 0; i < clients.size(); i++) {  
  213.             Client c = (Client) clients.elementAt(i);  
  214.             people.append(":" + c.name);  
  215.   
  216.         }  
  217.         sendClients(people); // 用sendClients方法向客户端发送信息  
  218.     }  
  219.   
  220.     public static synchronized void sendClients(StringBuffer msg) // 实现sendClients方法专用来向每个连接的客户端发送信息  
  221.     {  
  222.         for (int i = 0; i < clients.size(); i++) {  
  223.             Client c = (Client) clients.elementAt(i);  
  224.             c.send(msg);  
  225.         }  
  226.     }  
  227.   
  228.     public static void closeAll() // 实现关闭所有连接信息  
  229.     {  
  230.         while (clients.size() > 0// 遍历clients数组删除所有连接客户信息  
  231.         {  
  232.             Client c = (Client) clients.firstElement();  
  233.             try {  
  234.                 c.socket.close();  
  235.             } catch (IOException e) {  
  236.                 System.out.println("Error:" + e);  
  237.             } finally {  
  238.                 clients.removeElement(c);  
  239.             }  
  240.         }// end of while  
  241.     }// end of closeAll method  
  242.   
  243.     public static boolean checkName(Client newclient) // 实现检查连接客户的socket信息是否合法  
  244.     {  
  245.         for (int i = 0; i < clients.size(); i++) {  
  246.             Client c = (Client) clients.elementAt(i);  
  247.             if ((c != newclient) && c.equals(newclient.name))  
  248.                 return false;  
  249.         }  
  250.         return (true);  
  251.     }// end of checkName method  
  252.   
  253.     public static synchronized void disconnect(Client c) // 实现断开单个客户的方法  
  254.     {  
  255.         try {  
  256.             jList1.addItem(c.ip + "断开连接"); // 在服务器端程序的list框中显示断开信息  
  257.   
  258.             Server.active_connects--; // 将连接数减1  
  259.             c.send(new StringBuffer("QUIT")); // 向客户发送断开连接信息  
  260.             c.socket.close();  
  261.   
  262.         } catch (IOException e) {  
  263.             System.out.println("Error:" + e);  
  264.         } finally {  
  265.             clients.removeElement(c); // 从clients数组中删除此客户的相关socket等信息  
  266.         }  
  267.     }  
  268.   
  269. }  
  270.   
  271. class Client extends Thread // 实现 Client线程类  
  272. {  
  273.     Socket socket; // 用来存储一个连接客户的socket信息  
  274.   
  275.     String name; // 用来存储客户的连接姓名  
  276.   
  277.     String ip; // 用来存储客户的ip信息  
  278.   
  279.     DataInputStream dis; // 用来实现接受从客户端发来的数据流  
  280.   
  281.     PrintStream ps; // 用来实现向客户端发送信息的打印流  
  282.   
  283.     public void send(StringBuffer msg) // 实现想客户端发送信息的方法  
  284.     {  
  285.         ps.println(msg); // 用打印流发送信息  
  286.         ps.flush();  
  287.     }  
  288.   
  289.     public Client(Socket s) // Client线程类的构造器  
  290.     {  
  291.         socket = s;  
  292.         try {  
  293.             dis = new DataInputStream(s.getInputStream()); // 存储特定客户socket的输入流接受s这个客户发送到服务器端的信息  
  294.             ps = new PrintStream(s.getOutputStream()); // 存储特定客户socket的输出流发送服务器给s这个客户的信息  
  295.             String info = dis.readLine(); // 读取接受来的信息  
  296.   
  297.             StringTokenizer stinfo = new StringTokenizer(info, ":"); // 用StringTokenizer类来读取用":"分段字符  
  298.             String head = stinfo.nextToken(); // head用来存储类似于关键字的头信息  
  299.             if (stinfo.hasMoreTokens())  
  300.                 name = stinfo.nextToken(); // 关键字后的第二段数据是客户名信息  
  301.             if (stinfo.hasMoreTokens())  
  302.                 ip = stinfo.nextToken(); // 关键字后的第三段数据是客户ip信息  
  303.             System.out.println(head); // 在控制台打印头信息  
  304.         } catch (IOException e) {  
  305.             System.out.println("Error:" + e);  
  306.         }  
  307.     }// end of Client constrator  
  308.   
  309.     public void run() // 线程运行方法  
  310.     {  
  311.         while (true) {  
  312.             String line = null;  
  313.             try {  
  314.                 line = dis.readLine(); // 读取客户端发来的数据流  
  315.   
  316.             } catch (IOException e) {  
  317.                 System.out.println("Error" + e);  
  318.                 Server.disconnect(this);  
  319.                 Server.notifyRoom();  
  320.                 return;  
  321.             }  
  322.   
  323.             if (line == null// 客户已离开  
  324.             {  
  325.                 Server.disconnect(this);  
  326.                 Server.notifyRoom();  
  327.                 return;  
  328.             }  
  329.   
  330.             StringTokenizer st = new StringTokenizer(line, ":");  
  331.             String keyword = st.nextToken();  
  332.   
  333.             if (keyword.equals("MSG")) // 如果关键字是MSG则是客户端发来的聊天信息  
  334.             {  
  335.                 StringBuffer msg = new StringBuffer("MSG:"); // 在服务器端再重新建立一个字符缓冲  
  336.                 msg.append(name);  
  337.                 msg.append(st.nextToken("\0"));  
  338.                 Server.sendClients(msg); // 再将某个客户发来的聊天信息发送到每个连接客户的聊天栏中  
  339.             } else if (keyword.equals("QUIT")) // 如果关键字是QUIT则是客户端发来断开连接的信息  
  340.             {  
  341.   
  342.                 Server.disconnect(this); // 服务器断开与这个客户的连接  
  343.                 Server.notifyRoom(); // 继续监听聊天室并刷新其他客户的聊天人名list  
  344.                 this.stop();  
  345.             }  
  346.         }  
  347.     }  
  348. // end of class Client  
  349.   
  350. //ClientChat.java  
  351.   
  352. package com.mulThread;  
  353.   
  354. import java.awt.*;  
  355. import java.awt.event.*;  
  356. import java.applet.*;  
  357. import java.net.*;  
  358. import java.io.*;  
  359. import java.util.*;  
  360.   
  361. /** 
  362. * 
  363. * @E-mail:youngmaster.fly@gmail.com 
  364. * 
  365. * @author:young master 
  366.  
  367. * @version 1.0 
  368.  
  369. * @any questions,please reply to me for deeping improvements 
  370. */  
  371.   
  372. public class ClientChat extends Applet {  
  373.     /* 以下用于定义UI变量 */  
  374.     Panel panel1 = new Panel(); // 用于放置输入姓名和连接两个按钮  
  375.   
  376.     BorderLayout borderLayout1 = new BorderLayout();  
  377.   
  378.     Panel panel2 = new Panel(); // 用于放置聊天信息显示和聊天人员列表  
  379.   
  380.     Panel panel3 = new Panel(); // 用于放置发送信息区域  
  381.   
  382.     FlowLayout flowLayout1 = new FlowLayout();  
  383.   
  384.     FlowLayout flowLayout2 = new FlowLayout();  
  385.   
  386.     Label label1 = new Label();  
  387.   
  388.     TextField name_txt = new TextField(15);  
  389.   
  390.     Button button1 = new Button();  
  391.   
  392.     Button button2 = new Button();  
  393.   
  394.     TextArea chat_txt = new TextArea(1530);  
  395.   
  396.     Label label2 = new Label();  
  397.   
  398.     Button button3 = new Button();  
  399.   
  400.     TextField msg_txt = new TextField(20);  
  401.   
  402.     java.awt.List list1 = new java.awt.List(13);  
  403.   
  404.     /* 以下定义数据流和网络变量 */  
  405.     Socket soc = null// 定义连接套接字  
  406.   
  407.     PrintStream ps = null// 定义打印流  
  408.   
  409.     Listen listen = null// 定义一个客户端线程  
  410.   
  411.     public void init() // 初始化图形界面  
  412.     {  
  413.         resize(475350);  
  414.         this.setLayout(borderLayout1);  
  415.         panel2.setLayout(flowLayout1);  
  416.         panel3.setLayout(flowLayout2);  
  417.         label1.setText("用户名:");  
  418.   
  419.         button1.setLabel("连接");  
  420.         button2.setLabel("断开连接");  
  421.   
  422.         chat_txt.setEditable(false);  
  423.         panel2.setBackground(Color.cyan);  
  424.         panel1.setBackground(Color.cyan);  
  425.         label2.setText("聊天信息:");  
  426.         button3.setLabel("发送");  
  427.         msg_txt.setText("请输入聊天信息");  
  428.         panel3.setBackground(Color.cyan);  
  429.         this.add(panel1, BorderLayout.NORTH);  
  430.         panel1.add(label1, null);  
  431.         panel1.add(name_txt, null);  
  432.         panel1.add(button1, null);  
  433.         panel1.add(button2, null);  
  434.         this.add(panel2, BorderLayout.CENTER);  
  435.         panel2.add(chat_txt, null);  
  436.         panel2.add(list1, null);  
  437.         this.add(panel3, BorderLayout.SOUTH);  
  438.         panel3.add(label2, null);  
  439.         panel3.add(msg_txt, null);  
  440.         panel3.add(button3, null);  
  441.     }  
  442.   
  443.     public boolean action(Event evt, Object obj) // 事件触发代码  
  444.     {  
  445.         if (evt.target instanceof Button) {  
  446.             String label = (String) obj;  
  447.             if (label.equals("连接")) // 如果点击连接后  
  448.             {  
  449.                 if (soc == null) {  
  450.                     try {  
  451.                         soc = new Socket(InetAddress.getLocalHost(), 2525); // 使用端口2525实例化一个本地套接字  
  452.                         System.out.println(soc); // 在控制台打印实例化的结果  
  453.                         ps = new PrintStream(soc.getOutputStream()); // 将ps指向soc的输出流  
  454.                         StringBuffer info = new StringBuffer("INFO: "); // 定义一个字符缓冲存储发送信息  
  455.                         // 其中INFO为关键字让服务器识别为连接信息  
  456.                         // 并将name和ip用":"分开,在服务器端将用一个  
  457.                         // StringTokenizer类来读取数据  
  458.                         String userinfo = name_txt.getText() + ":"  
  459.                                 + InetAddress.getLocalHost().toString();  
  460.                         ps.println(info.append(userinfo));  
  461.   
  462.                         ps.flush();  
  463.                         listen = new Listen(this, name_txt.getText(), soc); // 将客户端线程实例化  
  464.                         listen.start(); // 启动线程  
  465.                     } catch (IOException e) {  
  466.                         System.out.println("Error:" + e);  
  467.                         disconnect();  
  468.                     }  
  469.                 } // end of if  
  470.             }// end of if  
  471.             else if (label.equals("断开连接")) // 如果点击断开连接按钮则运行disconnect()  
  472.             {  
  473.                 disconnect();  
  474.             } else if (label.equals("发送")) // 如果点击发送按钮  
  475.             {  
  476.                 if (soc != null) {  
  477.                     StringBuffer msg = new StringBuffer("MSG: "); // 定义并实例化一个字符缓冲存储发送的聊天信息  
  478.                     // 其中MSG为关键词  
  479.                     try {  
  480.                         String msgtxt = new String(msg_txt.getText());  
  481.                     } catch (Exception e) {  
  482.                     }  
  483.   
  484.                     ps.println(msg.append(msg_txt.getText())); // 用打印流发送聊天信息  
  485.                     ps.flush();  
  486.                 }  
  487.             }  
  488.         }  
  489.         return true;  
  490.     } // end of method action  
  491.   
  492.     public void disconnect() // 客户端点击断开连接要运行的方法  
  493.     {  
  494.         if (soc != null) {  
  495.             try {  
  496.                 listen.suspend();  
  497.                 ps.println("QUIT"); // 用打印流发送QUIT信息通知服务器断开此次通信  
  498.                 ps.flush();  
  499.                 soc.close(); // 关闭套接字  
  500.             } catch (IOException e) {  
  501.                 System.out.println("Error:" + e);  
  502.             } finally {  
  503.   
  504.             }  
  505.         }// end of if  
  506.     }  
  507.   
  508.     class Listen extends Thread // 客户端线程类用来监听服务器传来的信息  
  509.     {  
  510.         String name = null// 用来存储客户端连接后的name信息  
  511.   
  512.         DataInputStream dis = null// 用来实现客户端接受服务器数据的输入流  
  513.   
  514.         PrintStream ps = null// 用来实现从客户端发送数据到服务器的打印流  
  515.   
  516.         Socket socket = null// 用来存储客户端的socket信息  
  517.   
  518.         ClientChat parent = null// 用来存储当前运行的chatApplet实例  
  519.   
  520.         public Listen(ClientChat p, String n, Socket s) // Listen类的构造器  
  521.         {  
  522.             // 接受参数  
  523.             parent = p;  
  524.             name = n;  
  525.             socket = s;  
  526.   
  527.             try {  
  528.                 // 实例化两个数据流  
  529.                 dis = new DataInputStream(s.getInputStream());  
  530.                 ps = new PrintStream(s.getOutputStream());  
  531.   
  532.             } catch (IOException e) {  
  533.                 System.out.println("Error:" + e);  
  534.                 parent.disconnect();  
  535.             }  
  536.         } // end of Listen constractor  
  537.   
  538.         public void run() // 线程运行方法  
  539.         {  
  540.             String msg = null;  
  541.             while (true) {  
  542.                 try {  
  543.                     msg = dis.readLine();  
  544.                 } // 读取从服务器传来的信息  
  545.                 catch (IOException e) {  
  546.                     System.out.println("Error:" + e);  
  547.                     parent.disconnect();  
  548.                 }  
  549.                 if (msg == null// 如果从服务器传来的信息为空则断开此次连接  
  550.                 {  
  551.                     parent.listen = null;  
  552.                     parent.soc = null;  
  553.                     parent.list1.clear();  
  554.                     return;  
  555.                 }  
  556.                 StringTokenizer st = new StringTokenizer(msg, ":"); // 用StringTokenizer类来实现读取分段字符  
  557.                 String keyword = st.nextToken(); // 读取信息头即关键字用来识别是何种信息  
  558.   
  559.                 if (keyword.equals("PEOPLE")) // 如果是PEOPLE则是服务器发来的客户连接信息  
  560.                 // 主要用来刷新客户端的用户列表  
  561.                 {  
  562.                     parent.list1.clear();  
  563.                     while (st.hasMoreTokens()) // 遍历st取得目前所连接的客户  
  564.                     {  
  565.                         String str = st.nextToken();  
  566.                         parent.list1.addItem(str);  
  567.                     }  
  568.                 } else if (keyword.equals("MSG")) // 如果关键字是MSG则是服务器传来的聊天信息  
  569.                 // 主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来  
  570.                 {  
  571.                     String usr = st.nextToken();  
  572.                     parent.chat_txt.appendText(usr);  
  573.                     parent.chat_txt.appendText(st.nextToken("\0"));  
  574.                     parent.chat_txt.appendText("\n\n");  
  575.                 } else if (keyword.equals("QUIT")) // 如果关键字是QUIT则是服务器关闭的信息  
  576.                 // 用来切断此次连接  
  577.                 {  
  578.                     System.out.println("Quit");  
  579.                     try {  
  580.                         parent.listen.stop();  
  581.                         parent.listen = null;  
  582.                         parent.soc.close();  
  583.                         parent.soc = null;  
  584.                     } catch (IOException e) {  
  585.                         System.out.println("Error:" + e);  
  586.                     }  
  587.                     parent.list1.clear();  
  588.   
  589.                     return;  
  590.                 }  
  591.             }  
  592.   
  593.         } // end of run method  
  594.     } // end of Listen inner class  
  595.   
  596. // end of chatApplet class  
  597.   
  598. //AboutChat.java  
  599.   
  600. package com.mulThread;  
  601.   
  602. import java.awt.*;  
  603. import java.awt.event.*;  
  604. import javax.swing.*;  
  605. import javax.swing.border.*;  
  606.   
  607. /** 
  608. * 
  609. * @E-mail:youngmaster.fly@gmail.com 
  610. * 
  611. * @author:young master 
  612.  
  613. * @version 1.0 
  614.  
  615. * @any questions,please reply to me for deeping improvements 
  616. */  
  617.   
  618. public class AboutChat extends JDialog implements ActionListener {  
  619.   
  620.     JPanel panel1 = new JPanel();  
  621.   
  622.     JPanel panel2 = new JPanel();  
  623.   
  624.     JPanel insetsPanel1 = new JPanel();  
  625.   
  626.     JPanel insetsPanel2 = new JPanel();  
  627.   
  628.     JPanel insetsPanel3 = new JPanel();  
  629.   
  630.     JButton button1 = new JButton();  
  631.   
  632.     JLabel imageLabel = new JLabel();  
  633.   
  634.     JLabel label1 = new JLabel();  
  635.   
  636.     JLabel label2 = new JLabel();  
  637.   
  638.     JLabel label3 = new JLabel();  
  639.   
  640.     JLabel label4 = new JLabel();  
  641.   
  642.     BorderLayout borderLayout1 = new BorderLayout();  
  643.   
  644.     BorderLayout borderLayout2 = new BorderLayout();  
  645.   
  646.     FlowLayout flowLayout1 = new FlowLayout();  
  647.   
  648.     GridLayout gridLayout1 = new GridLayout();  
  649.   
  650.     String product = "product:少爷精简版--聊天服务器端";  
  651.   
  652.     String author = "author:young master   " +  
  653.             "any questions,please reply to me for deeping improvements";  
  654.   
  655.     String email = "@E-mail:youngmaster.fly@gmail.com";  
  656.   
  657.     String comments = "本聊天室服务器端实现了多线程客户连接和显示连接信息";  
  658.   
  659.     public AboutChat(Frame parent) {  
  660.         super(parent);  
  661.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  662.         try {  
  663.             jbInit();  
  664.         } catch (Exception e) {  
  665.             e.printStackTrace();  
  666.         }  
  667.         pack();  
  668.     }  
  669.   
  670.     private void jbInit() throws Exception {  
  671.         //imageLabel.setIcon(new ImageIcon(Frame1_AboutBox.class.getResource("[Your Image]")));  
  672.         this.setTitle("少爷精简版--聊天服务器端");  
  673.         setResizable(false);  
  674.         panel1.setLayout(borderLayout1);  
  675.         panel2.setLayout(borderLayout2);  
  676.         insetsPanel1.setLayout(flowLayout1);  
  677.         insetsPanel2.setLayout(flowLayout1);  
  678.         insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10101010));  
  679.         gridLayout1.setRows(4);  
  680.         gridLayout1.setColumns(1);  
  681.         label1.setText(product);  
  682.         label2.setText(author);  
  683.         label3.setText(email);  
  684.         label4.setText(comments);  
  685.         insetsPanel3.setLayout(gridLayout1);  
  686.         insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10601010));  
  687.         button1.setText("Ok");  
  688.         button1.addActionListener(this);  
  689.         insetsPanel2.add(imageLabel, null);  
  690.         panel2.add(insetsPanel2, BorderLayout.WEST);  
  691.         this.getContentPane().add(panel1, null);  
  692.         insetsPanel3.add(label1, null);  
  693.         insetsPanel3.add(label2, null);  
  694.         insetsPanel3.add(label3, null);  
  695.         insetsPanel3.add(label4, null);  
  696.         panel2.add(insetsPanel3, BorderLayout.CENTER);  
  697.         insetsPanel1.add(button1, null);  
  698.         panel1.add(insetsPanel1, BorderLayout.SOUTH);  
  699.         panel1.add(panel2, BorderLayout.NORTH);  
  700.     }  
  701.   
  702.     /**Overridden so we can exit when window is closed*/  
  703.     protected void processWindowEvent(WindowEvent e) {  
  704.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {  
  705.             cancel();  
  706.         }  
  707.         super.processWindowEvent(e);  
  708.     }  
  709.   
  710.     /**Close the dialog*/  
  711.     void cancel() {  
  712.         dispose();  
  713.     }  
  714.   
  715.     /**Close the dialog on a button event*/  
  716.     public void actionPerformed(ActionEvent e) {  
  717.         if (e.getSource() == button1) {  
  718.             cancel();  
  719.         }  
  720.     }  
  721. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章