JSP Servlets   «Prev  Next»

Lesson 3 Our sample applet
ObjectiveReview the code for an applet that works like a form.

Java Applet Tutorial

Building simple user interfaces is easy with the Java AWT. Components like buttons and input fields are provided for you to use. If you want a very simple appearance, and are not concerned with the precise appearance of your applet, you can get what you need in just a few lines of code. Here is the code for a complete applet that displays a form used to collect a name:
import the applet and AWT classes to use their short names
  1. import the applet and AWT classes to use their short names
  2. start of the applet definition
  3. typically the user interface is built in init()
  4. add a text label to the applet user interface
  5. create an input field for text
  6. add the input field to the applet user interface
  7. create a button
  8. add the button to the applet user interface
  9. end of the init() override
  10. end of the applet


Applet appearance (JFC versus JavaFX)

Applet embedded in HTML
Applet embedded in HTML

  line 1-2 import the applet and AWT classes to use their short names
  line 4 start of the applet definition
  line 5 typically the user interface is built in init()
  line 7 add a text label to the applet user interface
  line 8 create an input field for text
  line 9 add the input field to the applet user interface
  line 10 create a button
  line 11 add the button to the applet user interface
  line 12 end of the init() override
  line 13 end of the applet


To test this applet, you will need HTML.
Here is a page that will work:
<html>
<head>
<title>Form Tester</title>
<body bgcolor=white>
<h1>This applet behaves like an HTML form</h1>
<applet code="FormApplet.class" width=300 height=80>
</applet>
</body>
</html> 

The following diagram demonstrates what it looks like when this applet is run.

This text comes from the HTML around the applet
  1. This text comes from the HTML around the applet
  2. The label
  3. The input text field
  4. The button


Of course, this applet does not actually do anything with the information you add to the form, it just displays the input field and button. In the next lesson, you will learn to design an applet that sends GET to a servlet

Form Applet - Exercise

Build an applet that displays a form in this exercise. Form Applet - Exercise