免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1031 | 回复: 0
打印 上一主题 下一主题

Applet(二):最基本的Applet [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-03 16:30 |只看该作者 |倒序浏览

教你如何创建简单的Applet,应用Java1.0以前版本的AWT,不提倡应用,仅用于学习。
The basic applet [color="#000000"]标准Java library [color="#000000"]String[color="#000000"] 和 [color="#000000"]Vector[color="#000000"] 类就是按照他们的功能进行的分组. A certain class of library is the [color="#000000"]application framework [color="#000000"], whose goal is to help you build applications by providing a class or set of classes that produces the basic behavior that you need in every application of a particular type. Then, to customize the behavior to your own needs you inherit from the application class and override the methods of interest. The application framework’s default control mechanism will call your overridden methods at the appropriate time. An application framework is a good example of “separating the things that change from the things that stay the same,” since it attempts to localize all the unique parts of a program in the overridden methods. [color="#000000"]Applets 就是使用application framework创建. 你可以用从[color="#000000"]Applet继承[color="#000000"]重写相应的方法. 通常使用的关于Applet的方法如下: [color="#000000"]Method [color="#000000"]Operation [color="#000000"]init( ) [color="#000000"]Called when the applet is first created to perform first-time initialization of the applet [color="#000000"]start( ) [color="#000000"]Called every time the applet moves into sight on the Web browser to allow the applet to start up its normal operations (especially those that are shut off by [color="#000000"]stop( )[color="#000000"]). Also called after [color="#000000"]init( )[color="#000000"]. [color="#000000"]paint( ) [color="#000000"]Part of the base class [color="#000000"]Component[color="#000000"] (three levels of inheritance up). [color="#000000"][color="#000000"]Called as part of an [color="#000000"]update( )[color="#000000"] to perform special painting on the canvas of an applet. [color="#000000"]stop( ) [color="#000000"]Called every time the applet moves out of sight on the Web browser to allow the applet to shut off expensive operations. Also called right before [color="#000000"]destroy( )[color="#000000"]. [color="#000000"]destroy( ) [color="#000000"]Called when the applet is being unloaded from the page to perform final release of resources when the applet is no longer used [color="#000000"]paint( )[color="#000000"] :这个方法在构件(这里指的是)决定他自己需要更新的时候会自动调用.比如说:[color="#000000"] 它被放入后台,或者第一次在屏幕上刷新;一些其他的窗口放在了你的浏览器前面. applet调用[color="#000000"]update( )方法[color="#000000"](在基础类[color="#000000"]Component中定义的[color="#000000"]),[color="#000000"] [color="#000000"]用来恢复构件上所有东西, 复位的时候调用[color="#000000"]paint( )[color="#000000"]. 一般情况下不需要重写[color="#000000"]paint( ),[color="#000000"] 简单的方法是使用一个小的Applet,一开始就调用[color="#000000"]paint( )[color="#000000"]. [color="#000000"]当[color="#000000"]update( )[color="#000000"] 调用[color="#000000"]paint( )[color="#000000"] 传递一个引用给 [color="#000000"]Graphics[color="#000000"] 对象(表示那些部分是可以重绘的). 这个很重要他决定了你可以重绘的区域,而在这个区域之外将不能做动作. 对于applet, 这个表面指的是Applet内部. [color="#000000"]Graphics[color="#000000"] 同样提供了一系列的操作.包括drawing images, shapes, arcs, etc. 有些方法可以提供字符输出, 最长用的就是[color="#000000"]drawString( )[color="#000000"]. 指定一个[color="#000000"]String[color="#000000"],以及起始位置. 这个位置以象素形式给出, 不同的机器上看起来可能有些不同,至少他是可移植的. [color="#000000"]通过以上信息,我们创建一个简单的applet: [color="#990000"]//: Applet1.java
[color="#009900"]// Very simple applet
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class Applet1 [color="#0000ff"]extends Applet {
  [color="#0000ff"]public [color="#0000ff"]void paint(Graphics g) {
    g.drawString("First applet", 10, 10);
  }
} [color="#009900"]///:~ [color="#000000"]注意:Applet需要一个[color="#000000"]main( )函数[color="#000000"]. That’s all wired in to the application framework; you put any startup code in [color="#000000"]init( )[color="#000000"]. [color="#000000"]要运行这个程序,你需要把他放入一个网页. 把applet放入网页逆续要在HTML源文件中写入一些特殊的tag[color="#000000"] 来告诉网页我要加载运行哪一个applet. 下面就是 [color="#000000"]applet[color="#000000"] tag, 对于Applet1.java来说大概如下: [color="#990000"][color="#000000"] [color="#000000"]code 指出[color="#000000"].class 文件[color="#000000"]. [color="#000000"]width[color="#000000"] and [color="#000000"]height[color="#000000"] 指定了Applet的初始大小(象素). 还有一些其他的项目可以放进去比如:(1)可以找到[color="#000000"].class [color="#000000"]文件的路径([color="#000000"]codebase[color="#000000"])(2)布局信息([color="#000000"]align[color="#000000"]), 特殊的标识符,可以让其他的applet等访问([color="#000000"]name[color="#000000"])(4)[color="#000000"]applet 提供了applet可以提取的有用的信息. Parameters are in the form [color="#000000"][color="#000000"]可以参照这个写更多的tag. [color="#000000"]For simple applets all you need to do is place an applet tag in the above form inside your Web page and that will load and run the applet. 测试applets [color="#000000"]我们可以简单的测试一下上面的例子,而不需要使用浏览器打开包含有applet tag的html文件(Sun’s JDK 提供了一个工具叫:[color="#000000"]appletviewer[color="#000000"] 从html文件中抽取

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8682/showart_39002.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP