且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态调整 textarea 的大小以适应内容

更新时间:2023-02-09 18:54:22

除了原型设计之外,我不会将 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