动态调整 textarea 的大小以适应内容 [英] Dynamically resize textarea to fit content

查看:61
本文介绍了动态调整 textarea 的大小以适应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用 netbeans 放入我的程序中的文本区域.第一张图片显示了我运行程序时 textarea 的样子.第二个图像是在我按下一个按钮后显示文本区域,该按钮将大约 50 个Hello world"字符串添加到文本区域 - 仅显示前 6 个.我需要 textarea 的高度动态增加以适应所有这些字符串.有人知道怎么做吗?

Here is a textarea I have put in my program using netbeans. The first image shows what the textarea looks like when I run the program. The second image is show the textarea after I press a button that adds about 50 "Hello world" strings to the text area - only the first 6 get shown. I need the textarea's height to dynamically increase to fit all these strings. Anyone know how to do it?

我不想要滚动条,所以不能选择使用 JScrollPane.

I dont want scrollbars so using a JScrollPane is not an option.

推荐答案

除了原型设计之外,我不会将 JTextArea 用于任何事情.它具有记事本的苦涩功能.但这取决于你.

I wouldn't use JTextArea for anything except prototyping. It has the bitter functionality of notepad. But it's up to you.

我会使用 JEditorPane 或 JTextPane.我知道你不能根据字符大小来调整它的大小,但这是最好的.对于自动换行,您可以执行 setContentType("text/html");并将文本包裹在 <'p><'/p> 标签中.

I would use a JEditorPane or JTextPane. I know you can't size it based on character size but that's for the best. For word wrap, you can do setContentType("text/html"); and wrap the text in <'p><'/p> tags.

(注意您仍然使用 JScrollPane 进行滚动.实际上,滚动窗格适用于任何组件)

(Note you still use JScrollPane for scrolling. In fact, the scroll pane works for any component)

查看差异:http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

这篇关于动态调整 textarea 的大小以适应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