Lab 12 In-Class: Graphical User Interfaces
Vote Counter - Using Buttons
The file VoteCounter.java contains
a slightly modified version of PushCounter.java in
Listing 5.13 of the text (pages 305-306).
As in the text the program counts the
number of times the button is pushed; however, it assumes ("pretends")
each push is a vote for Joe so the button and variables have been
- Compile the program, then use the appletviewer to see how it works
(you may use the file Vote.html).
- Modify the program so that there are two candidates to vote for --
Joe and Sam. To do this you need to do the following:
- Add instance variables for Sam -- a vote counter,
a button, and a label. Initialize these variables in the
- Add a new inner class named SamButtonListener to listen
for clicks on the button for Sam. This will be just like the
JoeButtonListener class except it will modify the variables
that hold Sam's information instead of Joe's. Create an instance of
this class when adding the ActionListener to the button
- Add the button and label for Sam to the Container object.
- Compile and run the program.
Greetings - Listening for Events on a Text Field
The file Greetings.java contains an
applet with a text field. When the user enters a name in the
text field and presses ENTER the applet displays a greeting
(there are four different greetings that can be displayed).
- Save the program to your directory, compile it and run it
in the appletviewer (you may use the file
Greetings.html). Press ENTER several times in the text field;
change the name at least once.
- The same listener in a program can listen for more than one
event. Modify the program by adding a button that, when pressed,
will display a new greeting just as pressing enter on the text
field does. The listener that is already
defined should also listen for the button push. That is, one
listener will listen for two different events -- it will respond
to either event by displaying a new greeting. All you need to do
is the following:
- Compile and run the applet. You will see that the components no
longer fit in the applet window (you may have noticed this earlier if
you typed in a long name.) Change the applet size -- try various
sizes until you find something that looks good.
- Test the program by both pressing enter on the text field and
by pressing the button. Either event should cause a change in the
message. Also try typing in a new name (but don't press enter),
then clicking on the button. You will see that the new name is
displayed (because the actionPerformed method gets the name from
the text field each time it is executed -- the new name is there
even though enter was never pressed).
- Adding Some Color Modify the applet so that it changes
colors when an action event occurs. Do the following:
- Currently the Container object cp is a local variable
in init. Move the declaration (but not the instantiation)
so it is an instance variable.
- Now, in actionPerformed, in each part of the switch statement
that chooses a greeting, add a statement to set the background
color of the content pane. (All Java components have
a method void setBackground (Color color) that changes the
-- see the JComponent
class on pages 846 and 847).
Use a different color for each of the
- Also, change the color of the button in each of the four
cases -- both the background
(the button itself) and the foreground (the label on the button).
Components have a setForeground method that is analogous to the
- Compile and test your program. Be sure your colors look good and
that the labels are visible. (NOTE: You can also change the color
of your labels, such as the output label that gives the greeting.)
Computing Body Mass Index
Body Mass Index (BMI) is measure of weight that takes height into account.
Generally, a BMI above 25 is considered high, that is, likely to indicate
that an individual is overweight. BMI is calculated as follows for both
men and women:
(703 * weight in pounds) / (height in inches)2
File BMI.java contains a skeleton for a GUI to
calculate BMI.The user enters his or her height and weight and presses
"Calculate BMI"; the program then displays
the user's BMI. Much of the framework has been done for you, but you will need
to fill in code.
(Note that this program is much like the applet version of the temperature
conversion program handed out in class. The application version of that
program is FahrenheitGUI.java in Listing 5.15 of the text.)
Do the following (follow the instructions and the comments in the program):
In the init method of the applet you need to do the following (put
your code for each immediately after the corresponding comment in the
- JLabels have already been created to identify the height and weight
textfields. You need to create JLabels for the output, and to hold the
actual BMI. (NOTE: You need to use the variables that have already
- Create JTextFields to hold the person's height in inches and weight in pounds.
- Create an appropriately labeled JButton for the user to press to calculate the BMI.
- Create a BMIListener and make it listen for the button to be pressed.
Note that the listener is added to the button, not
the textfields, so the BMI is computed only when the button is pressed,
not when the
user presses Enter on the textfields.
- Add the height label and textfield to the content pane
(the content pane has already been
created for you). Note that the components will appear on the content
pane in the order
in which you add them (left to right, top to bottom).
- Add the weight label and textfield to the content pane.
- Add the button to the content pane.
- Add the label identifying the result and the label holding the result to
the content pane.
In the actionPerformed method of BMIListener:
- Get the text from the height and weight textfields and store
it in the String variables provided.
- Use Integer.parseInt to convert the text to integer values, and store
them in the int values provided.
- Calculate the BMI from the height and weight values.
- Use Double.toString to convert the BMI to a string, and store the string in
the result label.
Compile the BMI applet and run it in the applet viewer. You may use the
html file BMI.html to run the applet.
- (Optional) Add code to the BMI applet
to let the user have some idea of what
their BMI means. You need to either modify the JLabel that displays
the BMI or add a new JLabel. Your program should include a message based
on the following: a BMI less than 19 indicates the person is underweight,
a BMI between 19 and 25 (inclusive) means a healthy weight, a BMI between
26 and 30 (inclusive) indicates the person is overweight, and a BMI over
30 indicates the person is very overweight.
Hand in printouts for VoteCounter.java, Greetings.java, and BMI.java.
Tar your directory and email the tar file to your instructor with a
subject of cpsc120 lab12