Java Reference
In-Depth Information
A Short Introduction to AWT Programming
Although this topic is targeted at programmers who have some basic Java experience, a short
introduction to AWT programming is included here. The motivation is that modern Java desktop
applications are mostly based on SWING, and servlets are based on an HTML interface, so a large
fraction of Java programmers might not have much experience with AWT. Also, Rapid Application
Development tools contained in several J2SE IDEs might not yet be able to generate PDAP-compatible
code.
For a more detailed explanation of AWT, refer to a general Java book covering AWT or to the online
tutorials provided by Sun
http://java.sun.com/docs/books/tutorial/
.
An overview about AWT is
available from Sun's AWT Web site at
http://java.sun.com/products/jdk/awt/
.
Basic Component Model
Graphical user interfaces usually consist of a number of components like buttons, text fields, or check
boxes. In the Abstract Window Toolkit, these elements are represented by Java classes such as
Button
,
TextField
, and
Checkbox
. These classes handle the drawing of the corresponding
components, as well as basic handling of user interactions. Most elements of the user interface are
derived from the abstract base class
Component
. The
Component
class provides access methods for
the common properties of the user interface classes, such as the size and position.
A special subclass of
Component
is
Container
, which can contain a set of other components—
including containers. A special example of a container is a
Frame
. The
Frame
class represents a
regular screen window. Other containers are
Panel
s, which are used for grouping components, and
dialog windows.
Using a
Frame
and a
Label
, you are already able to build an AWT-based "Hello World" program
Label
showing
"Hello World"
is added to the
Frame
.
Listing 4.1
ComponentSample.java
import java.awt.*;
import javax.microedition.midlet.*;
public class ComponentSample extends MIDlet {
Frame frame;
public ComponentSample() {
frame = new Frame ("Hello World");
frame.add(new Label ("Hello World"));
}
public void startApp() {
frame.show();
}
public void pauseApp() {
}
public void destroyApp (boolean conditional) {
frame.dispose();
}
}
Search WWH ::
Custom Search