Source of MemoSaver.java


  1: 
  2: import javax.swing.*;
  3: import java.awt.*;
  4: import java.awt.event.*;
  5: 
  6: public class MemoSaver extends JFrame implements ActionListener
  7: {
  8:     public static final int WIDTH = 600;
  9:     public static final int HEIGHT = 300;
 10:     public static final int LINES = 10;
 11:     public static final int CHAR_PER_LINE = 40;
 12: 
 13:     private JTextArea theText;
 14:     private String memo1 = "No Memo 1.";
 15:     private String memo2 = "No Memo 2.";
 16: 
 17:     public MemoSaver( )
 18:     {
 19:         setSize(WIDTH, HEIGHT);
 20:         addWindowListener(new WindowDestroyer( ));
 21:         setTitle("Memo Saver");
 22:         Container contentPane = getContentPane( );
 23:         contentPane.setLayout(new BorderLayout( ));
 24: 
 25:         JPanel buttonPanel = new JPanel( );
 26:         buttonPanel.setBackground(Color.WHITE);
 27:         buttonPanel.setLayout(new FlowLayout( ));
 28:         JButton memo1Button = new JButton("Save Memo 1");
 29:         memo1Button.addActionListener(this);
 30:         buttonPanel.add(memo1Button);
 31:         JButton memo2Button = new JButton("Save Memo 2");
 32:         memo2Button.addActionListener(this);
 33:         buttonPanel.add(memo2Button);
 34:         JButton clearButton = new JButton("Clear");
 35:         clearButton.addActionListener(this);
 36:         buttonPanel.add(clearButton);
 37:         JButton get1Button = new JButton("Get Memo 1");
 38:         get1Button.addActionListener(this);
 39:         buttonPanel.add(get1Button);
 40:         JButton get2Button = new JButton("Get Memo 2");
 41:         get2Button.addActionListener(this);
 42:         buttonPanel.add(get2Button);
 43:         contentPane.add(buttonPanel, BorderLayout.SOUTH);
 44: 
 45:         JPanel textPanel = new JPanel( );
 46:         textPanel.setBackground(Color.BLUE);
 47:         theText = new JTextArea(LINES, CHAR_PER_LINE);
 48:         theText.setBackground(Color.WHITE);
 49:         textPanel.add(theText);
 50:         contentPane.add(textPanel, BorderLayout.CENTER);
 51:     }
 52: 
 53:     public void actionPerformed(ActionEvent e)
 54:     {
 55:         String actionCommand = e.getActionCommand( );
 56:         if (actionCommand.equals("Save Memo 1"))
 57:             memo1 = theText.getText( );
 58:         else if (actionCommand.equals("Save Memo 2"))
 59:             memo2 = theText.getText( );
 60:         else if (actionCommand.equals("Clear"))
 61:             theText.setText("");
 62:         else if (actionCommand.equals("Get Memo 1"))
 63:             theText.setText(memo1);
 64:         else if (actionCommand.equals("Get Memo 2"))
 65:             theText.setText(memo2);
 66:         else
 67:             theText.setText("Error in memo interface");
 68:      }
 69: 
 70:     public static void main(String[] args)
 71:     {
 72:         MemoSaver guiMemo = new MemoSaver( );
 73:         guiMemo.setVisible(true);
 74:     }
 75: }