Each window component
maintains a database of its attributes.
After a component has been added to the application window, the programmer
can access or modify its attributes by calling the appropriate methods. These
methods typically have the forms
widget.getAttributeName()
widget.setAttributeName(newValue)
As you have seen in the
hello world example, a label has a text font and a foreground color that can be
modified in this manner. A label
also can show either text or an image, depending on the setting of these
attributes. The next program, which
displays an image and a caption, shows how to set the image, font, and color attributes
of labels.
import javax.swing.*;
import BreezySwing.*;
import java.awt.Color;
import java.awt.Font;
public class ImageDemo
extends GBFrame{
public
ImageDemo(Icon image){
JLabel imageLabel = addLabel ("", 1,1,1,1);
imageLabel.setIcon (image);
JLabel captionLabel = addLabel ("Smokey the Cat", 2,1,1,1);
captionLabel.setFont (new Font
("Verdana", Font.ITALIC, 24));
captionLabel.setForeground (Color.blue);
}
// Create and display the
window when the app launches
public
static void main(String[] args){
Icon
image = new ImageIcon ("smokey.jpg");
JFrame frm = new ImageDemo (image);
//
Set the window's dimensions, leaving room for the title bar and caption
frm.setSize (image.getIconWidth(),
image.getIconHeight() + 50);
frm.setVisible (true);
}
}
Note that the main method of this program loads the image from a file
before the window is created, and passes this image as an argument to the
window's constructor. The main
method then uses the width and height of the image to set the width and height
of the window. Here is the window produced by the program:
|
Back to tutorial | Next topic: Message boxes |