Button b = new Button("Save");
add(b);
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu fm = new Menu("File");
mb.add(fm);
MenuItem mi = new MenuItem("Save");
fm.add(mi);
// Construct the ActionListener, and keep a reference to it.
ActionListener saver = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Saving your file...");
// In real life we would call the doSave() method
// in the main class, something like this:
// mainProg.doSave();
}
};
// Register the actionListener with the Button
b.addActionListener(saver);
// And now register the same actionListener with the MenuItem
mi.addActionListener(saver);
pack();