    001: import java.awt.*;
    002: import java.awt.event.*;
    003: import javax.swing.*;
    004:
    005: /**
    006:    Presents a phone GUI for the voice mail system.
    007: */
    008: public class Telephone
    009: {
    010:    /**
    011:       Constructs a telephone with a speaker, keypad,
    012:       and microphone.
    013:    */
    014:    public Telephone()
    015:    {
    016:
    017:       JPanel speakerPanel = new JPanel();
    018:       speakerPanel.setLayout(new BorderLayout());
    019:       speakerPanel.add(new JLabel("Speaker:"),
    020:          BorderLayout.NORTH);
    021:       speakerField = new JTextArea(10, 25);
    022:       speakerPanel.add(speakerField,
    023:          BorderLayout.CENTER);
    024:
    025:       String keyLabels = "123456789*0#";
    026:       JPanel keyPanel = new JPanel();
    027:       keyPanel.setLayout(new GridLayout(4, 3));
    028:       for (int i = 0; i < keyLabels.length(); i++)
    029:       {
    030:          final String label = keyLabels.substring(i, i + 1);
    031:          JButton keyButton = new JButton(label);
    032:          keyPanel.add(keyButton);
    033:          keyButton.addActionListener(new
    034:             ActionListener()
    035:             {
    036:                public void actionPerformed(ActionEvent event)
    037:                {
    038:                   connect.dial(label);
    039:                }
    040:             });
    041:       }
    042:
    043:       final JTextArea microphoneField = new JTextArea(10, 25);
    044:
    045:       JButton speechButton = new JButton("Send speech");
    046:       speechButton.addActionListener(new
    047:          ActionListener()
    048:          {
    049:             public void actionPerformed(ActionEvent event)
    050:             {
    051:                connect.record(microphoneField.getText());
    052:                microphoneField.setText("");
    053:             }
    054:          });
    055:
    056:       JButton hangupButton = new JButton("Hangup");
    057:       hangupButton.addActionListener(new
    058:          ActionListener()
    059:          {
    060:             public void actionPerformed(ActionEvent event)
    061:             {
    062:                connect.hangup();
    063:             }
    064:          });
    065:
    066:       JPanel buttonPanel = new JPanel();
    067:       buttonPanel.add(speechButton);
    068:       buttonPanel.add(hangupButton);
    069:
    070:       JPanel microphonePanel = new JPanel();
    071:       microphonePanel.setLayout(new BorderLayout());
    072:       microphonePanel.add(new JLabel("Microphone:"),
    073:          BorderLayout.NORTH);
    074:       microphonePanel.add(microphoneField,
    075:          BorderLayout.CENTER);
    076:       microphonePanel.add(buttonPanel,
    077:          BorderLayout.SOUTH);
    078:
    079:       JFrame frame = new JFrame();
    080:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    081:       Container contentPane = frame.getContentPane();
    082:       contentPane.add(speakerPanel,
    083:          BorderLayout.NORTH);
    084:       contentPane.add(keyPanel,
    085:          BorderLayout.CENTER);
    086:       contentPane.add(microphonePanel,
    087:          BorderLayout.SOUTH);
    088:
    089:       frame.pack();
    090:       frame.show();
    091:    }
    092:
    093:    /**
    094:       Give instructions to the mail system user.
    095:    */
    096:    public void speak(String output)
    097:    {
    098:       speakerField.setText(output);
    099:    }
    100:
    101:    public void run(Connection c)
    102:    {
    103:       connect = c;
    104:    }
    105:
    106:    private JTextArea speakerField;
    107:    private Connection connect;
    108: }