Source of ScrollBarDemo.java


  1: import javax.swing.*;
  2: 
  3: import java.awt.*;
  4: import java.awt.event.*;
  5: 
  6: public class ScrollBarDemo 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 ScrollBarDemo( )
 18:     {
 19:         setSize(WIDTH, HEIGHT);
 20:         addWindowListener(new WindowDestroyer( ));
 21:         setTitle("Scrolling 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:         JScrollPane scrolledText = new JScrollPane(theText);
 50:         scrolledText.setHorizontalScrollBarPolicy(
 51:                     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 52:         scrolledText.setVerticalScrollBarPolicy(
 53:                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 54:         textPanel.add(scrolledText);
 55:         contentPane.add(textPanel, BorderLayout.CENTER);
 56:     }
 57: 
 58:     public void actionPerformed(ActionEvent e)
 59:     {
 60:         String actionCommand = e.getActionCommand( );
 61:         if (actionCommand.equals("Save Memo 1"))
 62:             memo1 = theText.getText( );
 63:         else if (actionCommand.equals("Save Memo 2"))
 64:             memo2 = theText.getText( );
 65:         else if (actionCommand.equals("Clear"))
 66:             theText.setText("");
 67:         else if (actionCommand.equals("Get Memo 1"))
 68:             theText.setText(memo1);
 69:         else if (actionCommand.equals("Get Memo 2"))
 70:             theText.setText(memo2);
 71:         else
 72:             theText.setText("Error in memo interface");
 73:     }
 74: 
 75:     public static void main(String[] args)
 76:     {
 77:         ScrollBarDemo guiMemo = new ScrollBarDemo( );
 78:         guiMemo.setVisible(true);
 79:     }
 80: }