Java Applet类型代码

Java Applet类型代码

ID:37915129

大小:48.00 KB

页数:5页

时间:2019-06-02

Java Applet类型代码_第1页
Java Applet类型代码_第2页
Java Applet类型代码_第3页
Java Applet类型代码_第4页
Java Applet类型代码_第5页
资源描述:

《Java Applet类型代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、例3-1HelloWorld.javaimportjava.awt.*;importjava.applet.*;publicclassHelloWorldextendsApplet//继承Applet类,这是AppletJava程序的特点{publicvoidpaint(Graphicsg){g.drawString("HelloWorld!",5,35);}}例3-2RepaintStudy.javaimportjava.awt.*;importjava.awt.event.*;classMyCanvasextendsCanvas{intn

2、=-1;MyCanvas(){setSize(150,120);setBackground(Color.pink);}publicvoidpaint(Graphicsg){g.setColor(Color.red);g.drawString("部分清除",10,12);g.drawString("学习repaint方法",10,80);}publicvoidsetN(intn){this.n=n;}publicvoidupdate(Graphicsg){intwidth=0,height=0;width=getSize().width;hei

3、ght=getSize().height;if(n==0){g.clearRect(0,0,width,height);//paint(g);//如果取消该注释,update的功能就与父类相同。}elseif(n==1){g.clearRect(2,2,width,40);}}}publicclassRepaintStudyextendsjava.applet.AppletimplementsActionListener{Buttonb1,b2;MyCanvascanvas;publicvoidinit(){canvas=newMyCanva

4、s();b1=newButton("全部清除");b1.addActionListener(this);b2=newButton("部分清除");b2.addActionListener(this);add(b1);add(b2);add(canvas);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==b1){canvas.setN(0);canvas.repaint();}if(e.getSource()==b2){canvas.setN(1);canvas.repain

5、t();}}}例题3-1在HTML中传递Applet使用的参数,通过传递过来的参数设置Applet字体的显示颜色。importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Color;publicclassFancyHelloWorldextendsjava.applet.Applet{FonttempFont=newFont("Helvetica",Font.PLAIN,20);ColortempColor;StringtempString;publicvoidinit(){//得

6、到Web页中COLOR参数的值tempString=getParameter("COLOR");if(tempString.equals("WHITE"))//如果字符串等于"WHITE"tempColor=newColor(255,255,255);//创建颜色对象elseif(tempString.equals("BLACK"))tempColor=newColor(0,0,0);elseif(tempString.equals("GRAY"))tempColor=newColor(128,128,128);elseif(tempStri

7、ng.equals("RED"))tempColor=newColor(255,0,0);elseif(tempString.equals("CREEN"))tempColor=newColor(0,255,0);elseif(tempString.equals("BLUE"))tempColor=newColor(0,0,255);elseif(tempString.equals("YELLOW"))tempColor=newColor(255,255,0);elseif(tempString.equals("MAGENTA"))tempC

8、olor=newColor(255,0,255);elseif(tempString.equals("CYAN"))tempColor=newColor(0,255

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。