本文介绍了JTextPane换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

询问者说,在这个问题中,

事实并非如此.

scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

this.contentPane.add(scrollPane);

txtrFghj = new JTextPane();
txtrFghj.setBorder(null);
txtrFghj.setContentType("text/html");
txtrFghj.setText(content);

scrollPane.setViewportView(txtrFghj);

在给定的代码摘录中,content的内容根本不会自动换行,只是超出了窗口的可见大小.如果窗口不够大,则长句无法完全看到.

In the given code excerpt, content's content will not wrap at all, it simply exceedes the visible size of the window. Long sentences cannot be seen completely, if the window is not big enough.

如何实现换行?

我尝试过

txtrFghj.setSize(50,50);

但这完全不会改变任何行为.

but this does not change any behaviour at all.

推荐答案

感谢Guillaume,我弄清楚了,我花了三个小时才意识到:换行确实确实确实在这里工作,但是字符串中断无法正常工作,如线程所示在问题中被引用.

Thanks to Guillaume, I figured it out, it took me three hours to realize though: Line break very much indeed does work here, but string break is not working, as can be seen in the thread which was quoted in the question.

我的JTextPane内容如下所示:

<html>
   <body>
    <br>
    <font color="red">bla bla bla bla</font>\r\n
    <u>someVeeeeeeeeeeeeeryLooooongString__WithOUTanySpacesInBetweeeeeeeeeeeeeeeeeeeeeeeeen</u>
    <b>more text</b>
    // ........ a lot of more HTML
    Some funny SENTENCE which is longer than JTextPane.getSize().width usually allows. This sentence was NOT LINE WRAPPED which made me ask the question.
   </body>
</html>

现在,如果 VeeeeryLooongString 没有扩展 JTextPane的宽度,则该句子将被换行.一直以来,我都没有想到过TextPane对象中的长字符串以及它如何影响整个换行行为.

Now, the SENTENCE would have been line-wrapped, if the VeeeeryLooongString had not expanded the JTextPane's width.All the time, I hadn't thought of the long String within my TextPane Object and how it could have affected the overall line wrapping behaviour.

删除此千兆字符串为我解决了这个问题.

Deleting this giga-String solved the issue for me.

有关此主题的更多信息,请参见详细的.

For further information on this topic, see this detailed question.

现在,我将尝试在JTextPanes中启用字符串环绕,有关其更多信息,请参见.

Now I am going to try to enable String wrapping within JTextPanes, for which further information can again be found in the quoted thread.

编辑.:可以进行字母换行,但是换行符<br>似乎不再起作用.对于给定的问题,最简单的解决方法是以禁用水平滚动条并将其设置为AS_NEEDED:

Edit.: Letter-wrapping can be done, but it seems line-breaks <br> are not working anymore then. For the given question, the easiest workaround will be not to disable the horizontal scrollbar and set it to AS_NEEDED instead:

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

这篇关于JTextPane换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:49