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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7036543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:24:31  来源:igfitidea点击:

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

javaswingword-wrapjtextpanestyleddocument

提问by A Coder

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. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.

我试图确切地了解它是如何工作的,以便我可以修改行为。现在,如果我在 JScrollPane 中有一个标准的 JTextPane,它会在空格处打断文本,但不会在长词中打断——如果有一串没有空格的文本比窗口宽,它不会换行/打断和将出现一个水平滚动条。随着文本宽度的增加,ParagraphView 的宽度(通过 getWidth())也会增加以容纳文本。

This article by Lapitskysays 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? Is it somehow returning BadBreakWeight instead of GoodBreakWeight? Or is there some layout problem? Or is there a bug?

Lapitsky 的这篇文章说 LabelView.getBreakWeight() 为带空格的标签返回 View.ExcellentBreakWeight 和为不带空格的标签返回 View.GoodBreakWeight(GlyphView.java 中的代码似乎证实了这一点),那么为什么它不会中断?它是否以某种方式返回 BadBreakWeight 而不是 GoodBreakWeight?还是布局有问题?还是有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. Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together? 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?)

Swing 的 javadocs 似乎没有详细说明某些对象(如 JTextPane、View 和相关对象)如何协同工作。是否有此类类的设计的任何进一步文档,可能详细说明每个类的目的以及它们如何协同工作?只是不公开吗?(或者我是唯一一个遇到此类问题的人吗?或者文档不足仅限于典型开发人员不会处理的事情?)

回答by StanislavL

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