第十二章JavaApplet基础
例子1
importjava.applet.;
importjava.awt.;
publicclassExample12_1extendsApplet
{Buttonbutton1,button2;
intsum;
publicvoidinit()
{setBackground(Color.gray);
button1=newButton("yes");
button2=newButton("No");
add(button1);
add(button2);
}
publicvoidstart()
{sum=0;
for(inti=1;i<=100;i++)
{sum=sum+i;
}
}
publicvoidstop(){}
publicvoidpaint(Graphicsg)
{g.setColor(Color.blue);
g.drawString("程序设计方法",20,60);
g.setColor(Color.red);
g.drawString("sum="+sum,20,100);
}
}
例子2
importjava.awt.;
importjava.applet.;
publicclassExample12_2extendsApplet
{intx=0,y=0;
publicvoidinit()
{Strings1=getParameter("girl");//从html得到"girl"的值。
Strings2=getParameter("boy");//从html得到"boy"的值。
x=Integer.parseInt(s1);
y=Integer.parseInt(s2);
}
publicvoidpaint(Graphicsg)
{g.drawString("x="+x+","+"y="+y,90,120);
}
}
Boy.html
例子3
importjava.applet.;
importjava.awt.;
importjava.awt.event.;
importjava.net.;
publicclassBoyextendsAppletimplementsActionListener
{Buttonbutton;
URLurl;
TextFieldtext;
publicvoidinit()
{text=newTextField(18);
button=newButton(“确定”);
add(newLabel(“输入网址:”));
add(text);add(button);
button.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==button)
{try{url=newURL(text.getText().trim());
getAppletContext().showDocument(url);
}
catch(MalformedURLExceptiong)
{text.setText(“不正确的URL:"+url);
}
}
}
}
例子4
importjava.applet.;
importjava.awt.;
importjava.net.;
importjava.io.;
publicclassReadFileextendsApplet
{TextAreatext;
URLurl;
publicvoidinit()
{text=newTextArea(12,40);
add(text);
}
publicvoidstart()
{try{url=newURL(getCodeBase(),"hello.txt");
InputStreamin=url.openStream();//url返回输入流
intn=-1;
byteb[]=newbyte[100];
while((n=in.read(b))!=-1)
{Stringstr=newString(b,0,n);
text.append(str);
}
}
catch(Exceptionee){}
}
}
例子5
importjava.applet.;
importjava.awt.;
importjava.awt.event.;
publicclassCircleAndRectextends
AppletimplementsRunnable
{Threadleft,right;
Graphicsmypen;
intx,y;
publicvoidinit()
{left=newThread(this);
right=newThread(this);
x=10;y=10;
mypen=getGraphics();
}
publicvoidstart()
{try{left.start();
right.start();
}
catch(Exceptione){}
}
publicvoidrun()
{while(true)
{if(Thread.currentThread()==left)
{x=x+1;
if(x>240)x=10;
mypen.setColor(Color.blue);
mypen.clearRect(10,10,300,40);
mypen.drawRect(10+x,10,40,40);
try{left.sleep(60);
}
catch(InterruptedExceptione){}
}
elseif(Thread.currentThread()==right)
{y=y+1;
if(y>240)y=10;mypen.setColor(Color.red);
mypen.clearRect(10,90,300,40);
mypen.drawOval(10+y,90,40,40);
try{right.sleep(60);
}
catch(InterruptedExceptione){}
}
}
}
}
例子6
(1)客户端
importjava.net.;
importjava.io.;
importjava.awt.;
importjava.applet.;
publicclassClientextendsAppletimplementsRunnable
{TextFieldtext;
Socketsocket=null;
ObjectInputStreamin=null;
ObjectOutputStreamout=null;
Threadthread;
publicvoidinit()
{thread=newThread(this);
}
publicvoidstart()
{try{thread.start();
socket=newSocket(this.getCodeBase().getHost(),4331);
in=newObjectInputStream(socket.getInputStream());
out=newObjectOutputStream(socket.getOutputStream());
}
catch(Exceptione){}
}
publicvoidrun()
{Strings=null;
while(true)
{try{text=(TextField)in.readObject();
add(text);
validate();
return;
}
catch(Exceptione){}
}
}
}
(2)服务器端
importjava.io.;
importjava.net.;
importjava.util.;
importjava.awt.;
publicclassServer
{publicstaticvoidmain(Stringargs[])
{ServerSocketserver=null;
Server_threadthread;
Socketyou=null;
while(true)
{try{server=newServerSocket(4331);
}
catch(IOExceptione1)
{System.out.println("正在监听");
}
try{you=server.accept();
}
catch(IOExceptione){}
if(you!=null)
{newServer_thread(you).start();
}
}
}
}
classServer_threadextendsThread
{Socketsocket;
ObjectOutputStreamout=null;
ObjectInputStreamin=null;
Strings=null;
Server_thread(Sockett)
{socket=t;
try{out=newObjectOutputStream(socket.getOutputStream());
in=newObjectInputStream(socket.getInputStream());
}
catch(IOExceptione){}
}
publicvoidrun()
{TextFieldtext=newTextField("你好,我是服务器",20);
try{out.writeObject(text);
}
catch(IOExceptione){}
}
}
|
|