DOCX

Java CS mode --Client

By Dale Robinson,2014-10-16 23:38
11 views 0
Java CS mode --Client

    http://www.chinaunix.net 作者:ludejun98

import java.applet.*&#59;

    import java.awt.*&#59;

    import java.awt.event.*&#59;

    import java.io.*&#59;

    import java.net.*&#59;

//public String str&#59;

class javaclient extends Panel implements ActionListener

    {

TextField tf&#59;

    LudCanvas cv&#59;

    Button b&#59;

LudPanel(LudCanvas cv)

    {

    this.cv = cv&#59;

    tf = new TextField("ludejun",30)&#59; add(tf)&#59;

    b = new Button("退出")&#59; b.addActionListener(this)&#59;

    add(b)&#59;

    b = new Button("发送")&#59; b.addActionListener(this)&#59;

    add(b)&#59;

    }

    public str actionPerformed(ActionEvent ev) {

    String str = ev.getActionCommand()&#59; if( str.equals("退出") ) {

    //tf.setText("This is my first button")&#59;

    System.exit(0)&#59;

    }

    else

{

    str = tf.getText()&#59;

    tf.setText("开始检测发送......")&#59; return str&#59;

}

    }

    public static void main(String args[]) {

    String data&#59;

    Socket Client&#59;

    DataInputStream InputS&#59;

    DataInputStream KeyS&#59;

    PrintStream OutputS&#59;

Basic h = new Basic()&#59;

int i = 0&#59;

try

    {

    Client = new Socket(args[0],600)&#59; InputS = new

    DataInputStream(Client.getInputStream())&#59; OutputS = new PrintStream(Client.getOutputStream())&#59;

    KeyS = new DataInputStream(System.in)&#59; }

    catch(IOException e)

    {

    System.out.println("Cannot Connect with Server")&#59;

    return&#59;

    }

try

    {

    while (i<5)

{

    data = KeyS.readLine()&#59;

    OutputS.println(data)&#59;

    System.out.println(&quot;ECHO From Server:&quot;+ InputS.readLine())&

    #59;

    i++&#59;

    }

    }

    catch(IOException e)

    {

    System.out.println(&quot;IOException Happened&quot;)&#59;

    }

try

    {

    System.out.println(&quot;Now will end this program&quot;)&#59;

    Client.close()&#59;

    }

    catch(IOException e)

    {

    System.out.println(&quot;system cannot close socket&quot;)&#59;

    }

    }

    }

//@@@@@@@@@@@@@@@@@@@@@@@222

class LudCanvas extends Canvas

    {

    public void paint(Graphics g)

    {

    Rectangle r = getBounds()&#59;

    g.setColor(Color.red)&#59;

    int panelHeight = 15&#59;

    int b = 3&#59;

    g.drawRect(b , b , r.width-b-b , r.height-b-b-panelHeight)&#59;

    int t = 10&#59;

    g.drawRect(t , t , r.width-t-t , r.height-t-t-panelHeight)&#59;

g.drawString(&quot;The first applet of mine&quot;, 120, 30)&#59;

Image img = Toolkit.getDefaultToolkit().getImage(&quot;ie.gif&quot;)&

    #59;

g.drawImage(img, 50, 50, this)&#59;

}

}

    class LudPanel extends Panel implements ActionListener {

    TextField tf&#59;

    LudCanvas cv&#59;

    Button b&#59;

LudPanel(LudCanvas cv)

    {

    this.cv = cv&#59;

    tf = new TextField(&quot;ludejun&quot;,30)&#59; add(tf)&#59;

    b = new Button(&quot;退出&quot;)&#59; b.addActionListener(this)&#59;

    add(b)&#59;

    b = new Button(&quot;发送&quot;)&#59; b.addActionListener(this)&#59;

    add(b)&#59;

    }

    public void actionPerformed(ActionEvent ev) {

    String str = ev.getActionCommand()&#59; if( str.equals(&quot;退出&quot;) )

    {

    //tf.setText(&quot;This is my first button&quot;)&#59;

    System.exit(0)&#59; }

    else

    {

    tf.setText(&quot;开始检测发送......&quot;)&#59;

    }

    }

    }

     :em51: :em51: :em51:

Report this document

For any questions or suggestions please email
cust-service@docsford.com