📄 telephone.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
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(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect.dial(label);
}
});
}
final JTextArea microphoneField = new JTextArea(10, 25);
JButton speechButton = new JButton("Send speech");
speechButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect.record(microphoneField.getText());
microphoneField.setText("");
}
});
JButton hangupButton = new JButton("Hangup");
hangupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent 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);
Container contentPane = frame.getContentPane();
contentPane.add(speakerPanel,
BorderLayout.NORTH);
contentPane.add(keyPanel,
BorderLayout.CENTER);
contentPane.add(microphonePanel,
BorderLayout.SOUTH);
frame.pack();
frame.show();
}
/**
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -