Java Reference
In-Depth Information
Consider the
Visualization2
class from the
com.oozinoz.visualization
package.
This class provides a menu that lets the user save and restore visualizations of a simulated
Oozinoz factory. The application has a menu bar that includes, at present, just a file menu:
protected JMenuBar menuBar()
{
if (menuBar == null)
{
menuBar = new JMenuBar();
Menu fileMenu = new Menu("File");
menuBar.add(fileMenu());
}
return menuBar;
}
The file menu creates Save and Load menu items and registers listeners that wait for the user
to choose them. The listeners are instances of anonymous classes that implement the
actionPerformed()
method by calling the
save()
and
load()
methods of the
Visualization2
class:
protected JMenu fileMenu()
{
if (fileMenu == null)
{
fileMenu = new JMenu("File");
Font f = SwingFacade.getStandardFont();
fileMenu.setFont(f);
JMenuItem save = new JMenuItem("Save");
save.setFont(f);
fileMenu.add(save);
save.addActionListener
(
new ActionListener()
{
// challenge!
}
);
JMenuItem load = new JMenuItem("Load");
load.setFont(f);
fileMenu.add(load);
load.addActionListener
(
new ActionListener()
{
// challenge!
}
);
}
return fileMenu;
}