Source of NestedMenus.java


  1: import javax.swing.*;
  2: import java.awt.*;
  3: import java.awt.event.*;
  4: 
  5: public class NestedMenus extends JFrame implements ActionListener
  6: {
  7:     public static final int WIDTH = 600;
  8:     public static final int HEIGHT = 300;
  9:     public static final int LINES = 10;
 10:     public static final int CHAR_PER_LINE = 40;
 11: 
 12:     private JTextArea theText;
 13:     private String memo1 = "No Memo 1.";
 14:     private String memo2 = "No Memo 2.";
 15: 
 16:     public NestedMenus()
 17:     {
 18:         setSize(WIDTH, HEIGHT);
 19:         addWindowListener(new WindowDestroyer());
 20:         setTitle("Memo Saver");
 21:         Container contentPane = getContentPane();
 22:         contentPane.setLayout(new BorderLayout());
 23: 
 24:         JMenu memoMenu = new JMenu("Memos");
 25:         JMenuItem m;
 26: 
 27:         JMenu saveMenu = new JMenu("Save");
 28: 
 29:         m = new JMenuItem("Save Memo 1");
 30:         m.addActionListener(this);
 31:         saveMenu.add(m);
 32: 
 33:         m = new JMenuItem("Save Memo 2");
 34:         m.addActionListener(this);
 35:         saveMenu.add(m);
 36: 
 37:         memoMenu.add(saveMenu);
 38: 
 39:         JMenu getMenu = new JMenu("Get");
 40: 
 41:         m = new JMenuItem("Get Memo 1");
 42:         m.addActionListener(this);
 43:         getMenu.add(m);
 44: 
 45:         m = new JMenuItem("Get Memo 2");
 46:         m.addActionListener(this);
 47:         getMenu.add(m);
 48: 
 49:         memoMenu.add(getMenu);
 50: 
 51:         m = new JMenuItem("Clear");
 52:         m.addActionListener(this);
 53:         memoMenu.add(m);
 54: 
 55:         m = new JMenuItem("Exit");
 56:         m.addActionListener(this);
 57:         memoMenu.add(m);
 58: 
 59:         JMenuBar mBar = new JMenuBar();
 60:         mBar.add(memoMenu);
 61:         setJMenuBar(mBar);
 62: 
 63:         JPanel textPanel = new JPanel();
 64:         textPanel.setBackground(Color.BLUE);
 65:         theText = new JTextArea(LINES, CHAR_PER_LINE);
 66:         theText.setBackground(Color.WHITE);
 67:         textPanel.add(theText);
 68:         contentPane.add(textPanel, BorderLayout.CENTER);
 69:     }
 70: 
 71:     public void actionPerformed(ActionEvent e)
 72:     {
 73:         String actionCommand = e.getActionCommand();
 74:         if (actionCommand.equals("Save Memo 1"))
 75:             memo1 = theText.getText();
 76:         else if (actionCommand.equals("Save Memo 2"))
 77:             memo2 = theText.getText();
 78:         else if (actionCommand.equals("Clear"))
 79:             theText.setText("");
 80:         else if (actionCommand.equals("Get Memo 1"))
 81:             theText.setText(memo1);
 82:         else if (actionCommand.equals("Get Memo 2"))
 83:             theText.setText(memo2);
 84:         else if (actionCommand.equals("Exit"))
 85:             System.exit(0);
 86:         else
 87:             theText.setText("Error in memo interface");
 88:     }
 89: 
 90:     public static void main(String[] args)
 91:     {
 92:         NestedMenus gui = new NestedMenus();
 93:         gui.setVisible(true);
 94:     }
 95: }