Home  >  Article  >  Java  >  Take you to a preliminary understanding of Java Applet program

Take you to a preliminary understanding of Java Applet program

Y2J
Y2JOriginal
2017-05-18 10:16:322312browse

The main purpose of this article is to create a simple Java applet. Friends who need it can refer to the following

Introduction

Note: Before you start this tutorial, you The downloaded and installed Java SE Development Kit must be downloaded and installed.

Java applets are like Java applications. They are created by following the same three steps - writing, compiling and running. The difference is that they run on part of the web page, not on your desktop.

The main purpose of this article is to create a simple Java applet. To achieve this there are three basic steps to follow:

1. Write a simple applet in Java

2. Compile Java source code

3. Create an HTML page involving the applet

4. Open the HTML page in the browser

Write Java source code

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

I use Notepad to create my Java source code files. Open the editor of your choice and enter code like this:

//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);
  }
   }

Don’t worry too much about the meaning of the code. This is your first applet, and it's important to see how it is created, compiled, and run.

Save the file

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

Save your program file as " FirstApplet.java". Make sure you use the correct file name. If you see code like this:

public class FirstApplet extends Applet {

This is an instruction to call the applet class "FirstApplet". The file name should match the name of the class and have a ".java" extension. If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.

Open a terminal window

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

To open a terminal window , you need to press "Windows

key" and the letter "R".

You will see "Run Dialog". Enter "cmd" and click "OK".

The terminal window will be displayed. It thinks of it as a text version of Windows Explorer; it lets you browse different directories on your computer, look at the files they contain, and run the programs you want. This can be done by typing the command in the window.

Java Compiler

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

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

In order to run javac in a terminal window, you need to tell the computer where it is. On my machine it's in the directory "C:\Program Files\Java\jdk1.6.0_06\bin". If you don't have such a directory,

search

the file "javac" in Windows Explorer to find its location. Once you find its location, enter the following command into the terminal window:

set path= *the directory where javac lives*

E.g.,

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

Press Enter. The terminal window doesn't do anything flashy, it just returns you to the command prompt. However, the compiler path has now been set.

Change directory

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

Look where it is saved FirstApplet.java file. My files are saved in "C:\Documents and Settings\Paul\My Documents\Java\Applets".

To change the directory in the terminal window, enter the following command:

cd *directory where FirstApplet.java file is saved*

E.g.,

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

You can tell you are in the directory on the right by seeing the cursor on the left. We are now ready to compile the applet. Enter the command:

javac FirstApplet.java

After clicking Enter, the compiler will see the code contained in the FirstApplet.java file and try to compile it. If it can't, it will display a series of errors to help you fix the code.

如果在没有任何信息提示下你返回到命令提示符处,说明你的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代码:

<HTML>
<HEAD>
<TITLE>My First Java Applet
</HEAD>
<BODY>
Here&#39;s my first Java Applet: 
<applet code="FirstApplet.class" width="300" height ="300">
</BODY>
</HTML>

在相同的目录中文件另存为“MyWebpage.html”作为你的Java applet 文件。

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

44b2bfd9e7f8db29dba4bcda95e40acc

当网页被显示,它会告诉浏览器打开你的Java applet并运行它。

打开HTML页

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

最后一步也是最好的一个;我们可以看到Java applet开始运行了。使用Windows Explorer来导航在 HTML页中储存的目录。例如,我的网页保存在“C:\Documents and Settings\Paul\My Documents\Java\Applets”和我的另一个Java applet文件。

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

恭喜你!你已经创建了你的第一个Java applet!

快速小总结

花一点时间来总结一下创建Java applet的步骤。它们在你创建的每一个applet都是相同的:

1. 在文本文件中编写Java 代码

2. 保存文件

3. 编译代码

4. 修复错误

5. 在HTML也中参考applet

6. 过浏览网页运行applet

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本下载

2. Java免费视频教程

3. 教你如何去配置Applet的环境

4. 详解Application和Applet的有什么不同之处

5. 详细介绍什么是Java applet

The above is the detailed content of Take you to a preliminary understanding of Java Applet program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn