Java Textarea ScrollPane [英] Java Textarea ScrollPane
问题描述
我创建了一个textarea,我需要在必要时将一个滚动条应用于textarea(当文本太长而且无法再读取时)。
I have created a textarea, and i need a scrollbar applied to the textarea when necessary (when the text gets too long down and it cant be read anymore).
这是我写的代码,但由于某种原因,滚动条真的没有出现?
this is the code i have written, but for some reason, the scrollbar doesnt really come up?
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 152, 456, 255);
textArea.setBorder(border);
textArea.setLineWrap(true);
sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel_1.add(textArea);
推荐答案
-
你由于使用
setBounds()
,必须删除使JTextArea
在屏幕上具有绝对大小的代码行。这使得不可调整大小,而JScrollPane
仅在其内容可调整大小时才有效。You have to remove the code line that makes the
JTextArea
have absolute size on the screen due to usingsetBounds()
. This makes it non-resizable, andJScrollPane
works only if its content is resizable.// wrong textArea.setBounds(10, 152, 456, 255);
-
请阅读 JTextArea 和 JScrollPane 教程;请从两个教程中运行示例。
Please read JTextArea and JScrollPane tutorial; please run examples from both tutorials.
这篇关于Java Textarea ScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!