java applet_构建您的第一个Java Applet

java applet

 Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit.

在开始本教程之前,您必须已经下载并安装了Java SE Development Kit

Java applets are like Java applications, their creation follows the same three-step process of write, compile and run. The difference is, instead of running on your desktop, they run as part of a web page.

Java小程序就像Java应用程序一样,它们的创建遵循相同的三步编写,编译和运行过程。 区别在于它们不是在桌面上运行,而是作为网页的一部分运行。

The goal of this tutorial is to create a simple Java applet. This can be achieved by following these basic steps:

本教程的目的是创建一个简单的Java applet。 可以通过执行以下基本步骤来实现:

  1. Write a simple applet in Java

    用Java编写一个简单的applet
  2. Compile the Java source code

    编译Java源代码
  3. Create an HTML page that references the applet

    创建引用小程序HTML页面
  4. Open the HTML page in a browser

    在浏览器中打开HTML页面

编写Java源代码 ( Write the Java Source Code )

Writing the Source Code

This example uses Notepad to create the Java source code file. Open up your chosen editor, and type in this code:

本示例使用记事本创建Java源代码文件。 打开您选择的编辑器,然后输入以下代码:

Don’t worry too much about what the code means. For your first applet, it’s more important to see how it’s created, compiled and run.

不必太担心代码的含义。 对于您的第一个applet,了解它的创建, 编译和运行方式更为重要。

保存文件 ( Save the File )

Save the File

Save your program file as "FirstApplet.java". Make sure the filename you use is correct. If you look at the code you will see the statement:

将程序文件另存为“ FirstApplet.java”。 确保您使用的文件名正确。 如果您查看代码,将看到以下语句:

It’s an instruction to call the applet class "FirstApplet". The filename must match this class name, and have an extension of ".java". If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.

这是调用applet类“ FirstApplet”的指令。 文件名必须与该类名匹配,并且扩展名为“ .java”。 如果您的文件未另存为“ FirstApplet.java”,则Java编译器将抱怨并且不编译您的applet。

打开终端窗口 ( Open a Terminal Window )

Run Dialog Box

To open a terminal window, press the “"Windows key" and the letter "R".

要打开终端窗口,请按“ Windows键”和字母“ R”。

You will now see the "Run Dialog". Type "cmd", and press "OK".

现在,您将看到“运行对话框”。 键入“ cmd”,然后按“确定”。

A terminal window will appear. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files that they contain, and run any programs that you want to. This is all done by typing commands into the window.

将出现一个终端窗口。 将其视为Windows资源管理器的文本版本; 它可以让您导航到计算机上的其他目录,查看其中包含的文件,然后运行所需的任何程序。 这都是通过在窗口中键入命令来完成的。

Java编译器 ( The Java Compiler )

Compile the Applet

We need the terminal window to access the Java compiler called "javac". This is the program that will read the code in the FirstApplet.java file, and translate it into a language that your computer can understand. This process is called compiling. Just like Java applications, Java applets must be compiled too.

我们需要终端窗口来访问名为“ javac”的Java编译器。 该程序将读取FirstApplet.java文件中的代码,并将其翻译为您的计算机可以理解的语言。 此过程称为编译。 就像Java应用程序一样,Java applet也必须进行编译。

To run javac from the terminal window, you need to tell your computer where it is. On some machines, it’s in a directory called "C:\Program Files\Java\jdk1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" and find out where it lives.

要从终端窗口运行javac,您需要告诉计算机它在哪里。 在某些计算机上,它位于名为“ C:\ Program Files \ Java \ jdk1.6.0_06 \ bin”的目录中。 如果您没有此目录,请在Windows资源管理器中搜索“ javac”文件,然后查找其存放位置。

Once you’ve found its location, type the following command into the terminal window:

找到其位置后,在终端窗口中键入以下命令:

E.g.,

例如,

Press Enter. The terminal window won’t do anything flashy, it will just return to the command prompt. However, the path to the compiler has now been set.

按Enter键。 终端窗口不会做任何浮华的事情,它只会返回到命令提示符。 但是,现已设置了编译器的路径。

更改目录 ( Change the Directory )

Change Directory

Navigate to where the FirstApplet.java file is saved. For example: "C:\Documents and Settings\Paul\My Documents\Java\Applets".

