资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
小应用程序,独立应用程序的概念,所谓独立应用程序是指从一个Java类的main()方法开始运行的程序。在这种运行模式中,Java类的.class文件和Java虚拟机进程位于同一个台机器上,小应用程序的概念,JApplet以及它的父类Applet与其他Java类的最大区别在于,它们可以作为小应用程序,在浏览器中运行。当浏览器访问Web服务器中的一个嵌入了Applet的网页时,这个Applet的.class文件会从Web服务器端下载到浏览器端,浏览器启动一个Java虚拟机来运行Applet。,主要的Applet方法,代表了Applet生命周期的主要活动内容的方法: init (),start(),stop(),destory() 代表了Applet访问环境资源的方法 getDocumentBase(), getCodeBase() getImage(URL base, String target),getParameter(),Applet举例,public class MyApplet extends Applet implements Runnable String word; int fontSize; Thread changer; public void init() word=getParameter(word); fontSize=8; setSize(100,100); public void start() changer=new Thread(this); changer.start(); public void stop() changer.stop(); ,public void paint(Graphics g) g.setFont(new Font(newFont,Font.BOLD,fontSize); g.drawString(word,30,80); public void run() while(true) fontSize+=4; repaint(); /The word will be redrawed with new fontSize try Thread.sleep(1000); catch(Exception e) if(fontSize40) fontSize=8; /run() ,HTML中的Applet标记,以IE浏览器为例,IE浏览器支持JDK1.0版本中的Applet,只要在网页中加入一个标记就能嵌入一个Applet. 浏览器一遇到这个标记,就会下载相应的 Applet类文件,并启动Java虚拟机运行这个Applet。,hi.html ,读取HTML文件中的参数, 以下是Applet的init方法: init() String s1=getParameter(“none”); /s1=null String s2=getParameter(“word”); /s2=“hi” String s3=getParameter(“WORD”); /s3=“hi” System.out.println(s2=s3); /true String s4=getParameter(“longWord”); / s4=”hi all” ,Applet的运行,Applet通常运行于一个Web浏览器中 JDK附带有一个专为查看Applet而设计的工具appletviewer。它的运行命令为: Appletviewer -debug -J -encoding url |File 例如: appletviewer hi.html,将Applet加载到独立应用程序中,Applet是Panel的子类,可以把Applet放到Frame中,然后通过独立应用程序显式。 参见AppletShower.java,Frame,Applet,JApplet与Applet的区别,不能直接向JApplet中加入组件,而必须首先 获得它的内容面板,然后向内容面板中加入组件, 内容面板默认的布局管理器为BorderLayout,这一 点和JFrame一样: Container contentPane=getContentPane(); contentPane.setBackground(Color.WHITE); contentPane.add(contrlButton, BorderLayout.NORTH);,JApplet举例,public class MyJApplet extends JApplet implements Runnable String word; int fontSize; Thread changer; public void init() word=getParameter(word); fontSize=8; setSize(100,100); public void start() changer=new Thread(this); changer.start(); public void stop() changer.stop(); ,public void paint(Graphics g) super.paint(g); g.setFont(new Font(newFont,Font.BOLD,fontSize); g.drawString(word,30,80); public void run() while(true) fontSize+=4; repaint(); /The word will be redrawed with new fontSize try Thread.sleep(1000); catch(Exception e) if(fontSize40) fontSize=8; /run() ,在HTML中加入JApplet,IE浏览器无法运行Java2中的JApplet,在这种情况下,可以把JApplet作为一种插件来运行。在IE浏览器中,插件的标记为,在Netscape浏览器中,插件的标记为。当浏览器遇到插件标记,就会启动相应的插件运行程序,通过该程序来运行特定的插件。 JApplet作为一种插件时,它的插件运行程序为JRE程序(Java Runtime Enviroment,Java运行时环境)。该程序的下载网址为:,在HTML中加入JApplet,hi_1.html ,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号