In this magercise, you will explore the JRadioButton
,
JCheckbox
, and JScrollPane
classes. Here,
you'll create a basic pizza ordering program. The pizza parlor has three
possible sizes (Small, Medium, and Large), as well as three possible
toppings (Green Peppers, Pineapple, and Pepperoni). It's your job to
show the options to the customer and let them select their order. Once
an order is placed, a picture of the pizza ordered will be shown.
Magercise 4 Prerequisites
Buttons With Icons
Skeleton Code
Order.java
Resources
Image JAR File
The skeleton code contains a class to use as an Icon
for the
toppings, as well as the appropriate color definitions. It also handles
the pizza ordering.
Tasks
Save image JAR File to your
working directory.
Starting with the skeleton, create
a ButtonGroup
named sizeGroup
with three
JRadioButton
objects in it. For the included event
handling code to work without modifications, the sizeGroup
variable needs to be final
, while the button labels need
to be "Small", "Medium", and "Large".
Have the "Large" toggle initially selected. For each of
the buttons, associate an ActionCommand of the text label.
This will allow you to ask the ButtonGroup
which
JRadioButton
is selected.
Place the radio buttons within a vertical Box
in the west
area of the screen. Add a "Size" label to the top of the
box. Have the box appear in the vertical-middle of the area with 5
pixels separating the items.
The content pane for the JFrame
can be referenced by the
local variable content
.
For the toppings, create three JCheckbox
items, named
peppers
, pepperoni
, and pineapple
with
appropriate text labels for the three toppings. They should all start
out unselected, and the variables all need to be final
so the
event handling code works unchanged.
For each of the checkboxes, instead of using the default toggle box,
create icons in the appropriate color for both the selected and unselected
states. There is a support class provided, ColoredToggle
to use
to create each icon. Its constructor requires a color and the toggle state.
There are three predefined colors that you can use:
-
pepperColor
-
pepperoniColor
-
pineappleColor
Associate the icons to the appropriate JCheckbox
objects. Use
the setIcon()
method to associate the icon for the unselected
state and setSelectedIcon
for the selected state icon.
Place the checkboxes within a vertical Box
in the east area of
the screen. Add a "Toppings" label to the top of the box. Have
the box appear in the vertical-middle of the area with 5 pixels separating
the items.
Create a JScrollPane
with a JLabel
for the center part
of the screen. The label will be used to display the pizza image. It needs
to be named imageLabel
and be a final
variable.
The pizza ordering button is already added to the screen for you in the
South quadrant, so save everything and compile the program. Then run it
to see the results. Be sure to try all the different toppings since the
pizza is free of calories.
As a precaution, the Save command appends a "1"
to the end of the filename you want to save. This can help
prevent you from accidently overwriting your source code.
Where help exists, the task numbers above are linked to the
step-by-step
help page.
Pizza images used with permission from
The Internet Pizza Server.
The following JavaTM source file represents
a solution to this Magercise.
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.