GridLayout 导致我的组件消失

GridLayout is causing my components to disappear

我正在尝试创建一个 window,其中包含一个可滚动的 JTextArea 和一个位于其下方的 JTextField。我希望框架看起来像聊天 window;一,大的可滚动文本区域和单行文本框。我试过各种变体,但如果不使整个 window 可滚动,我就无法使文本区域可滚动。这非常烦人。我当前的迭代只在屏幕上绘制一个面板:

private void buildGUI() {

    Container chatClientContainer = getContentPane();
    chatClientContainer.setLayout(new BorderLayout());

    JPanel messagesReceivedPanel = new JPanel();
    messagesReceivedPanel.setLayout(new GridLayout(1, 1, 5, 5));
    JTextArea messagesReceived = new JTextArea("area");
    messagesReceivedPanel.add(messagesReceived);

    JPanel draftPanel = new JPanel();
    draftPanel.setLayout(new GridLayout(1, 1, 5, 5));
    JTextField draftMessage = new JTextField("field");
    draftPanel.add(draftMessage);

    chatClientContainer.add(new JScrollPane(messagesReceivedPanel));
    chatClientContainer.add(draftPanel);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int windowWidth = 400;
    int windowHeight = 600;
    int posX = ((int) screenSize.getWidth())/2 - windowWidth/2;
    int posY = (int) screenSize.getHeight()/2 - windowHeight/2;

    setBounds(posX, posY, windowWidth, windowHeight);
    setResizable(true);
    setVisible(true);
}

我怎样才能按照我想要的方式放置它?

为什么不直接使用 BorderLayout?放置 JTextArea 的 JScrollPane BorderLayout.CENTER 和 JTextField(不是 JTextFrame)BorderLayout.PAGE_END.

例如:

import java.awt.BorderLayout;

import javax.swing.*;

public class ChatPanel extends JPanel {
   private static final long serialVersionUID = 1L;
   private static final int ROWS = 15;
   private static final int COLS = 30;
   private JTextArea textArea = new JTextArea(ROWS, COLS);
   private JTextField textField = new JTextField(COLS);

   public ChatPanel() {
      JScrollPane scrollPane = new JScrollPane(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
      add(textField, BorderLayout.PAGE_END);
   }

   private static void createAndShowGUI() {
      ChatPanel paintEg = new ChatPanel();

      JFrame frame = new JFrame("ChatPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}