01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
 | 
package horstmann.ch05_mailgui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
   Presents a phone GUI for the voice mail system.
 */
public class Telephone
{
  /**
      Constructs a telephone with a speaker, keypad,
      and microphone.
   */
  public Telephone()
  {
    JPanel speakerPanel = new JPanel();
    speakerPanel.setLayout(new BorderLayout());
    speakerPanel.add(new JLabel("Speaker:"),
        BorderLayout.NORTH);
    speakerField = new JTextArea(10, 25);
    speakerPanel.add(speakerField,
        BorderLayout.CENTER);
    String keyLabels = "123456789*0#";
    JPanel keyPanel = new JPanel();
    keyPanel.setLayout(new GridLayout(4, 3));
    for (int i = 0; i < keyLabels.length(); i++)
    {
      final String label = keyLabels.substring(i, i + 1);
      JButton keyButton = new JButton(label);
      keyPanel.add(keyButton);
      keyButton.addActionListener(event -> connect.dial(label));
    }
    final JTextArea microphoneField = new JTextArea(10,25);
    JButton speechButton = new JButton("Send speech");
    speechButton.addActionListener(event -> {
      connect.record(microphoneField.getText());
      microphoneField.setText("");
    });
    JButton hangupButton = new JButton("Hangup");
    hangupButton.addActionListener(event -> connect.hangup());
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(speechButton);
    buttonPanel.add(hangupButton);
    JPanel microphonePanel = new JPanel();
    microphonePanel.setLayout(new BorderLayout());
    microphonePanel.add(new JLabel("Microphone:"),
        BorderLayout.NORTH);
    microphonePanel.add(microphoneField, BorderLayout.CENTER);
    microphonePanel.add(buttonPanel, BorderLayout.SOUTH);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(speakerPanel, BorderLayout.NORTH);
    frame.add(keyPanel, BorderLayout.CENTER);
    frame.add(microphonePanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
  }
  /**
      Give instructions to the mail system user.
   */
  public void speak(String output)
  {
    speakerField.setText(output);
  }
  public void run(Connection c)
  {
    connect = c;
  }
  private JTextArea speakerField;
  private Connection connect;
}
 |