Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Putting an Applet on a Web Page
Applets are placed on a Web page in the same way that anything unusual is put on a page: HTML commands are used to describe the applet, and the Web browser loads it along with the other parts of the page. If you have used HTML to create a Web page, you know that it's a way to combine formatted text, images, sound, and other elements together. HTML uses special commands called tags that are surrounded by < and > marks, including <IMG> for the display of images, <P> for the insertion of a paragraph mark, and <CENTER> to center the text that follows until a </CENTER> tag is reached.
The performance of some of these HTML tags can be affected by attributes that determine how they function. For example, SRC is an attribute of the <IMG> tag, and it provides the name of the image file that should be displayed. The following is an example of an <IMG> tag:
<IMG SRC="Graduation.jpg">
One way to place applets on a Web page is by using an <APPLET> tag and several attributes. The following is an example of the HTML required to put an applet on a page:
<APPLET CODE="StripYahtzee.class" CODEBASE="javadir" HEIGHT=300 WIDTH=400> Sorry, no dice ... this requires a Java-enabled browser. </APPLET>
The CODE attribute identifies the name of the applet's class file. If more than one class file is being used with an applet, CODE should refer to the main class file that is a subclass of the JApplet class.
If there is no CODEBASE attribute, all files associated with the applet should be in the same folder as the Web page that loads the program. CODEBASE should contain a reference to the folder or subfolder where the applet and any related files can be found. In the preceding example, CODEBASE indicates that the StripYahtzee applet can be found in the javadir subfolder.
The HEIGHT and WIDTH attributes designate the exact size of the applet window on the Web page. It must be big enough to handle the things you are displaying in your applet.
In between the opening <APPLET> tag and the closing </APPLET> tag, you can provide an alternate of some kind for Web users whose browser software cannot run Java programs (less than five percent of all Web users run browsers that fall into this group). In the preceding example, the text "Sorry, no dice…this requires a Java-enabled browser" is displayed in place of the applet on a browser such as Lynx, which does not support Java. You can put instructions here on how to download a Java-enabled browser from Netscape, Microsoft, or Opera Software. You can also include hyperlinks and other HTML elements.
Another attribute you can use with applets is ALIGN. It designates how the applet will be displayed in relation to the surrounding material on the page, including text and graphics. Values include ALIGN="Left", ALIGN="Right", and others.
A Sample Applet | Next Section

Account Sign In
View your cart