我放了一个JTextPane
进入JScrollPane
。当我尝试时,JTextPane
如果长线超出显示区域的宽度,将自动换行。自动换行是基于word boundary
,例如space
特点。
我的内容包含大量space
。我想显示它literally
。所以我需要自动换行,但我希望它发生ONLY
显示区域最大宽度时,NOT
上word boundary
。
如何?
我尝试过的:
- 将所有空格替换为“\0”,所以从字面上看,我的内容是
single big word
.
添加1
以下是我在阅读 StanislavL's solution 后的失败尝试。 我并不是责怪他的解决方案,因为我的场景与他的不完全相同。
StanislavL
的解决方案要求一行至少包含 2 LabelView
s。据他介绍,这是由Swing
强加的。 layout()
的实现方法where forced break works only if row view has more than one child
(参见:http://java-sl.com/wrap.html)。所以StanislavL
故意为 \r
分配了一个特殊属性确保单独的 LabelView
的字符。并使用 \r
作为包装的里程碑。但在我的场景中,我无法在内容中插入任何字符。
我的想法很简单,只要提供ViewFactory
的定制实现即可对于StyledEditorKit
自 ViewFactory
接口(interface)确定断裂权重以及断裂应如何发生:
this.jTextPane.setEditorKit(new StyledEditorKit(){
@Override
public ViewFactory getViewFactory(){
return new LetterWrappingStyledViewFactory(maxCharWidth);
}
});
下面是我对接口(interface) ViewFactory
的实现:
public class LetterWrappingStyledViewFactory implements ViewFactory {
public int maxCharWidth = -1; // this is the max width where I want wrap to happen.
public LetterWrappingStyledViewFactory(int maxCharWidth) {
this.maxCharWidth = maxCharWidth;
}
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem) {
public int getBreakWeight(int axis, float pos, float len) {
if (axis == View.X_AXIS) {
checkPainter();
int p0 = getStartOffset();
int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
if (p1 > maxCharWidth)
return View.ForcedBreakWeight;
else
return View.BadBreakWeight;
}
return super.getBreakWeight(axis, pos, len);
}
public View breakView(int axis, int p0, float pos, float len) {
if (axis == View.X_AXIS) {
checkPainter();
int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
if (p0 == getStartOffset() && p1 <= maxCharWidth) {
return this;
}
return createFragment(p0, maxCharWidth);
}
return this;
}
};
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
最佳答案
此示例适用于 StyledEditorKit(使用 java 7 进行测试)
import javax.swing.*;
import javax.swing.text.*;
public class WrapApp extends JFrame {
JEditorPane edit=new JEditorPane();
public WrapApp() {
super("Wrap in the mid");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
edit.setEditorKit(new WrapEditorKit());
edit.setText("111 222 333333333333333333333333333333333333333333333");
getContentPane().add(new JScrollPane(edit));
setSize(200,200);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
WrapApp m = new WrapApp();
m.setVisible(true);
}
}
class WrapEditorKit extends StyledEditorKit {
ViewFactory defaultFactory=new WrapColumnFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
class WrapColumnFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new WrapLabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
class WrapLabelView extends LabelView {
public WrapLabelView(Element elem) {
super(elem);
}
public int getBreakWeight(int axis, float pos, float len) {
if (axis == View.X_AXIS) {
checkPainter();
int p0 = getStartOffset();
int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
if (p1 == p0) {
// can't even fit a single character
return View.BadBreakWeight;
}
return View.GoodBreakWeight;
}
return super.getBreakWeight(axis, pos, len);
}
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
public View breakView(int axis, int p0, float pos, float len) {
if (axis == View.X_AXIS) {
checkPainter();
int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
GlyphView v = (GlyphView) createFragment(p0, p1);
return v;
}
return super.breakView(axis, p0, pos, len);
}
}
更新:
关于 GlyphPainter。事实上,我们应该找到一个可以中断 GlyphView 的位置(this
在 getBoundedPosition() 中过去)。 p0 表示文档中的字符偏移量(GlyphView 表示字符元素或元素的片段))
所以想象一下一大串文本。在文档中,它只是一个元素,因为它具有所有相同的属性。但我们必须创建多个 LabelView,因为文本片段太大,无法适应可用宽度。因此,对于第一个标签,p0 是 0。然后对于下一个标签,是我们打破最初的大 View 的偏移量。如果我们再次打破它,那么另一个偏移量。然后 2 个参数仅代表容器的 x 位移和宽度(例如父 View )。
所以我们有一个很大的文本,应该找到中断的地方。当前的方法更容易,因为我们不关心优先级(空格与其他字符)。
关于java - 如何 : JTextPane auto wrapping NOT on word boundary such as "space", 但在字母边界上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501191/