gpt4 book ai didi

java - 添加滚动面板使文本区域消失

转载 作者:行者123 更新时间:2023-12-01 11:41:36 24 4
gpt4 key购买 nike

我有一个电话模拟程序,其中包含一个框架,该框架具有扬声器文本区域、按钮面板和麦克风文本区域。我正在尝试向两个文本区域添加垂直滚动条。但是当我这样做时,两个文本区域似乎都会折叠并消失。谁能解释一下问题是什么?这是我的电话类(class):

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;
}

}

最佳答案

scrollSpeaker = new JScrollPane(speakerField);
speakerPanel.add(speakerField, BorderLayout.CENTER);
speakerPanel.add(scrollSpeaker);

一个组件只能有一个父组件。

在您的代码中,您创建了带有文本字段的滚动 Pane ,这很好。

但是你

  1. 将文本区域添加到面板,这会将其从滚动 Pane 中删除
  2. 将滚动 Pane 添加到面板中,但滚动 Pane 中没有任何内容

您所需要的是:

scrollSpeaker = new JScrollPane(speakerField);
speakerPanel.add(scrollSpeaker);

关于java - 添加滚动面板使文本区域消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29478040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com