Java Reference
In-Depth Information
class FileAction extends AbstractAction {
// Constructor
FileAction(String name) {
super(name);
}
// Constructor
FileAction(String name, KeyStroke keystroke) {
this(name);
if(keystroke != null)
putValue(ACCELERATOR _ KEY, keystroke);
}
// Event handler
public void actionPerformed(ActionEvent e) {
// We will add action code here eventually...
}
}
We have two constructors. The first just stores the name for the action by calling the base class constructor.
The second stores the name by calling the first constructor and then stores the accelerator keystroke using the
appropriate key if the argument is not null . Calling the other constructor rather than the base class
constructor is better here, in case we add code to the other constructor later on (as we shall!).
Since our class is an action listener, we can implement the actionPerformed() method in it. We
don't yet know what we are going to do with the File menu item actions, so we will leave it open for
now and let the actionPerformed() method do nothing. Add this inner class to SketchFrame
where the comment indicates.
The SketchFrame class will need a data member of type FileAction for each menu item we intend
to add, so add the following statement to the SketchFrame class definition:
// File actions
private FileAction newAction, openAction, closeAction,
saveAction, saveAsAction, printAction;
We can define an inner class for the element type menus next:
class TypeAction extends AbstractAction {
TypeAction(String name, int typeID) {
super(name);
this.typeID = typeID;
}
public void actionPerformed(ActionEvent e) {
elementType = typeID;
}
private int typeID;
}
Search WWH ::




Custom Search