Java Reference
In-Depth Information
public void actionPerformed(ActionEvent e) {
elementColor = color;
// This is temporary - just to show it works...
getContentPane().setBackground(color);
}
private Color color;
}
Directory "Sketcher 6 using Action objects"
You use the idea that you used in the listener class for the Color menu items in the previous implement-
ation of SketcherFrame to show when it works. You have a statement in the actionPerformed() method
that sets the background color of the content pane to the element color. When you click on a color menu
item, the background color of the content pane changes. You remove this code later.
Add the following statement to the SketcherFrame class for the color action members, following the
TypeAction fields:
// Element color actions
private ColorAction redAction, greenAction, blueAction, yellowAction;
private ColorAction[] colorActions;
// Color actions as an array
Directory "Sketcher 6 using Action objects"
You can try these action classes out now to reconstruct the menus in Sketcher.
TRY IT OUT: Actions in Action
To break up the code into more manageable chunks, you can add three helper methods to the Sketcher-
Frame class to initialize the Action objects for the menu items. Here's how you can create the File menu
actions:
private void createFileMenuActions() {
newAction = new FileAction("New", 'N', CTRL_DOWN_MASK);
openAction = new FileAction("Open", 'O', CTRL_DOWN_MASK);
closeAction = new FileAction("Close");
saveAction = new FileAction("Save", 'S', CTRL_DOWN_MASK);
saveAsAction = new FileAction("Save As...");
printAction = new FileAction("Print", 'P', CTRL_DOWN_MASK);
Search WWH ::




Custom Search