Java中JTextArea的自动动态扩展/收缩
我首先创建一个特定大小的 JTextArea。用户可以在其中添加文本,但如果文本变得太长(垂直或水平),它将被切断。我希望 JTextArea 自动扩展或收缩(用于删除文本)。
我将来可能允许用户更改字体和字体大小,因此如果我可以避免使内容增加/减少一定的大小,那就太好了。
我目前正在使用边界来调整我的 JTextArea 的大小。也许我应该按行和列调整大小并使用侦听器并采取适当的行动?
提前致谢!
I start off by creating a JTextArea of a specific size. The user can add text within it but it will get cut off if it becomes too long (vertically or horizontally). I want the JTextArea to automatically expand or contract (for deletion of text).
I may allow users to change font and font size in the future, so it would be good if I could avoid making things increase/decrease by a certain size.
I am currently using bounds to size my JTextArea. Perhaps I should size by rows and columns and use a listener and act appropriately?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法想象你为什么要这样做,为什么不把 JTextArea 放在 JScrollPane 中,但是好吧,我会一起玩......也许是这样的:
I can't imagine why you'd want to do this, why not put a JTextArea in a JScrollPane, but ok, i'll play along... Maybe something like this:
我赞同简单地将 JTextArea 放在 JScrollPane 中并让它处理额外文本的建议。另外,也许最重要的是,不要设置 JTextArea 的边界,因为如果这样做,您会将其限制为一定的大小,而这不是您希望发生的情况。相反,使用两个 int 常量来初始化 JTextArea,以表示应可视化的行数和列数,然后将其放入 JScrollPane 中。另外,请务必阅读有关使用布局管理器的信息,这样您就可以避免设置 JScrollPane 的大小!
编辑:在测试中,似乎 setPreferredSize 对 JTextArea 比 setSize 更危险。
I second the advice to simply put the JTextArea in a JScrollPane and let this take care of extra text. Also and perhaps most importantly, don't set the bounds of the JTextArea because if you do this, you constrain it to be a certain size and that's not what you want to have happen. Instead initialize your JTextArea with two int constants to represent the number of rows and columns that should be visualized and then place it in a JScrollPane. Also be sure to read up on using the layout managers so you can avoid setting the size of your JScrollPane too!
Edit: on testing, it seems that setPreferredSize is more dangerous to a JTextArea than setSize.
另请参阅
GrowingTextAreaExample
also, see
GrowingTextAreaExample