我有一个电话模拟程序,其中包含一个框架,该框架具有扬声器文本区域,按钮面板和麦克风文本区域。我正在尝试将垂直滚动条添加到两个文本区域。但是当我这样做时,两个文本区域似乎都会折叠并消失。谁能解释一下问题是什么? 这是我的电话课:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
Presents a phone GUI for the voice mail system. When multiple Telephones
are created, closing the last one will exit the program.
*/
public class Telephone
{
private JTextArea speakerField;
private JScrollPane scrollSpeaker;
private Connection connect;
private static int numberOfPhones = 0;
/**
Constructs a telephone with a speaker, keypad, and microphone.
*/
public Telephone()
{
numberOfPhones = numberOfPhones + 1;
JPanel speakerPanel = new JPanel();
speakerPanel.setLayout(new BorderLayout());
speakerPanel.add(new JLabel("Speaker:"),
BorderLayout.NORTH);
speakerField = new JTextArea(10, 25);
scrollSpeaker = new JScrollPane(speakerField);
scrollSpeaker.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
speakerPanel.add(speakerField,
BorderLayout.CENTER);
speakerPanel.add(scrollSpeaker);
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);
final JScrollPane scrollMic = new JScrollPane(microphoneField);
scrollMic.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
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);
microphonePanel.add(scrollMic);
final JFrame frame = new JFrame();
frame.add(speakerPanel,
BorderLayout.NORTH);
frame.add(keyPanel,
BorderLayout.CENTER);
frame.add(microphonePanel,
BorderLayout.SOUTH);
// Replace the default close operation with a window event listener.
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
if (numberOfPhones == 1)
System.exit(0);
else
{
numberOfPhones = numberOfPhones - 1;
frame.dispose();
}
}
});
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;
}
}
答案 0 :(得分:1)
scrollSpeaker = new JScrollPane(speakerField);
speakerPanel.add(speakerField, BorderLayout.CENTER);
speakerPanel.add(scrollSpeaker);
组件只能有一个父组件。
在您的代码中,您可以使用文本字段创建滚动窗格,这很好。
但是你呢
您只需要:
scrollSpeaker = new JScrollPane(speakerField);
speakerPanel.add(scrollSpeaker);