content









Content










11.5
GUI
Components in Applets




11.5.1
GUI
components




The Applet class extends from Panel,
which extends from Container. So, an Applet is a Container. Any
Abstract Window Toolkit (AWT) component can be added to the applet.
Code can also be included to set the font and color of some of the
components. In addition to AWT components, graphic objects can be
included using the Graphics class to draw simple shapes or combine
simple shapes to create complex images. Some simple uses of Graphics
objects are described later in this chapter.
The following code demonstrates the use
of Label, TextField, and Button objects in an applet
.
This program presents the user with textfields to enter username and
password. The username and password are checked against a list. The
applet returns a message if the entry is correct. The actual
verification of user names and passwords in a business application can
be far more complex than this example. Often this will include
retrieving data from a database to verify that the information the
user has provided is correct. Here, it can be assumed that only a
single password, rosetime, is acceptable to the program.
Samples of output from the program are
shown in Figure . The class
definition includes references to import classes from three sources:
java.applet.* to use the Applet class; java.awt.* to use the Component
class and its subclasses; and java.awt.event.* to use Listener
interfaces and event objects. This applet implements the
ActionListener interface. The applet will include the labels banner1,
banner2, and result. The first two labels are used to display a prompt
to the user and label the textfield. The third label is added to the
applet in the actionPerformed method of the applet.
These concepts are demonstrated by the
following code:

Components are added in the init()
method. The init() method will be followed by the start() method,
which calls the paint() method to draw the graphics for each
component.
The PasswordTest object, referenced
by the keyword this, implements the ActionListener
interface:button1.addActionListener(this). The Listener object
will handle any ActionEvent objects that are generated by button1.
This statement can be described as the registering of an
ActionListener object with a component (button1) that generates
events. When a user presses the button on the applet screen, an
ActionEvent object will be generated.
The focus (that is, the cursor is
positioned in the textfield) is requested for the object
referenced by un: un.requestFocus();.
The actionPerformed method is
implemented, as this is the only method declared in the ActionListener Interface. In the actionPerformed method, the code
verifies the data provided by the user, changes the text for the
Label object referenced by result, and adds this label component
to the applet.
Because components of the applet
have changed, the invalidate() method is called to mark the applet
as invalid or out-of-date, and then a call to the validate()
method is given to redraw the window and implement the addition of
the new label referenced by result.

 










Wyszukiwarka

Podobne podstrony:
content
content
content
content
content
content
content
content
content
function domnode get content
content
content
content
content
content
content

więcej podobnych podstron