Java Reference
In-Depth Information
discussed previously in Supplement 3G, fonts are represented by Font objects. A
Font object can be constructed by passing its name, style (such as bold or italic), and
size in pixels. For example, the following line of code tells the text area to use a size
14 serifed font:
area.setFont(new Font("Serif", Font.BOLD, 14));
The text area now has the following appearance:
See Supplement 3G for a more lengthy discussion of Font objects.
Icons
Many Java components, including buttons and labels, have an icon property that can
be set to place an image on the component. This property is of type Icon .
The getIcon and setIcon methods use values that implement the Icon inter-
face. The easiest way to get an object that implements Icon is to create an object
of type ImageIcon . The ImageIcon constructor accepts a String parameter that
represents the image file to load. The image is a file on your hard disk, such as a
GIF, JPG, or PNG image. Place your image files in the same folder as your pro-
gram files.
The following code places an icon on a button. The icon image comes from a file
called smiley.jpg that has already been saved to the same folder as the code:
// create a smiley face icon for this button
JButton button = new JButton("Have a nice day");
button.setIcon(new ImageIcon("smiley.jpg"));
The screenshot that follows shows the appearance of the button when it is placed
in a frame. It is enlarged to accommodate both its text and its new icon:
 
Search WWH ::




Custom Search