如何使 JTextArea 可滚动到用户输入?

How to make JTextArea scrollable to user input?

几天以来我一直在研究 java 应用程序(我是新手)。我必须使用 JTextArea 进行用户输入(250 个字符)。我已使用 setPreferredSize() 方法调整 JTextArea 的大小。但是,当用户键入时,当插入的单词超过 JTextArea 高度时,尽管用户键入,我也看不到输入的其余部分。

有没有办法解决这个问题?

请 post 一些代码,以便我们可以看到您已经做了什么。 您可以尝试添加一个 JScrollPane:

JFrame frame = new JFrame ("Frame");
JTextArea textArea = new JTextArea ("Textarea");

JScrollPane scroll = new JScrollPane (textArea, 
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

frame.add(scroll);//add Scrollbar to frame

请关注this link

import javax.swing.*;
public class ScrollingTextArea 
{
  JFrame f;
  JTextArea ta;
  JScrollPane scrolltxt;
  public ScrollingTextArea() 
    {
         // TODO Auto-generated constructor stub

          f=new JFrame();
          f.setLayout(null);
          f.setVisible(true);
          f.setSize(500,500);
          ta=new JTextArea();
          ta.setBounds(5,5,100,200);

          scrolltxt=new JScrollPane(ta);
          scrolltxt.setBounds(3,3,400,400);

          f.add(scrolltxt);

    }

     public static void main(String[] args) 
    {
        new ScrollingTextArea();
    }

}