You can draw things on the screen, but there are things that have already been
made for you to put on the screen. These are called components. An example of a
component is a label, button or scrollbar.
The Java Swing classes provide the tools that help you build a good graphical
user interface. You've already used some of the drawing methods. In this section
you will learn to use Swing components.
Some advantages of using these components are:
Java has already defined them along with a
number of methods for customizing them.
They paint themselves with their own
paintComponent method, and redraw themselves as necessary when their
They automatically position and resize themselves
depending on the layout.
Most allow user interaction which can result in
calls to your methods to process the input.
Defining your own components
The predefined Java components are adequate for all normal programming, with the
one exception of drawing graphical output. In this case you want to define your
own "component" that draws itself, which is easily done with a subclass of
JPanel and overriding the default paintComponent method.
Some of the kinds of components that you will learn to use are JLabel,
JTextField, and JButton.