Java Reference
In-Depth Information
Table 14.4
Useful Properties of All Components (Including JFrames )
Property
Type
Description
Methods
background
Background color
Color
getBackground,
setBackground(Color)
enabled
Whether the component can be
boolean
isEnabled,
interacted with
setEnabled(boolean)
focusable
Whether the keyboard can send
boolean
isFocusable,
input to the component
setFocusable(boolean)
font
Font used to write text
Font
getFont, setFont(Font)
foreground
Foreground color
Color
getForeground,
setForeground(Color)
location
( x , y ) coordinate of component's
Point
getLocation,
top-left corner
setLocation(Point)
size
Current width and height of the
Dimension
getSize,
component
setSize(Dimension)
preferred size
“Preferred” width and height of
Dimension
getPreferredSize,
the component; the size it should
setPreferredSize(Dimension)
be to make it appear naturally on
the screen (used with layout
managers, seen later)
visible
Whether the component can be
boolean
isVisible,
seen on the screen
setVisible(boolean)
It turns out that all graphical components and frames share a common set of
properties, because they exist in a common inheritance hierarchy. The Swing GUI
framework is a powerful example of the code sharing of inheritance, since many
components share features represented in common superclasses. Table 14.4 lists
several useful common properties of frames/components and their respective
methods.
Several of the properties in Table 14.4 are objects. The background and fore-
ground properties are Color objects, which you may have seen previously in
Supplement 3G. The location property is a Point object; the Point class was dis-
cussed in Chapter 8. The font property is a Font object; we'll discuss fonts later in
this chapter. All of these types are found in the java.awt package, so be sure to
import it, just as you did when you were drawing graphical programs with
DrawingPanel in previous chapters:
import java.awt.*; // for various graphical objects
The size property is an object of type Dimension , which simply stores a width
and height and is constructed with two integers representing those values. We'll use
this property several times in this chapter. We only need to know a bit about the
 
Search WWH ::




Custom Search