简体   繁体   English

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

[英]How to make JTextArea scrollable to user input?

I have been working on a java application since a few days (I am a novice). 几天以来我一直在研究Java应用程序(我是新手)。 I have to make use of JTextArea for user input(250 characters). 我必须使用JTextArea进行用户输入(250个字符)。 I have used setPreferredSize() method to resize my JTextArea . 我已经使用setPreferredSize()方法来调整JTextArea大小。 However, as the user types, when the inserted words exceed the JTextArea height, I cannot see the rest of the input though the user types. 但是,随着用户的键入,当插入的单词超过JTextArea高度时,尽管用户键入,但我看不到其余的输入。

Is there a way to solve this problem? 有办法解决这个问题吗?

Please post some code so we can see what you did already. 请发布一些代码,以便我们可以看到您已经做了什么。 What you can try is add a JScrollPane : 您可以尝试添加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

Please follow 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();
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM