Source of MenuAdd.java


  1: import javax.swing.*;
  2: import java.awt.*;
  3: import java.awt.event.*;
  4: 
  5: public class MenuAdd 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 MenuAdd()
 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:         m = new JMenuItem("Save Memo 1");
 28:         m.addActionListener(this);
 29:         memoMenu.add(m);
 30: 
 31:         m = new JMenuItem("Save Memo 2");
 32:         m.addActionListener(this);
 33:         memoMenu.add(m);
 34: 
 35:         m = new JMenuItem("Get Memo 1");
 36:         m.addActionListener(this);
 37:         memoMenu.add(m);
 38: 
 39:         m = new JMenuItem("Get Memo 2");
 40:         m.addActionListener(this);
 41:         memoMenu.add(m);
 42: 
 43:         m = new JMenuItem("Clear");
 44:         m.addActionListener(this);
 45:         memoMenu.add(m);
 46: 
 47:         m = new JMenuItem("Exit");
 48:         m.addActionListener(this);
 49:         memoMenu.add(m);
 50: 
 51:         JMenuBar mBar = new JMenuBar();
 52:         mBar.add(memoMenu);
 53:         contentPane.add(mBar, BorderLayout.SOUTH);
 54: 
 55:         JPanel textPanel = new JPanel();
 56:         textPanel.setBackground(Color.BLUE);
 57:         theText = new JTextArea(LINES, CHAR_PER_LINE);
 58:         theText.setText("Menu bar is at the bottom.");
 59:         theText.setBackground(Color.WHITE);
 60:         textPanel.add(theText);
 61:         contentPane.add(textPanel, BorderLayout.CENTER);
 62:     }
 63: 
 64:     public void actionPerformed(ActionEvent e)
 65:     {
 66:         String actionCommand = e.getActionCommand();
 67:         if (actionCommand.equals("Save Memo 1"))
 68:             memo1 = theText.getText();
 69:         else if (actionCommand.equals("Save Memo 2"))
 70:             memo2 = theText.getText();
 71:         else if (actionCommand.equals("Clear"))
 72:             theText.setText("");
 73:         else if (actionCommand.equals("Get Memo 1"))
 74:             theText.setText(memo1);
 75:         else if (actionCommand.equals("Get Memo 2"))
 76:             theText.setText(memo2);
 77:         else if (actionCommand.equals("Exit"))
 78:             System.exit(0);
 79:         else
 80:             theText.setText("Error in memo interface");
 81:     }
 82: 
 83:     public static void main(String[] args)
 84:     {
 85:         MenuAdd gui = new MenuAdd();
 86:         gui.setVisible(true);
 87:     }
 88: }