教你构建第一个JavaApplet

介绍

Note: 在你开始本教程之前,你必须下载downloaded 并安装installed Java SE Development Kit。

Java applets像Java应用程序一样,它们的建立都是遵循相同的三个步骤—编写,编译及运行。不同 的是,它们是在一部分网页上运行,而不是在你的桌面上运行。

本文的主要目的是创建一个简单的Java applet. 为了达到这一点要遵循以下三个基本步骤:

1. 在Java中编写一个简单的applet

2. 编译Java源代码

3. 创建一个涉及到applet的HTML页

4. 在浏览器中打开HTML页

编写Java源代码

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

我使用记事本来创建我的Java源代码文件。打开你选择的ediTor并输入这样的代码:

//Reference the required Java libraries  import java.applet.Applet;   import java.awt.*;  //The applet code  public class FirstApplet extends Applet {  public void paint(Graphics g) {  //Draw a rectangle width=250, height=100  g.drawRect(0,0,250,100);  //Set the color to blue  g.setColor(Color.blue);  //Write the message to the web page  g.drawString("Look at me, I'm a Java Applet!",10,50);  }   }

不要过多的担心代码的意义。这是你的第一个applet,重要的是看一看它是如何创建,编译和运行的 。

保存文件

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

保存你的程序文件为“FirstApplet.java”。确保你使用的文件名是正确的。如果你看到如下显示的 代码:

public class FirstApplet extends Applet {

这是一个指令来调用applet类 “FirstApplet”。文件名要与这个类的名字相匹配并有一个“.java” 的扩展。如果你的文件没有另存为”FirstApplet.java”,Java compiler就会抱怨而且不会编译你的 applet。

打开一个终端窗口

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

要打开一个终端窗口,需按下“Windows key”和字母“R”。

你将看到“Run Dialog”。输入“cmd”,点击“OK”。

终端窗口就显示出来。它它看成是文字版的Windows Explorer;它会让你砸电脑上浏览不同的目录,看 一看它们包含的文件,运行你想要的程序。在窗口中输入命令就可以完成这个了。

Java Compiler

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

我们需要终端窗口来访问叫做“javac”的Java compiler。这是一个要在FirstApplet.java文件中读 取代码的程序,把它翻译成你的电脑可以知道的语言。这个过程就做编译。就像Java应用程序一样,Java applet也必须编译。

为了在终端窗口运行javac,你需要告诉电脑它在哪里。在我的机器上,它在目录“C:/Program Files/Java/jdk1.6.0_06/bin”中。如果你没有这样的目录,在Windows资源管理器中搜索文件“javac” ,找到它的位置。

一但你找到它的位置,输入以下命令道终端窗口:

set path= *the direcTory where javac lives*

E.g.,

set path=C:/Program Files/Java/jdk1.6.0_06/bin

按回车。终端窗口不会做任何华而不实的东西,它只是返回到命令提示符。但是,compiler的路径现 在已经被设置。

改变目录

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

看一看在哪里保存了FirstApplet.java文件。我的文件保存地点是“C:/Documents and Settings/Paul/My Documents/Java/Applets”。

在终端窗口改变目录,要输入以下命令:

cd *direcTory where FirstApplet.java file is saved*

E.g.,

cd C:/Documents and Settings/Paul/My Documents/Java/Applets

通过看到左边的光标你可以分辨你在右边的目录中。我们现在准备编译applet。输入命令:

javac FirstApplet.java

在点击回车之后,compiler将看到包含在FirstApplet.java文件中的代码,并试着去编译它。如果它 不能,就会显示一系列的错位帮助你修复代码。

如果在没有任何信息提示下你返回到命令提示符处,说明你的applet编译成功了。如果不是这样,返 回在坚查你写的代码。确保它与example code相匹配并重新保存文件。一直这样做直到在没有任何错误提 示下运行javac。

Tip: 一但applets编译成功,你会在同样的目录中看到一个新的文件。它叫做“FirstApplet.class” 。这是你的applet已编译版本。

创建HTML文件

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

值得注意的是到目前为止你已经确切的遵循相同的步骤,如果你在创建一个Java应用程序。Applet被 创建并保存在一个文本文件中,通过javac compiler已经进行编译。

Java Applets不同于Java 应用程序,当它们运行的时候。现在需要的是涉及FirstApplet.class文件 的网页。记住,类文件是你的applet已编译的版本;这是你的电脑可以知道并执行的文件。

打开记事本,输入以下HTML代码:

