Scrollbars on JTextArea in a JScrollPane do not work

大兔子大兔子 提交于 2019-12-04 01:42:15

Set the preferred size of the scroll pane rather than the text area.

The others are right about the size. As an aside, consider starting on the Event Dispatch Thread (EDT):

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new TextAreaTest().setVisible(true);
        }
    });
}

Use this code

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

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!