JComponent
Class Diagram
Ancestor class specifications:
JComponent
Class Specification
Invariant
A java.awt.Component object...
- is a transparent rectangular region
- should be added to a container object with its upper-left corner x pixels from the left and y pixels from the top. (The upper left corner of the container is (0, 0).)
Below is a JComponent added to the content pane of a JFrame. The Component is depicted as a dashed rectangle, although it would be transparent on an actual computer display.

Constructor Methods
public Component( )
post: A new Component object is created
and backColor and foreColor are transparent
(Note that Components also don't appear until added.)
Update
Methods
public void add( java.awt.Component c, int j )
pre: j == 0
post: c is placed on top of this JComponent located c.getX() pixels from the left and c.getY() pixels from the top of this JComponent.
public void remove( java.awt.Component c )
post: The c graphical object will be removed from this JComponent
note: Nothing occurs if c was not added to this JComponent.
public void paint( java.awt.Graphics g )
post: An invisible image has been drawn using g as its graphics context.
note: This method should be overridden in order to draw a desired image.
public void paintChildren( java.awt.Graphics g )
post: All objects that have been added to this are drawn.
note: This method is often called as the last statement within an implementation of paint.
public void setBackground( java.awt.Color c )
post: backColor == c
public void setForeground (java.awt.Color c )
post: foreColor == c
public void setBounds( int initX, int initY, int w, int h )
post: x == initX and y ==
initY and width ==
w and height == h
public void setLayout( java.awt.LayoutManager m ) )
post: m is established as the layout
manager for this JComponent.
note: In this book an argument of null is
suggested. This method need not
be
called except for the content pane of a JFrame.
public void setLocation( int initX, int initY )
post: x == initX and y ==
initY
public void setSize( int w, int h )
post: width == w and height == h
public void repaint( )
post: Causes the Java virtual machine to update the display of this JComponent, and its content, as soon as possible.
public void addKeyListener( java.awt.event.KeyListener lis )
post: Key listener events (keyPressed, keyReleased and keyTyped) have been redirected to the lis object
public void addMouseListener (java.awt.event.MouseListener lis )
post: Mouse listener events (mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased) have been redirected to the lis object
public void addMouseMotionListener( java.awt.event.MouseMotionListener lis )
post: Mouse motion listener events (mouseDragged and mouseMoved) have been redirected to the lis object
Query
Methods
public java.awt.Color getBackground( )
post: result ==
backColor
public java.awt.Color getForeground( )
post: result ==
foreColor
public int getX( )
post: result ==
x
public int getY( )
post: result ==
y
public int getWidth( )
post: result ==
width
public int getHeight( )
post: result ==
height
public java.awt.Container getParent( )
post: this Component is added to another
implies result ==
the Container to
which this is added
and this JComponent is not added to another implies result == null