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.
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,
searchthe 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's my first Java Applet: <applet code="FirstApplet.class" width="300" height ="300"> </BODY> </HTML>
在相同的目录中文件另存为“MyWebpage.html”作为你的Java applet 文件。
这个是在网页中最重要的一行:
当网页被显示,它会告诉浏览器打开你的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免费视频教程
4. 详解Application和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!

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
