Java Reference
In-Depth Information
17.2.3
Other components
Sections 17.2.1 and 17.2.2 describe the basic components that are used in GUIs:
buttons, labels, text fields, and text areas. A number of other components can be
placed in a JFrame . We list their classes here and summarize what each is.
JSlider : A bar with a tab, which the user can move.
JCheckBox : A titled box, which the user can check or uncheck.
JRadioButton : a titled circle, which the user can check or uncheck.
JComboBox : A menu of items; the user selects one, which is then shown.
JList : A list of items, all showing (if possible); the user selects items.
JColorChooser . An instance allows the user to choose a color. It is fun!
In addition, a class ButtonGroup can be used to group a bunch of JRadio-
Buttons so that only one can be selected at any time. Select one, and the others
in the group become unselected.
Activity 17-2.4 shows how sliders are created and used, and each of the
other components listed above is described extensively in a footnote on lesson
page 17-2 of the CD. The illustrations are in color and far better than we could
do here, on paper. Further, from lesson page 17.2, you can obtain subclasses of
JFrame that have the components on them. The ProgramLive CD is the best
place to see examples of these components.
Lesson page 17-
2 describes bet-
ter all the com-
ponents men-
tioned in this
section. Also,
get sample clas-
ses from there.
import javax.swing.*;
import java.awt.*;
public class GraphicsPanelExample extends JPanel {
/** width and height of the panel and a color for painting the panel */
private int width;
private int height;
private Color color;
/** Constructor: a JPanel that is colored c and has width w and height h */
public GraphicsPanelExample( int w, int h, Color c) {
super ();
color= c;
width= w;
height= h;
setPreferredSize( new Dimension(width, height));
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(0, 0, width, height);
}
}
Figure 17.5:
A JPanel that is painted a certain color
Search WWH ::




Custom Search