Source of MemoSaver.java


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