Java Reference
In-Depth Information
app. object reference
setJMenuBar(menuBar); // Add the
menu bar to the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // Default
is exit the application
createFileMenu(); // Create
the File menu
createElementMenu(); // Create
the element menu
createColorMenu(); // Create
the element menu
JMenu optionsMenu = new JMenu("Options"); // Create
options menu
optionsMenu.setMnemonic('O');
// Create
shortcut
menuBar.add(optionsMenu);
// Add options
to menu bar
// Add the font choice item to the options menu
fontItem = new JMenuItem("Choose font...");
fontItem.addActionListener(this);
optionsMenu.add(fontItem);
fontDlg = new FontDialog(this);
// Create the
font dialog
// Rest of the constructor code as before...
}
Directory "Sketcher 5 displaying a font dialog"
You create the
FontDialog
object in the
SketcherFrame
constructor. You are able to reuse the
FontDia-
log
object as often as you want. When you need to display it, you simply call its
setVisible()
method.
The
actionPerformed()
method in the
SketcherFrame
class can handle the events for the new menu
item:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == aboutItem) {
// Create about dialog with the app window as parent
JOptionPane.showMessageDialog(this, //
Parent
"Sketcher Copyright Ivor Horton 2011", //
Message
"About Sketcher", // Title
JOptionPane.INFORMATION_MESSAGE); //
Message type
} else if(e.getSource() == fontItem) {
// Set the
dialog window position