java JTextPane - 动态自动换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11300094/
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-31 04:35:01  来源:igfitidea点击:

JTextPane - dynamic word wrap

javaswingword-wrapjtextpane

提问by user592704

I tried to use jTextPane1.setText("xxx xxxx xxx xxxxx xx xxx xxxx xxx etc...");but JTextPanedoes not word wrap it at all showing all the text in one line only instead. It would be interesting to support word wrap on jTextPane1resized too...

我尝试使用jTextPane1.setText("xxx xxxx xxx xxxxx xx xxx xxxx xxx etc...");JTextPane根本没有自动换行,而是仅在一行中显示所有文本。支持jTextPane1调整大小的自动换行也会很有趣......

So my question is... how to make JTextPanesupport word wrap?

所以我的问题是...如何进行JTextPane支持自动换行?

采纳答案by David Kroukamp

Try using a JTextAreaand call setWrapStyleWord(true);on its instance this should do what you need.

尝试使用 aJTextArea并调用setWrapStyleWord(true);其实例,这应该可以满足您的需求。

EDIT:

编辑:

If you need to use a JTextPaneas a requirement(which you said you do), then have a look at a similar question that I found which answer should be of help: How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

如果您需要将 aJTextPane作为要求(您说过要这样做),请查看一个类似的问题,我发现哪个答案应该有帮助:JTextPane 中如何实现自动换行,以及如何实现包裹一个没有空格的字符串?

回答by xorissao

I had the same problem, the solution from David Kroukamp was very helpful. I changed it to JTextArea and set the following properties, as described in this tutorial:

我遇到了同样的问题,David Kroukamp 的解决方案非常有帮助。我将其更改为 JTextArea 并设置以下属性,如本教程中所述

    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

回答by Chad

Why not use a JTextArea instead of a Pane?

为什么不使用 JTextArea 而不是 Pane?

http://docs.oracle.com/javase/1.5.0/docs/api/

http://docs.oracle.com/javase/1.5.0/docs/api/

public void setWrapStyleWord(boolean word)

public void setWrapStyleWord(布尔词)

Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false. Parameters: word - indicates if word boundaries should be used for line wrapping See Also: getWrapStyleWord()

如果文本区域是换行,则设置使用的换行样式。如果设置为 true,如果行太长而无法容纳在分配的宽度内,则行将在字边界(空白)处换行。如果设置为 false,则行将在字符边界处换行。默认情况下,此属性为 false。参数: word - 指示是否应将单词边界用于换行 另请参见:getWrapStyleWord()