    My First Java Applet      Here's my first Java Applet:       </code></p> <p>在相同的目录中文件另存为“MyWebpage.html”作为你的Java applet 文件。</p> <p>这个是在网页中最重要的一行:</p> </p> <p>当网页被显示,它会告诉浏览器打开你的Java applet并运行它。</p> <p>打开HTML页</p> </p> <p>Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.</p> <p>最后一步也是最好的一个;我们可以看到Java applet开始运行了。使用Windows Explorer来导航在 HTML页中储存的目录。例如,我的网页保存在“C:/Documents and Settings/Paul/My Documents/Java/Applets”和我的另一个Java applet文件。</p> <p>双击MyWebpage.html文件。你的默认浏览器将打开,Java applet将会运行。</p> <p>恭喜你!你已经创建了你的第一个Java applet!</p> <p>快速小总结</p> <p>花一点时间来总结一下创建Java applet的步骤。它们在你创建的每一个applet都是相同的:</p> <p>1. 在文本文件中编写Java 代码</p> <p>2. 保存文件</p> <p>3. 编译代码</p> <p>4. 修复错误</p> <p>5. 在HTML也中参考applet</p> <p>6. 过浏览网页运行applet</p> <p> 接受自己的失败面,是一种成熟,更是一种睿智;</p> </div> <center> <a href="https://www.note234.com/336086.html" rel="bookmark" title="教你构建第一个JavaApplet"><img src="https://www.note234.com/wp-content/themes/generatepress/images/66.jpg" alt="教你构建第一个JavaApplet" /></a> </center> <footer class="entry-meta" aria-label="条目 meta"> <span class="cat-links"><span class="gp-icon icon-categories"><svg viewBox="0 0 512 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"><path d="M0 112c0-26.51 21.49-48 48-48h110.014a48 48 0 0143.592 27.907l12.349 26.791A16 16 0 00228.486 128H464c26.51 0 48 21.49 48 48v224c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V112z" /></svg></span><span class="screen-reader-text">分类 </span><a href="https://www.note234.com/category/bckf" rel="category tag">编程开发</a></span> <span class="tags-links"><span class="gp-icon icon-tags"><svg viewBox="0 0 512 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"><path d="M20 39.5c-8.836 0-16 7.163-16 16v176c0 4.243 1.686 8.313 4.687 11.314l224 224c6.248 6.248 16.378 6.248 22.626 0l176-176c6.244-6.244 6.25-16.364.013-22.615l-223.5-224A15.999 15.999 0 00196.5 39.5H20zm56 96c0-13.255 10.745-24 24-24s24 10.745 24 24-10.745 24-24 24-24-10.745-24-24z"/><path d="M259.515 43.015c4.686-4.687 12.284-4.687 16.97 0l228 228c4.686 4.686 4.686 12.284 0 16.97l-180 180c-4.686 4.687-12.284 4.687-16.97 0-4.686-4.686-4.686-12.284 0-16.97L479.029 279.5 259.515 59.985c-4.686-4.686-4.686-12.284 0-16.97z" /></svg></span><span class="screen-reader-text">标签 </span><a href="https://www.note234.com/tag/java" rel="tag">java</a>、 <a href="https://www.note234.com/tag/%e6%ba%90%e4%bb%a3%e7%a0%81" rel="tag">源代码</a>、 <a href="https://www.note234.com/tag/%e7%bc%96%e5%86%99" rel="tag">编写</a></span> <nav id="nav-below" class="post-navigation" aria-label="文章"> <div class="nav-previous"><span class="gp-icon icon-arrow-left"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 138.212c0 2.265-1.133 4.813-2.832 6.512L64.276 256.001l111.317 111.277c1.7 1.7 2.832 4.247 2.832 6.513 0 2.265-1.133 4.813-2.832 6.512L161.43 394.46c-1.7 1.7-4.249 2.832-6.514 2.832-2.266 0-4.816-1.133-6.515-2.832L16.407 262.514c-1.699-1.7-2.832-4.248-2.832-6.513 0-2.265 1.133-4.813 2.832-6.512l131.994-131.947c1.7-1.699 4.249-2.831 6.515-2.831 2.265 0 4.815 1.132 6.514 2.831l14.163 14.157c1.7 1.7 2.832 3.965 2.832 6.513z" fill-rule="nonzero" /></svg></span><span class="prev"><a href="https://www.note234.com/336085.html" rel="prev">Java终止函数详解</a></span></div><div class="nav-next"><span class="gp-icon icon-arrow-right"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 256.001c0 2.266-1.133 4.815-2.832 6.515L43.599 394.509c-1.7 1.7-4.248 2.833-6.514 2.833s-4.816-1.133-6.515-2.833l-14.163-14.162c-1.699-1.7-2.832-3.966-2.832-6.515 0-2.266 1.133-4.815 2.832-6.515l111.317-111.316L16.407 144.685c-1.699-1.7-2.832-4.249-2.832-6.515s1.133-4.815 2.832-6.515l14.163-14.162c1.7-1.7 4.249-2.833 6.515-2.833s4.815 1.133 6.514 2.833l131.994 131.993c1.7 1.7 2.832 4.249 2.832 6.515z" fill-rule="nonzero" /></svg></span><span class="next"><a href="https://www.note234.com/336087.html" rel="next">利用PAPI接口监测Java程序的硬件执行特征</a></span></div> </nav> </footer> </div> </article> <div class="inside-article"> <h3>相关文章:</h3> <ul><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/439623.html">跨境电商中SKU编写的技巧是什么?如何确定SKU的数量?</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/438754.html">Shopee的标题规则有哪些?在编写标题时有哪些小技巧?</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/436950.html">在亚马逊上,如何编写SKU?有哪些特性需要注意?</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/436741.html">如何编写亚马逊的关键词?在设置关键词时需要注意哪些问题?</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/436244.html">ebay标题规范:标题怎么写才能优化搜索排名?</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433636.html">时间戳转化为今天、昨天、明天(字符串格式)</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433573.html">母牛的故事</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433568.html">(hdu step 3.1.1)超级楼梯(简单递推:从第1级到第m级有多少种走法</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433563.html">分享一个java Map实现的cache manager,定时清除缓存起来的值</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433536.html">玩github的一些相关资料的整理</a></div> </div></li></ul> <h4>你感兴趣的文章:</h4> <ul> <li><a href="https://www.note234.com/311448.html">揭秘7种黑白转换方法 看看哪种更适合你</a></li> <li><a href="https://www.note234.com/437931.html">Lazada自发货是否需要转运?一般运费是多少?</a></li> <li><a href="https://www.note234.com/3572.html">单片机编程能不能自学?我是如何自学单片机编程语言的</a></li> <li><a href="https://www.note234.com/49096.html">汽车仪表盘的转速表怎样看?功能有哪些?</a></li> <li><a href="https://www.note234.com/132729.html">刚上大一,想学嵌入式这个专业!解决办法</a></li> <li><a href="https://www.note234.com/45091.html">详解人工智能机器人的工作原理</a></li> </ul> <h4>标签云:</h4> <div class="customtag"> <a href='https://www.note234.com/wangzhan/tag/%e4%ba%9a%e6%b4%b2%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1%e5%9c%a8%e7%ba%bf/'>亚洲高清电影在线</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%8d%e8%b4%b9%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1/'>免费高清电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%ab%e6%88%92%e5%bd%b1%e9%99%a2%e5%a4%9c%e9%97%b4/'>八戒影院夜间</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%ab%e6%88%92%e7%94%b5%e5%bd%b1%e6%9c%80%e6%96%b0%e5%a4%a7%e7%89%87/'>八戒电影最新大片</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%87%ba%e8%bd%a8%e5%9c%a8%e7%ba%bf%e7%94%b5%e5%bd%b1/'>出轨在线电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%8d%88%e5%a4%9c%e7%94%b5%e5%bd%b1%e9%99%a2/'>午夜电影院</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e5%bd%b1%e9%99%a2a1166/'>在线影院a1166</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e7%94%b5%e5%bd%b1%e9%99%a2/'>在线电影院</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e8%a7%82%e7%9c%8b%e7%be%8e%e5%89%a7%e4%b8%8b%e8%bd%bd/'>在线观看美剧下载</a>, <a href='https://www.note234.com/wangzhan/tag/%e6%97%a5%e6%9c%ac%e7%88%b1%e6%83%85%e7%94%b5%e5%bd%b1/'>日本爱情电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e6%97%a5%e9%9f%a9%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1%e5%9c%a8%e7%ba%bf/'>日韩高清电影在线</a>, <a href='https://www.note234.com/wangzhan/tag/%e7%94%b5%e5%bd%b1%e5%a4%a9%e5%a0%82%e7%bd%91/'>电影天堂网</a>, <a href='https://www.note234.com/wangzhan/tag/%e7%9b%b4%e6%92%ad%e7%9b%92%e5%ad%90app/'>直播盒子app</a>, <a href='https://www.note234.com/wangzhan/tag/%e8%81%9a%e5%90%88%e7%9b%b4%e6%92%ad/'>聚合直播</a>, <a href='https://www.note234.com/wangzhan/tag/%e9%ab%98%e6%b8%85%e7%be%8e%e5%89%a7/'>高清美剧</a>, <a href='https://www.note234.com/wangzhan/tag/%e9%ab%98%e6%b8%85%e7%be%8e%e5%89%a7%e5%9c%a8%e7%ba%bf%e8%a7%82%e7%9c%8b/'>高清美剧在线观看</a> <a href='https://www.note234.com/wangzhan/tag/ehviewer-e%e7%ab%99/'>EhViewer-E站</a>, <a href='https://www.note234.com/wangzhan/tag/e%e7%ab%99/'>E站</a>, <a href='https://www.note234.com/wangzhan/tag/e%e7%ab%99%e7%bb%bf%e8%89%b2%e7%89%88/' >E站绿色版</a>, <a href='https://www.note234.com/wangzhan/tag/qqmulu-com/'>qqmulu.com</a>, <a href='https://www.note234.com/wangzhan/tag/qq%e7%9b%ae%e5%bd%95%e7%bd%91/'>qq目录网</a>, <a href='https://www.note234.com/wangzhan/tag/qq%e7%bd%91%e7%ab%99%e7%9b%ae%e5%bd%95/'>qq网站目录</a>, </div></div> </main> </div> <div class="widget-area sidebar is-right-sidebar" id="right-sidebar"> <div class="inside-right-sidebar"> <aside id="block-11" class="widget inner-padding widget_block"><div class="_2ho2ho6w023"></div> <script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6953233", container: "_2ho2ho6w023", async: true }); </script> <!-- 多条广告如下脚本只需引入一次 --> <script type="text/javascript" src="//cpro.baidustatic.com/cpro/ui/cm.js" async="async" defer="defer" > </script></aside> </div> <div id="box_float"> <div id="float" class="div1"> <aside id="block-12" class="widget inner-padding widget_block"> <div class="_x3rdkhcqm5n"></div> <script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6958514", container: "_x3rdkhcqm5n", async: true }); </script> </aside></div> </div> </div> </div> </div> </div> <div class="site-footer"> <footer class="site-info" aria-label="站点" itemtype="https://schema.org/WPFooter" itemscope> <div class="inside-site-info grid-container"> <div class="copyright-bar"> <p></p> <p>Copyright © 2023 <a href="https://www.note234.com/" target="_blank">摘录笔记网</a></p> <a href="https://www.note234.com/tags" target="_blank">tags</a>,<a href="https://www.note234.com/wp-sitemap.xml" target="_blank">xml地图</a>,<a href="https://www.note234.com/sitemap.html" target="_blank">网站地图</a>,<a href="https://www.note234.com/lxwm" target="_blank">联系我们</a>,<a href="https://www.yiwen55.com/" target="_blank">易海博客</a>,<a href="https://www.note234.com/wangzhan/" target="_blank">免费网站</a>,<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备20014300号-1</a> </div> </div> </footer> </div> <a title="回到顶部" aria-label="回到顶部" rel="nofollow" href="#" class="generate-back-to-top" data-scroll-speed="400" data-start-scroll="300"> <span class="gp-icon icon-arrow-up"><svg viewBox="0 0 330 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M305.863 314.916c0 2.266-1.133 4.815-2.832 6.514l-14.157 14.163c-1.699 1.7-3.964 2.832-6.513 2.832-2.265 0-4.813-1.133-6.512-2.832L164.572 224.276 53.295 335.593c-1.699 1.7-4.247 2.832-6.512 2.832-2.265 0-4.814-1.133-6.513-2.832L26.113 321.43c-1.699-1.7-2.831-4.248-2.831-6.514s1.132-4.816 2.831-6.515L158.06 176.408c1.699-1.7 4.247-2.833 6.512-2.833 2.265 0 4.814 1.133 6.513 2.833L303.03 308.4c1.7 1.7 2.832 4.249 2.832 6.515z" fill-rule="nonzero" /></svg></span> </a><script id="generate-a11y">!function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}();</script><!--[if lte IE 11]> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/classList.min.js" id="generate-classlist-js"></script> <![endif]--> <script id="generate-menu-js-extra"> var generatepressMenu = {"toggleOpenedSubMenus":"1","openSubMenuLabel":"\u6253\u5f00\u5b50\u83dc\u5355","closeSubMenuLabel":"\u5173\u95ed\u5b50\u83dc\u5355"}; </script> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/menu.min.js" id="generate-menu-js"></script> <script id="generate-back-to-top-js-extra"> var generatepressBackToTop = {"smooth":"1"}; </script> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/back-to-top.min.js" id="generate-back-to-top-js"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?95d0945b5c7afc67da1ee2166f8ca4b1"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html> <!-- Cached by ezCache --> <!-- Cache created in 1.40s -->