导航到FirstApplet.java文件的保存位置。 例如:“ C:\ Documents and Settings \ Paul \ My Documents \ Java \ Applets”。

To change the directory in the terminal window, type in the command:

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

E.g.,

例如,

You can tell if you’re in the right directory by looking to the left of the cursor.

您可以通过查看光标左侧来判断自己是否在正确的目录中。

编译小程序 ( Compile the Applet )

Compile the Applet

We’re now ready to compile the applet. To do so, enter the command:

现在,我们准备编译小程序。 为此,输入命令:

After you hit Enter, the compiler will look at the code contained within the FirstApplet.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

按下Enter键后,编译器将查看FirstApplet.java文件中包含的代码,并尝试对其进行编译。 如果不能,它将显示一系列错误以帮助您修复代码。

The applet has been compiled successfully if you are returned to the command prompt without any messages. If that’s not the case, go back and check the code you’ve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.

如果返回到命令提示符而没有任何消息,则小程序已成功编译。 如果不是这种情况,请返回并检查您编写的代码。 确保它与示例代码匹配,然后重新保存文件。 继续执行此操作,直到可以运行javac而不会出现任何错误为止。

Tip: Once the applet has been successfully compiled, you will see a new file in the same directory. It will be called “FirstApplet.class”. This is the compiled version of your applet.

提示:小程序成功编译后,您将在同一目录中看到一个新文件。 它将被称为“ FirstApplet.class”。 这是您的applet的编译版本。

创建HTML文件 ( Create the HTML File )

HTML File

It’s worth noting that so far you have followed exactly the same steps as you would if you were creating a Java application. The applet has been created and saved in a text file, and it has been compiled by the javac compiler.

值得注意的是,到目前为止,您所执行的步骤与创建Java应用程序时完全相同。 小程序已创建并保存在文本文件中,并且已由javac编译器进行了编译。

Java Applets differ from Java applications when it comes to running them. What’s needed now is a web page that references the FirstApplet.class file. Remember, the class file is the compiled version of your applet; this is the file your computer can understand and execute.

Java Applet在运行它们方面不同于Java应用程序。 现在需要的是一个引用FirstApplet.class文件的网页。 记住,类文件是小应用程序的编译版本。 这是您的计算机可以理解和执行的文件。

Open up Notepad, and type in the following HTML code:

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

Save the file as “MyWebpage.html” in the same directory as your Java applet files.

将文件另存为与Java applet文件相同的目录中的“ MyWebpage.html”。

This is the most important line in the webpage:

这是网页中最重要的一行:

When the web page is displayed, it tells the browser to open up your Java applet and run it.

显示该网页时,它告诉浏览器打开Java小程序并运行它。

打开HTML页面 ( Open the HTML Page )

A Simple Java Applet

The last step is the best one; you get to see the Java applet in action. Use Windows Explorer to navigate to the directory where the HTML page is stored. For example, "C:\Documents and Settings\Paul\My Documents\Java\Applets" with the other Java applet files.

最后一步是最好的一步; 您将看到Java小程序的实际应用。 使用Windows资源管理器导航到HTML页面存储的目录。 例如,“ C:\ Documents and Settings \ Paul \ My Documents \ Java \ Applets”和其他Java applet文件。

Double-click on the MyWebpage.html file. Your default browser will open, and the Java applet will run.

双击MyWebpage.html文件。 您的默认浏览器将打开,并且Java applet将运行。

Congratulations, you have created your first Java applet!

恭喜,您已经创建了第一个Java applet!

快速回顾 ( A Quick Recap )

Take a moment to review the steps you took to create the Java applet. They will be the same for every applet you make:

花点时间回顾一下创建Java小程序所采取的步骤。 对于您制作的每个小程序,它们都是相同的:

  1. Write the Java code in a text file

    将Java代码写入文本文件
  2. Save the file

    保存文件
  3. Compile the code

    编译代码
  4. Fix any errors

    修正任何错误
  5. Reference the applet in a HTML page

    在HTML页面中引用小程序
  6. Run the applet by viewing the web page

    通过查看网页运行小程序

翻译自: https://www.thoughtco.com/building-your-first-java-applet-2034332

java applet

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值