Source of Telephone.java


  1: import java.awt.*;
  2: import java.awt.event.*;
  3: import javax.swing.*;

  5: /**
  6:    Presents a phone GUI for the voice mail system.
  7: */
  8: public class Telephone
  9: {
 10:    /**
 11:       Constructs a telephone with a speaker, keypad,
 12:       and microphone.
 13:    */
 14:    public Telephone()
 15:    {
 16:       JPanel speakerPanel = new JPanel();
 17:       speakerPanel.setLayout(new BorderLayout());
 18:       speakerPanel.add(new JLabel("Speaker:"),
 19:             BorderLayout.NORTH);
 20:       speakerField = new JTextArea(10, 25);
 21:       speakerPanel.add(speakerField,
 22:             BorderLayout.CENTER);

 24:       String keyLabels = "123456789*0#";
 25:       JPanel keyPanel = new JPanel();
 26:       keyPanel.setLayout(new GridLayout(4, 3));
 27:       for (int i = 0; i < keyLabels.length(); i++)
 28:       {
 29:          final String label = keyLabels.substring(i, i + 1);
 30:          JButton keyButton = new JButton(label);
 31:          keyPanel.add(keyButton);
 32:          keyButton.addActionListener(new
 33:             ActionListener()
 34:             {
 35:                public void actionPerformed(ActionEvent event)
 36:                {
 37:                   connect.dial(label);
 38:                }
 39:             });
 40:       }

 42:       final JTextArea microphoneField = new JTextArea(10,25);

 44:       JButton speechButton = new JButton("Send speech");
 45:       speechButton.addActionListener(new
 46:          ActionListener()
 47:          {
 48:             public void actionPerformed(ActionEvent event)
 49:             {
 50:                connect.record(microphoneField.getText());
 51:                microphoneField.setText("");
 52:             }
 53:          });

 55:       JButton hangupButton = new JButton("Hangup");
 56:       hangupButton.addActionListener(new
 57:          ActionListener()
 58:          {
 59:             public void actionPerformed(ActionEvent event)
 60:             {
 61:                connect.hangup();
 62:             }
 63:          });

 65:       JPanel buttonPanel = new JPanel();
 66:       buttonPanel.add(speechButton);
 67:       buttonPanel.add(hangupButton);

 69:       JPanel microphonePanel = new JPanel();
 70:       microphonePanel.setLayout(new BorderLayout());
 71:       microphonePanel.add(new JLabel("Microphone:"),
 72:             BorderLayout.NORTH);
 73:       microphonePanel.add(microphoneField, BorderLayout.CENTER);
 74:       microphonePanel.add(buttonPanel, BorderLayout.SOUTH);

 76:       JFrame frame = new JFrame();
 77:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 78:       frame.add(speakerPanel, BorderLayout.NORTH);
 79:       frame.add(keyPanel, BorderLayout.CENTER);
 80:       frame.add(microphonePanel, BorderLayout.SOUTH);

 82:       frame.pack();
 83:       frame.setVisible(true);
 84:    }

 86:    /**
 87:       Give instructions to the mail system user.
 88:    */
 89:    public void speak(String output)
 90:    {
 91:       speakerField.setText(output);
 92:    }

 94:    public void run(Connection c)
 95:    {
 96:       connect = c;
 97:    }

 99:    private JTextArea speakerField;
100:    private Connection connect;
101: }