java 自动调整 JTextArea 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2601074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-Resizing JTextArea
提问by Halo
I want my JTextArea to resize itself (expand vertically) when the last line (that the text area's height can offer) is reached and the user wants to start a new line. You know, like the textbox in MSWord.
当到达最后一行(文本区域的高度可以提供)并且用户想要开始新行时,我希望我的 JTextArea 调整自身大小(垂直扩展)。您知道,就像 MSWord 中的文本框一样。
I have an idea to use getLineCount()and determine (if necessary) the new height of the JTextArea. Do you have, or know of better approaches for implementing this?
我有一个想法来使用getLineCount()和确定(如有必要)JTextArea 的新高度。你有或知道更好的方法来实现这个吗?
回答by Aaron Digulla
Actually, the JTextAreaalways has the correct size so all lines of text are visible. What you experience is probably that you wrapped the text area in a JScrollPane. Just omit the scroll pane and make the text area a direct child of the container.
实际上,JTextArea始终具有正确的大小,因此所有文本行都可见。您所经历的可能是您将文本区域包裹在JScrollPane. 只需省略滚动窗格并使文本区域成为容器的直接子项。
Another solution is to listen to resize events of the text area and size the scroll pane accordingly. This way, you can grow to a certain size and then start to display scroll bars (for example, when someone pastes 500KB of text into the text area).
另一种解决方案是侦听文本区域的调整大小事件并相应地调整滚动窗格的大小。这样,您可以增长到一定大小,然后开始显示滚动条(例如,当有人将 500KB 的文本粘贴到文本区域时)。
回答by Hillary Ryan
I had the same problem. From my tests, I do not believe that the JTextArea sets its size dynamically. Instead, its size seems to be limited by its container (a JPanel in my case). However, the JTextArea does change its preferred size based on the text it contains. From the documentation:
我有同样的问题。从我的测试来看,我不相信 JTextArea 会动态设置其大小。相反,它的大小似乎受限于它的容器(在我的例子中是一个 JPanel)。但是,JTextArea 确实会根据它包含的文本更改其首选大小。从文档:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
java.awt.TextArea 有两个属性行和列,用于确定首选大小。当 JTextArea 放置在 JScrollPane 中以匹配 java.awt.TextArea 提供的功能时,它使用这些属性来指示视口的首选大小。JTextArea 具有显示所有文本所需的首选大小,以便它在 JScrollPane 内正常运行。如果行或列的值为零,则沿该轴的首选大小用于沿同一轴的视口首选大小。
回答by MrVPlusOne
I had the same problem,I put the JTextArea into a JScrollPane and set the preferred size of JTextArea, and I believe that's the cause of the problem.
我遇到了同样的问题,我将 JTextArea 放入 JScrollPane 并设置了 JTextArea 的首选大小,我相信这就是问题的原因。
So the right solution is to put the JTextArea into a JScrollPane, and don't touch the preferred size of JTextArea, set JScrollPane's instead.
所以正确的解决方案是将 JTextArea 放入 JScrollPane 中,并且不要触摸 JTextArea 的首选大小,而是设置 JScrollPane 的大小。
回答by Arvy Dharmmaparayaandhika
Go to JTextArea "Properties" - checklist "lineWrap".
转到 JTextArea“属性”-清单“ lineWrap”。

