首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

有好手可以帮忙写一写客户端的注释吗

2011-11-29 
有高手可以帮忙写一写客户端的注释吗?importjavax.swing.*importjava.awt.event.*importjava.io.*impor

有高手可以帮忙写一写客户端的注释吗?
import       javax.swing.*;      
    import       java.awt.event.*;      
    import       java.io.*;      
    import       java.net.*;      
    import       java.awt.*;      
       
class       ClientPoint       extends       JFrame       implements       ActionListener,KeyListener{      
            static       int       SERVERPORT=8088;       //还是这里不明白
            static       String       SERVERADDRESS= "127.0.0.1 ";      
            private       BufferedReader       cin=null;      
            private       PrintWriter       cout=null;      
            private       Socket       client=null;      
            public       String       clientname;      
            private       JPanel       jPanel1=new       JPanel();      
            private       JTextField       jTextSendInfo=new       JTextField(38);      
            private       JTextArea       jTextGetInfo=new       JTextArea(16,33);              
            public       JScrollPane       areaScrollPane=new       JScrollPane(jTextGetInfo);              
            private       Button       Send=new       Button( "发送 ");      
            private       Button       Link=new       Button( "连接 ");      
               
            public       ClientPoint(){              
                    super( "聊天室^_^ ");                      
                enableEvents(AWTEvent.WINDOW_EVENT_MASK);       //不知道?                
                    setSize(480,465);      
                    jPanel1.add( "South ",jTextSendInfo);      
                    jPanel1.setBackground(new       Color(75,       234,       166));      
    jTextGetInfo.setBackground(new       Color(122,150,223));      


                    jTextGetInfo.setFont(new       java.awt.Font( "Dialog ",       0,       15));      
                    jTextGetInfo.setForeground(new       Color(176,       42,       0));      
                    this.setResizable(false);      
                    Send.setBackground(Color.pink);      
                    Send.setForeground(Color.blue);      
                    Send.setBounds(new       Rectangle(92,       400,       90,       37));      
                    Link.setBackground(Color.pink);      
                    Link.setForeground(Color.blue);                  
                    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);      
                    areaScrollPane.setAutoscrolls(true);      
                jTextSendInfo.setText( " ");      
                    jPanel1.add( "North ",areaScrollPane);      
                    jTextGetInfo.setEditable(false);      
                    jPanel1.add( "South ",Send);      
                    Send.setEnabled(false);      
                    jPanel1.add( "South ",Link);      
                    Send.addActionListener(this);      
    jTextSendInfo.addKeyListener(this);      
                    Link.addActionListener(new       ActionListener()      
                                            {public       void       actionPerformed(ActionEvent       e){      
                                            if(!jTextSendInfo.getText().equals( " ")){      
                                                            clientname=jTextSendInfo.getText();      
                                                            connect();                                                          


                                                            Link.setEnabled(false);      
                                                            Send.setEnabled(true);      
                                                            jTextSendInfo.setText( " ");      
                                            }      
                                            else       JOptionPane.showMessageDialog(null, "警告:用户名不能为空!! ", "消息对话框 ",JOptionPane.WARNING_MESSAGE);      
                                            }});      
                    this.setContentPane(jPanel1);      
            }      
            public       void       connect(){      
                    try{      
                            client=new       Socket(SERVERADDRESS,SERVERPORT);                                  
                                cout=new       PrintWriter(client.getOutputStream());                                              
                                        cin=new       BufferedReader(      
                                            new       InputStreamReader(client.getInputStream()));      
                                            cout.println( "欢迎[ "+clientname+ "]进入聊天室 ");      
                                            cout.flush();      
                                                Receiver       r=new       Receiver();      


                                            r.start();                      
            }catch(Exception       e){      
                                           
                        e.printStackTrace();      
            }      
    }      
    protected       void       processWindowEvent(WindowEvent       e){      
                        if(e.getID()==WindowEvent.WINDOW_CLOSING){      
                                        quit();      
                        }      
                        super.processWindowEvent(e);      
                    }      
                    public       static       void       main(String[]       args){      
                            ClientPoint       cp=new       ClientPoint();      
                            cp.show();      
                               
                    }      
       
    class       Receiver       extends       Thread{      
    public       void       run(){      
    String       msg=null;      
    JScrollBar       sb;      
    try{      
    msg=cin.readLine();      
    while(true){      
    jTextGetInfo.append(msg+ "\n ");      
    sb=areaScrollPane.getVerticalScrollBar();      
    sb.setValue(sb.getMaximum()+50);      
    jTextSendInfo.setEnabled(true);      
    msg=cin.readLine();      
    }      
    }catch(Exception       e){      
    Send.setEnabled(false);      
    }      
    }      
    }      
    void       quit(){      


    try{      
    cout.println( "[ "+clientname+ "]离开聊天室 ");      
    cout.flush();      
    cout.println( "exit ");      
    cout.flush();      
    System.out.print( "lianjieduankai ");      
    cin.close();      
    cout.close();      
    client.close();      
    }catch(Exception       e){}      
    finally{System.exit(0);      
    }      
    }      
       
    public       void       actionPerformed(ActionEvent       e){      
                                            if(!jTextSendInfo.getText().equals( " ")){      
                                            cout.println( "[ "+clientname+ "]: "+jTextSendInfo.getText());      
                                            cout.flush();                                              
                                            jTextSendInfo.setText( " ");      
                                        jTextSendInfo.setFocusable(true);                                              
                                                    }      
                                                    else       JOptionPane.showMessageDialog(null, "警告:消息不能为空!! ", "消息对话框 ",JOptionPane.WARNING_MESSAGE);      
                                                    }      
       
    public       void       keyPressed(KeyEvent       e){}      
    public       void       keyTyped(KeyEvent       e){}      
    public       void       keyReleased(KeyEvent       e)      
    {      
    if       (e.getKeyCode()==KeyEvent.VK_ENTER)      


    {      
    if(!jTextSendInfo.getText().equals( " "))      
    {      
    if(cout==null)      
    {      
    clientname=jTextSendInfo.getText();      
    connect();                                                          
    Link.setEnabled(false);      
    Send.setEnabled(true);      
    jTextSendInfo.setText( " ");      
    }      
    else      
    {      
    cout.println( "[ "+clientname+ "]: "+jTextSendInfo.getText());       //不明白
    cout.flush();                                              
    jTextSendInfo.setText( " ");      
    jTextSendInfo.setFocusable(true);          
    }      
    }      
    else      
    {      
    if       (cout==null)      
    {      
    JOptionPane.showMessageDialog(null, "警告:用户名不能为空!! ", "消息对话框 ",JOptionPane.WARNING_MESSAGE);      
    }      
    else       JOptionPane.showMessageDialog(null, "警告:消息不能为空!! ", "消息对话框 ",JOptionPane.WARNING_MESSAGE);      
    }      
       
       
       
    }      
    }      
       
    }      


[解决办法]
jTextSendInfo.getText()键盘敲入字符是什么就取什么。

热点排行