The largest type of container is a frame. This container can be created simply as an object of class JFrame of the Swing library.
An empty frame window can be built by creating an object of class JFrame with a specified title and setting the size for it. Figure 14.2 shows an empty frame (window). The relevant properties of this frame are its title, color, and size.
Figure 14.2: A sample empty frame.
The KJP code in Class Frame_sample implements the construction of an empty frame with title "Frame sample" and size of 400 by 300 pixels. The title is set by invoking the constructor of the frame object, and the size is set by invoking the method setSize of object frame_obj. To make the frame window visible, method setVisible is invoked.
On the CD | The KJP code that implements class Frame_sample follows. It is stored in the file Frame_sample.kpl. The Java implementation of the class is stored in the file Frame_sample.java. |
import all javax.swing // Library for graphics description This class creates and displays an empty frame window. */ class Frame_sample is public description This is the main function of the application. */ function main is constants integer WIDTH = 400 integer HEIGHT = 300 objects object frame_obj of class JFrame begin create frame_obj of class JFrame using "Frame sample" call setSize of frame_obj using WIDTH, HEIGHT call setVisible of frame_obj using true endfun main endclass Frame_sample
Note | The size of the graphical object on the screen is measured in pixels. A pixel is the smallest unit of space that can be displayed on the video screen. The total number of pixels on the screen defines the resolution of the screen. High-resolution screens have a larger number of pixels, and the pixels are much smaller. |
The simplest types of components are labels, which can display text titles and images. Text labels just display their text titles when they appear on a container of a window. A text label is defined as an object of class JLabel. The following KJP statements declare two text labels:
object blabel1 of class JLabel // text label object blabel2 of class JLabel
When the objects of class JLabel are created, their text titles are defined. The following KJP statements create the two text objects and define their associated text titles.
create blabel1 of class JLabel using "Kennesaw Java Preprocessor" create blabel2 of class JLabel using "The Language for OOP"
Labels can also display pictures by indicating icons for the pictures. Image labels display a picture by indicating the corresponding icon. In classes using Swing, a picture is set up into an icon in a label, so that the classes can position the label in a container and display it. The pictures are normally in a standard format, such as JPEG or GIF.
Note | A picture is defined as an icon, which is an object of class ImageIcon. The icon is then defined as part of the label. The following statements declare an object variable of class ImageIcon and an object of class JLabel.
object kjpimage of class ImageIcon // image object kjplabel of class JLabel // for image |
On the CD | The icon object is created with a picture stored in a specified picture file. The following statement creates the icon object kjpimage with a picture in the file kjplogo.gif.
create kjpimage of class ImageIcon using "kjplogo2.gif" |
Finally, with the icon object created, it can be defined as part of a label. The following statement creates the label object with the icon defined in object variable kjpimage.
create kjplabel of class JLabel using kjpimage
Components cannot be added directly to a window, which is an object of class JFrame. A special container called the content pane defines the working area for the window. All the graphical elements, components, and smaller containers are added to the content pane. The content pane is an object of class Container.
There are several ways to arrange graphical elements in the content pane. The type of arrangement is defined by the layout manager selected. The AWT package provides six layout managers:
Border, which arranges the components in the north, east, west, center, and south positions in the container
Flow, which arranges the components in the container from left to right
Grid, which arranges the components in a matrix with row and column positions
Box, which arranges the components in a single row or single column
Card, which arranges the components in a similar manner as the stack of cards
Gridbag, which arranges the components in a similar manner as the grid but with variable size cells
The most common layout managers are the first three: border, flow, and grid. Figure 14.3 shows the positioning of components using the border layout manager.
Figure 14.3: Arrangement of the border layout manager.
As mentioned before, the content pane is an object of class Container. To manipulate the content pane object, a reference to this object is accessed by invoking method getContentPane of the window. Before adding the various graphical elements to the content pane, the layout must be set by invoking the method setLayout of the content pane.
On the CD | The following class, Kjplogo, sets up a window (an object of class JFrame) with three components: two text labels and an image label. The KJP code for class Kjplogo is implemented and stored in file Kjplogo.kpl. |
import all javax.swing // Library for graphics import all java.awt description This class creates and displays a frame window with an image and two text labels. */ class Kjplogo is public description This is the main function of the application. */ function main is constants integer WIDTH = 400 integer HEIGHT = 300 objects object frame_obj of class JFrame // window // content pane object cpane of class Container object blabel1 of class JLabel // text label object blabel2 of class JLabel object kjpimage of class ImageIcon // image object kjplabel of class JLabel // for image // layout manager object lmanager of class BorderLayout begin create frame_obj of class JFrame using "KJP logo" create blabel1 of class JLabel using "Kennesaw Java Preprocessor" create blabel2 of class JLabel using "The Language for OOP" create kjpimage of class ImageIcon using "kjplogo2.gif" create kjplabel of class JLabel using kjpimage create lmanager of class BorderLayout set cpane = call getContentPane of frame_obj call setLayout of cpane using lmanager // add the text image label and text label // components // to the content pane of the window call add of cpane using kjplabel, BorderLayout.CENTER call add of cpane using blabel1, BorderLayout.NORTH call add of cpane using blabel2, BorderLayout.SOUTH call setSize of frame_obj using WIDTH, HEIGHT call setVisible of frame_obj using true endfun main endclass Kjplogo
Figure 14.4 shows the window that appears on the screen when the program in class Kjplogo executes.
Figure 14.4: A frame with three components.
Two attributes of frames that are used in the previous examples are title and size. The title was set with the constructor of class JFrame when creating a frame object. This attribute can be set at any time by invoking method setTitle with a string argument. For example, the following statement sets a title to the frame object declared with reference frame_obj in the previous example:
call setTitle using "New Frame Title"
The size of the frame object is normally set before displaying it on the screen. As mentioned before, the units for size are in pixels. The previous example used two named constants: WIDTH and HEIGHT. This is the recommended manner to set the values for the size. The size is set by invoking method setSize with the values in pixels for width and height of the frame. For example, to set the size of the frame referenced by frame_obj, the statement is:
call setSize of frame_obj using WIDTH, HEIGHT
The third attribute of a frame is the color. This attribute is normally set to a container in the frame, such as the content pane of the frame or the panels defined and contained in the content pane. In other words, the color of any of these containers can directly be set. The most common method to invoke for a container is setBackground. The argument is normally a predefined constant in class Color, which is available in the AWT package. These constants represent various colors to apply as background color to the container. The most common constants for the colors are listed in Table 14.1.
Color.blue | Color.orange | Color.green |
Color.gray | Color.magenta | Color.pink |
Color.red | Color.white | Color.yellow |
Color.darkGray | Color.lightGray |
To set the background color of a container in a frame object, method setBackground is invoked with the selected color. This method is defined in class JFrame. The following statement sets the background color to pink in frame frame_obj of the previous example. Recall that cpane is the content pane of frame_obj.
call setBackground of cpane using Color.pink