简体   繁体   English

如何防止JTextPane水平增长…?

[英]How can I prevent JTextPane to grow horizontally…?

I am building an application with several tabs containing JPanels with JTextPanes. 我正在构建一个带有几个带有JPanel和JTextPanes的选项卡的应用程序。 While testing, I wrote text longer than the screen, and the JTextPane grows horizontally. 在测试时,我写的文本比屏幕长,并且JTextPane水平增长。 Does somebody know a way to stop it from growing like that? 有人知道阻止它那样增长的方法吗? Also, how can I force that the text, once the line has been completed, jumps to the next line on the JTextPane? 另外,一旦行完成,如何强制文本跳到JTextPane的下一行?

Thanks! 谢谢!

UPDATE: I opted for the first option that Julien Gauthier mentioned, which is using JTextArea and enabling the Wrapping. 更新:我选择了Julien Gauthier提到的第一个选项,它使用JTextArea并启用Wrapping。 It worked neatly. 它工作整齐。 Given that the size of my text boxes (or areas) is more than enough for the limit I established as text capture (800 characters), I did not need to add Scrolls to the text areas. 鉴于我的文本框(或区域)的大小足以满足我作为文本捕获所建立的限制(800个字符),因此无需在文本区域中添加滚动。 And also, I did not had any trouble with my save-text-to-file functions with JTextAreas instead of JTextPanes. 而且,使用JTextAreas(而不是JTextPanes)使用保存文本到文件的功能时,我也没有遇到任何麻烦。 Thanks a lot for the help! 非常感谢您的帮助!

I wrote text longer than the screen, and the JTextPane grows horizontally. 我写的文本比屏幕长,并且JTextPane水平增长。 Does somebody know a way to stop it from growing like that? 有人知道阻止它那样增长的方法吗?

Add the text pane to a JScrollPane and then add the scroll pane to the frame. 将文本窗格添加到JScrollPane,然后将滚动窗格添加到框架。 Then the text will wrap and scrollbars will appear when required. 然后,文字将自动换行,并在需要时显示滚动条。

Read the section from the Swing tutorial on Text Component Features for a working example of a text pane. 阅读Swing教程中有关文本组件功能的部分,以获取文本窗格的工作示例。

You have several possibilities : 您有几种可能性:

  • You can first replace the JTextpane by a JTextArea, and use jTextArea.setLineWrap(boolea wrap). 您可以先用JTextArea替换JTextpane,然后使用jTextArea.setLineWrap(boolea wrap)。

  • You can use an editorKit with your JTextPane, wich will wrap the text. 您可以在JTextPane中使用editorKit,这将包装文本。 Please look this example : http://java-sl.com/tip_html_letter_wrap.html 请查看以下示例: http : //java-sl.com/tip_html_letter_wrap.html

  • You can use a JScrollPane, but the result will be ugly with long sentences. 您可以使用JScrollPane,但是长句子会使结果难看。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM