简体   繁体   English

JTextPane 中的自动换行是如何实现的,我如何让它包装一个没有空格的字符串?

[英]How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

How exactly is word-wrapping implemented in JTextPane? JTextPane 中的自动换行究竟是如何实现的?

I'm trying to understand exactly how it works so that I can modify the behavior.我试图准确地了解它是如何工作的,以便我可以修改行为。 Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear.现在,如果我在 JScrollPane 中有一个标准的 JTextPane,它会在空格处打断文本,但不会在长词内打断 - 如果有一串没有空格的文本比 window 宽,它不会换行/打断和将出现一个水平滚动条。 As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.随着文本宽度的增加,ParagraphView 的宽度(通过 getWidth())增加以容纳文本。

This article by Lapitsky says that LabelView.getBreakWeight() returns View.ExcellentBreakWeight for labels with spaces and View.GoodBreakWeight for labels without spaces (and the code in GlyphView.java seems to confirm this), so why doesn't it break? Lapitsky 的这篇文章说 LabelView.getBreakWeight() 返回 View.ExcellentBreakWeight 用于带有空格的标签,而 View.GoodBreakWeight 用于不带空格的标签(并且 GlyphView.java 中的代码似乎证实了这一点),那么它为什么不中断呢? Is it somehow returning BadBreakWeight instead of GoodBreakWeight?它是否以某种方式返回 BadBreakWeight 而不是 GoodBreakWeight? Or is there some layout problem?还是布局有问题? Or is there a bug?还是有错误?

Here's some code (for your viewing pleasure):这是一些代码(为了您的观看乐趣):

//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);

Note that it still doesn't wrap if I take out the scroll pane and just use the text pane (it just gets clipped as it goes outside the window).请注意,如果我取出滚动窗格并仅使用文本窗格,它仍然不会换行(它只是在超出窗口时被剪裁)。


The javadocs for Swing don't seem to go into enough detail on how some objects (like JTextPane, View's, and related objects) work together. Swing 的 javadocs 似乎对 go 没有足够详细地说明某些对象(如 JTextPane、View 和相关对象)如何协同工作。 Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together?是否有任何关于此类设计的进一步文档,也许详细说明每个 class 的目的以及它们如何协同工作? Is it just not publicly available?它只是不公开吗? (Or am I the only one having trouble with things like this? Or is the insufficient documentation limited to things which aren't expected to be dealt with by a typical developer?) (或者我是唯一一个遇到此类问题的人吗?或者文档不足是否仅限于典型开发人员不会处理的事情?)

The link about custom wrap (forced wrap and no wrap).关于自定义换行(强制换行和不换行)的链接。 http://java-sl.com/wrap.html http://java-sl.com/wrap.html

The link about letter wrap http://java-sl.com/tip_html_letter_wrap.html关于字母换行 http://java-sl.com/tip_html_letter_wrap.html的链接

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

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