|
Below is a listing of some of the most common GUI
classes and interfaces used. It is not complete by any means,
and is only intended as a quick overview for novices. For more
complete discussions of the classes involved, please see the main Java API
documentation.
"AWT" stands for "Abstract Windowing Toolkit"
which is the primary set of classes used
by Java to produce GUI's. "Swing" stands for the latest edition of GUI
components that are designed for better performance and flexibility over the original
AWT components. Given a choice, always choose the Swing component over the AWT
component.
-
java.awt.Component
These components allow you to display textual or
iconic information on the screen.
This method set the text of the label to
the supplied String value.
This method retrieves the text of
the label.
These components allow your program to
respond to mouse clicks (note: responding to mouse clicks is not
restricted to buttons).
This method set the text of the button to
the supplied String value.
This method retrieves the text of
the button.
These components allow you to enter a single
line of text information.
This method set the text of the text field to
the supplied String value.
This method retrieves the text
of the text field.
These components allow you to input and
display multiple lines of text
information.
This
method set the text of the text area to
the supplied String value.
This method retrieves the text of
the text area.
This method adds the supplied text
to the existing text in the text
area.
Containers are components that can contain other components (Composite design
pattern).
A window with a title bar, resizing controls
and optionally, menu and task bars. The components that a
JFrame holds are organize into "panes" which are individual layers
of components. Panes can be tranparent or opaque, visible or
invisible and can be used to rapidly switch from one collection
of components, to another.
-
Container getContentPane()
This method returns the "content
pane" which is the default pane for a JFrame. This is
normally the only pane that is used. Components
need to be added to this
pane.
This container is used inside frames or other panels to
isolate areas of the screen. For instance, a set of
buttons can be grouped together by placing them all in the same
panel and thus be separated from other collections of components.
A total layout of a GUI usually consists of many layers of panels
holding other
panels.
-
Methods common to all Containers
This method adds the supplied Component to the
container and subjects it to the Container's installed
layout.
Sets the container's layout to the
supplied
layout.
Layouts control the automatic positioning of Components in a
Container. This enables the Java program to run on a
wide variety of operating systems and window managers which all
have slightly different ideas of what graphical elements should
look like. It also enables a window and the
components in it to properly
resize.
This places components in one of 5 locations: around
the edge of the container ("east", "west", "north" or "south")
or in the center ("center"); This is the default layout for
frames.
This places components in a single centered line in the
container. It is the default layout for
panels.
This places components in a mxn grid, where m and n
can be specified. Using -1 for the number of rows or
columns allows you to be unspecific--the number of rows or
columns that will be shown will correspond to the number of
components
added.
-
Methods Common to All Components
This method is called by the Java graphics
subsystem ("the "AWT" subsystem) whenever a component needs to be
drawn ("painted"). The Graphics object handed to the
method by the AWT system represents the screen onto which
the Component can draw itself. The (0,
0) coordinate of the Graphics object is the upper left hand
corner of the Component on the screen. Since painting
a Component often involves a number of processes that programmer
may not care about, the first line of an overriden paint() method is usually
super.paint(g);
to let the Component take care of what is
usually done, such as clearing the screen.
The paint() method is
NEVER called directly from a program! Only the AWT
system can call it, as the AWT system is the only thing that can
correctly provide the proper Graphics
object.
This method sends a request to
the AWT system to repaint the Component. There is no
guarantee as to when the Component will be
repainted! A common mistake is to assume that the
screen gets repainted as soon as a a repaint() method is called.
A program can only call
repaint() to request that the screen be repainted, never paint()
directly.
This method causes the the
Component to recalculate the layout of the Component.
Usually only used on Containers to re-layout the contained
Components. Useful when Components are being added,
removed or moved dynamically.
-
void addActionListener(ActionListener al)
This method adds the supplied listener to the set of
"observers" that the Component (the "Observable") will call when
an event occurs.
-
void setEnabled(Boolean isEnabled)
This method enables or disables the Component's ability to
process events. Useful for things like disabling
buttons when certain processes are taking
place.
-
void setVisible(Boolean isVisible)
This method shows or hides the component on the
screen.
-
void setSize(int width, int height)
This method sets the width and height of the component
in pixels.
-
void setPreferredSize(Dimension d)
This is a method of javax.swing.JComponent, not
Component. It sets the size the JComponent should be, if the
Java graphics system can do it. Dimension is a class
that holds a width and a height, e.g
myJComponent.setPreferredSize(new Dimension( myWidth,
myHeight)).
-
Other Classes and Interfaces
This class represents a color to Java. It has a
number of public static fields that can be used to represent common
colors: Color.red, Color.blue, Color.black, Color.white,
etc.
These objects represent the graphics surface
of the screen. That is, any direct painting of the screen, such
as for drawing lines or shapes, is done on a Graphics
object.
Sets the color to be used for the next drawing task.
-
void drawLine(int x1, int y1, int x2, int y2)
Draws a line from (x1, y1) to (x2,
y2). The origin used is the upper left corner of the
Component that is being painted upon. Positive y
values are downward and positive x values are to the right.
-
void drawArc(int x, int y,
int width, int height, int startAngle, int arcAngle)
Draws an arc using the
following parameters:
x- the x
coordinate of the upper-left corner of the arc to be drawn.
y - the y coordinate of the upper-left
corner of the arc to be drawn. width - the
width of the arc to be drawn. height - the
height of the arc to be drawn. startAngle - the
beginning angle. arcAngle - the angular extent
of the arc, relative to the start
angle.
There is also a fillArc() method that will create a
filled
arc.
-
java.awt.event.ActionListener
This interface provides the
actionPerformed(ActionEvent e) method that Components such as
buttons, labels and text fields call when mouse
clicked. This class defines a listener for
those components. There is no associated adapter class since ther is only
one method.
-
java.awt.event.ActionEvent
This class encapsulates all the relevant information about an
event. The Java AWT system will construct an instance of
this class and pass it as a parameter when the ActionListener's
actionPerformed() is called. Good OO code rarely
needs the information in the ActionEvent because the information is
already known via polymorphism.
-
java.awt.event.MouseListener
This interface is used for events that concern mouse presses,
releases, entering a component, leaving a component,
etc. It has an associated MouseEvent class that is used
to encapsulate the specifics of the event. The
MouseInputAdapter class implements this interface and gives default
no-operation behavior to all the
methods.
-
java.awt.event.MouseMotionListener
This interface is very similar to MouseListener, but is for
events such as the mouse is moving or the mouse is
dragged. The event information is encapsulated in
MouseEvent classes. The MouseInputAdapter also
implements this interface. MouseMotionAdapter only
implements this interface.
-
java.awt.event.KeyListener
This interface is used for key pressed, key
released and character typed events. The event specifics
are encapsulated in KeyEvent objects. The associated
adapter is KeyAdapter.
|