Java: AWT and swing
There have been three major versions of the graphical
user interface.
AWT and Swing
The Graphical User Interface (GUI) that Java offers has
changed with each version of Java.
- Java 1.0
-
This offered the AWT (Abstract Windowing Toolkit) which
was a basic set of tools for building and managing a GUI,
but it lacked some important features. It is now considered
obsolete and should not be used.
- Java 1.1
-
This version made a basic change in event handling of the AWT.
It is still commonly used in applets so that the applets work
on all versions of browsers. However, using these AWT components
is not a good idea for new applications because they are much
more limited.
- Java 1.2/2.0
-
This introduced the Swing GUI, which
provides the facilities to create a really good interface.
Use Swing components, not AWT
Swing did not replace everything in AWT, therefore you must
use a mixture of the two. However, never mix
the components from Swing and AWT. The
Swing component names generally start with 'J' (eg
JButton), and the AWT components don't (eg Button).
The Swing components are "lighter", faster, and use less memory.
Although mixing the two types will not produce a compile-time error,
the result may not display the way you want it.
The Graphics, Color, Font, ... classes are still used from AWT,
so you still need to use the AWT.
Imports
For a graphical user interface, you typically have these import statements:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
There are many other packages, but these are the most common.
Note that the AWT packages are in "java" and the Swing classes
are in "javax".
AWT Graphics
This is the collection of basic classes and methods for
drawing, colors, fonts, images, etc. The paintComponent(Graphics g)
method is still called with an AWT Graphics object.
Java 2 Graphics2D
Java 2 has added many additional classes for working with graphics, and
a Graphics object can easily
be downcast to Graphics2D to take advantage of the additional facilities.