简体   繁体   English

JTextArea的JScrollPane无法正常工作; 不会滚动

[英]The JScrollPane of the JTextArea is not working; Won't Scroll

I am a novice programmer in Java Swing. 我是Java Swing的新手程序员。 I am trying to make a JScrollPane with the JTextarea so that the text will scroll. 我试图用JTextarea制作一个JScrollPane ,以便文本滚动。 But the scroll is not working. 但是滚动不起作用。 Please help me with this. 请帮我解决一下这个。 I don't know what I am doing wrong. 我不知道我在做什么错。 I am providing my code snippet here. 我在这里提供我的代码段。

JTextArea ta = new JTextArea("Hello.\n \n \n \n \n There.", 5, 10);
ta.setBounds(250, 60, 120, 60);

JScrollPane jp = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setBounds(250, 60, 140, 80);
jp.setPreferredSize(new Dimension(150, 150));

//jp.setViewportView(ta);
ta.setLineWrap(true);
panel.add(ta);
panel.add(jp);

By removing the below line: 通过删除以下行:

panel.add(ta);

This will solve your problem since you are already adding the JTextArea to the JScrollPane (Also the other comments are worth looking at :) ) 这将解决您的问题,因为您已经将JTextArea添加到JScrollPane (其他注释也值得一看:))

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

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