java通信

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

jxdh-flying发表时间:2004-3-1510:47:35第1楼importjava.io.*;importjava.net.*;publicclassClient{publicstaticvoidmain(Stringargs[]){Strings=null;Socketmysocket;DataInputStreamin=null;DataOutputStreamout=null;try{mysocket=newSocket(localhost,5000);in=newDataInputStream(mysocket.getInputStream());out=newDataOutputStream(mysocket.getOutputStream());out.writeUTF(你好啊);while(true){s=in.readUTF();if(s==null)break;}//从服务器线路中读取信息!mysocket.close();}catch(IOExceptione){System.out.println(发生意外阿,现在确实是无法连接,请稍候.....);}System.out.println(s);}}=---------------------------------------------------------------------------------------------------------------------------------------//以上实验:JAVA网络程序设计成功实现本机通讯!importjava.io.*;importjava.net.*;publicclassServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;//Socketyou=null;//服务器端的socket对象!Strings=null;DataInputStreamin=null;DataOutputStreamout=null;try{server=newServerSocket(5000);}catch(IOExceptione1){System.out.println(ERRO:+e1);}try{you=server.accept();in=newDataInputStream(you.getInputStream());out=newDataOutputStream(you.getOutputStream());while(true){s=in.readUTF();if(s!=null)break;}out.writeUTF(你好!服务器受到了你发的信息!);//通过out向线路写入信息!client接受信息!you.close();}catch(IOExceptione){System.out.println(ERRO:+e);}}}//以上实验:JAVA网络程序设计成功实现本机通讯!==========================================================================importjava.applet.*;importjava.awt.*;publicclassWuqiongextendsApplet{staticImageimg;Canvascanvas;staticintwidth,height;publicvoidinit(){setLayout(newGirlLayout(3,1);addf(newButton(祝愿));add(newButton(进步));canvas=newMycanvas();add(canvas);width=getSize().width;height=getSize().height;}publicvoidstart(){img=getImage(getCodeBase(),20041.jpg);}classMycanvasextendsCancas{publicvoidpaint(Graphicsg){g.drawImage(Wuqiong.img,0,0,Wuqiong.width,(Wuqiong.height)/3,this);}}jxdh-flying发表时间:2004-3-1510:47:51第2楼importjava.applet.*;importjava.awt.*;//importjava.awt.TextField;//importjava.awt.TextArea;importjava.awt.event.*;publicclasswenbenquceshiextendsAppletimplementsTextListener{//处理文本框事件的接口!TextAreatext1,text2;TextFieldtext3;publicvoidinit(){text1=newTextArea(,10,10);text2=newTextArea(,10,10);text3=newTextField(10);add(text1);add(text2);add(text3);text1.addTextListener(this);//将珠主类的实例作为监听器!}//实例必须实现接口方法!publicvoidtextValueChanged(TextEvente)//{if(e.getSource()==text1){text2.setText(text1.getText());text3.setText(我开始要改变内容);}else{}}}jxdh-flying发表时间:2004-3-1510:48:06第3楼importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclasswenbenkuangextendsAppletimplementsActionListener{TextFieldtext1,text3;publicvoidinit(){setBackground(Color.green);setSize(300,300);text1=newTextField(10);text3=newTextField(10);add(text1);add(text3);text1.addActionListener(this);}publicvoidactionPerformed(ActionEvente)//当前类对象作为监视器并且实现接口方法,通过该方法来处理文本事件的方法!{//文本框这个事件源可以发生ActionEvent事件!//if((e.getSource()==text1)&&(text1.getText().equals(boy))){text3.setText(男孩);}elseif((e.getSource()==text1)&&(text1.getText().equals(girl))){text3.setText(女孩);}elseif((e.getSource()==text1)&&(text1.getText().equals(sun))){text3.setText(太阳);}else{text3.setText(没有这个单词阿!);}}}jxdh-flying发表时间:2004-3-1510:48:22第4楼importjava.applet.*;importjava.awt.*;publicclassStringindexextendsApplet{Stringtom=Iamagoodbook!;intx,y,z;booleanm;publicvoidinit(){tom=newString(Iamagoodbook!);}publicvoidstart(){m=tom.startsWith(I);}publicvoidpaint(Graphicsg){x=tom.indexOf(a);y=tom.indexOf(good,2);z=tom.indexOf(a,7);g.setColor(Color.red);g.drawString(+x+:+y++z++m,20,30);}}jxdh-flying发表时间:2004-3-1510:48:45第5楼importjava.applet.*;importjava.awt.*;importjava.awt.event.*;importjava.net.*;publicclassnetextendsAppletimplementsActionListener{//理解监听器与接口的作用和关系!Choicechoice;Buttonbutton;URLurl;TextFieldtext;Strings1,s2,s3,s4;publicvoidinit(){setLayout(newGridLayout(2,1));text=newTextField(10);button=newButton(确定);choice=newChoice();s1====(s1);choice.add(s2);choice.add(s3);choice.add(s4);add(choice);add(button);add(text);//注意要将组件放入容器中!add(newLabel());button.addActionListener(this);//当前类对象}publicvoidactionPerformed(ActionEvente)//当时间源发生事件时,监视器自动调用对应的接口方法对时间进行处理!{if(e.getSource()==button){try{url=newURL(choice.getSelectedItem().trim());text.setText(请等待......);}catch(MalformedURLExceptiong){System.out.println(不正确的URL.....:+url);getAppletContext().showDocument(url);}}else{}}}jxdh-flying发表时间:2004-3-1510:49:04第6楼importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassmouseevnetextendsAppletimplementsMouseListener,ItemListener{TextFieldtext,text2;intx;Choicechoice;Strings1,s2,s3,s4;publicvoidinit(){setSize(400,400);setBackground(Color.red);x=5;s1=中国;s2=日本;s3=泰国;s4=新加坡;text=newTextField(40);text2=newTextField(40);choice=newChoice();choice.add(s1);choice.add(s2);choice.add(s3);choice.add(s4);add(choice);add(text);add(text2);addMouseListener(this);choice.addItemListener(this);}publicvoidpaint(Graphicsg){x=x+3;g.drawOval(10,10,x,x);}publicvoidmousePressed(MouseEvente){text.se

1 / 58
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功