Java Reference
In-Depth Information
•
A
panel
is a container object that is used to group components inside of a larger
container. Panels are objects in the class
JPanel
.
•
A
menu item
is a choice on a menu. A menu item is realized in your code as an object
of the class
JMenuItem
. A
menu
is an object of the class
JMenu
. A menu item is added
to a
JMenu
with the method
add
. A
menu bar
is an object of the class
JMenuBar
. A
menu is added to a
JMenuBar
with the method
add
.
•
A
JMenuBar
can be added to a
JFrame
with the method
setJMenuBar
. It can also be
added using the method
add
, just as any other component can be added.
•
Both buttons and menu items fire action events and so normally have one or more
action listeners registered with them to respond to the events.
Answers to Self-Test Exercises
1. The
JFrame
class.
2. Sizes in Swing are measured in pixels.
3.
someWindow.setDefaultCloseOperation(
JFrame.DO_NOTHING_ON_CLOSE);
4.
someWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5. When you click the minimizing button, the
JFrame
is reduced to an icon, usually
at the bottom of your monitor screen.
6 .
someWindow.setVisible(n > 0);
The following also works but is not good style:
if
(n > 0)
someWindow.setVisible(
true
);
else
someWindow.setVisible(
false
);
7. An action event.
8.
public void
actionPerformed(ActionEvent e)
9. Change
JFrame firstWindow =
new
JFrame();
to
JFrame firstWindow =
new
JFrame("My First Window");
Alternatively, you can add the following:
firstWindow.setTitle("My First Window");