Java Reference
In-Depth Information
Self-Test Exercises
(continued)
28. If you want to change the action command for a
JButton
, use the method
setActionCommand
. What method do you use to change the action command
for a
JMenuItem
?
29. Is the following legal in Java?
JMenu aMenu =
new
JMenu();
...
JMenu aSubMenu =
new
JMenu();
...
aMenu.add(aSubMenu);
30. How many
JMenuBar
objects can you have in a
JFrame
?
31. A
JFrame
has a private instance variable of type
JMenuBar
. What is the name
of the mutator method to change the value of this instance variable?
32. Write code to create a new menu item named
aChoice
that has the label
"Exit".
33. Suppose you are defining a class called
MenuGUI
that is a derived class of the
class
JFrame
. Write code to add the menu item
mItem
to the menu
m.
Then
add
m
to the menu bar
mBar
, and then add the menu bar to the
JFrame
MenuGUI
. Assume that this all takes place inside a constructor for
MenuGUI
.
Also assume that everything has already been constructed with
new
, and that all
necessary listeners are registered. You just need to do the adding.
34.
★
How can you modify the program in Display 17.16 so that when the
Blue
menu item is clicked, all three colors are shown? The
Red
and
White
choices
remain the same. (Remember the menu items may be clicked in any order, so
the
Blue
menu item can be the first or second item clicked.)
★
35.
Rewrite the Swing GUI in Display 17.16 so that there is only one action
listener inner class. The inner class constructor will have two parameters, one
for a panel and one for a color.
17.5
Text Fields and Text Areas
Write your answers in the spaces provided.
COMMON INSTRUCTION FOR AN EXAMINATION
You have undoubtedly interacted with windowing systems that provide spaces for you
to enter text information such as your name, address, and credit card number. In this
section, we show you how to add these fields for text input and text output to your
Swing GUIs.