1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  
  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);
 23  
 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           String label = keyLabels.substring(i, i + 1);
 30           JButton keyButton = new JButton(label);
 31           keyPanel.add(keyButton);
 32           keyButton.addActionListener(event ->
 33              connect.dial(label));
 34        }
 35  
 36        JTextArea microphoneField = new JTextArea(10,25);
 37  
 38        JButton speechButton = new JButton("Send speech");
 39        speechButton.addActionListener(event ->
 40           {
 41              connect.record(microphoneField.getText());
 42              microphoneField.setText("");
 43           });
 44  
 45        JButton hangupButton = new JButton("Hangup");
 46        hangupButton.addActionListener(event ->
 47           connect.hangup());
 48  
 49        JPanel buttonPanel = new JPanel();
 50        buttonPanel.add(speechButton);
 51        buttonPanel.add(hangupButton);
 52  
 53        JPanel microphonePanel = new JPanel();
 54        microphonePanel.setLayout(new BorderLayout());
 55        microphonePanel.add(new JLabel("Microphone:"),
 56              BorderLayout.NORTH);
 57        microphonePanel.add(microphoneField, BorderLayout.CENTER);
 58        microphonePanel.add(buttonPanel, BorderLayout.SOUTH);
 59  
 60        JFrame frame = new JFrame();
 61        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 62        frame.add(speakerPanel, BorderLayout.NORTH);
 63        frame.add(keyPanel, BorderLayout.CENTER);
 64        frame.add(microphonePanel, BorderLayout.SOUTH);
 65  
 66        frame.pack();
 67        frame.setVisible(true);
 68     }
 69  
 70     /**
 71        Give instructions to the mail system user.
 72     */
 73     public void speak(String output)
 74     {
 75        speakerField.setText(output);
 76     }
 77  
 78     public void run(Connection c)
 79     {
 80        connect = c;
 81     }
 82  
 83     private JTextArea speakerField;
 84     private Connection connect;
 85  }