我的程序有问题。这是WordWrap能够......
确切地说,我想使用linwrap,但我不想要wordwrap ......
我搜索了这个
JTextPane textPane = new JTextPane();
JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
JScrollPane scrollPane = new JScrollPane( noWrapPanel );
当我使用此代码时,linewrap和wordwrap都被禁用....
我想使用linewrap ....
对不起我糟糕的英语技巧...... 我相信你能明白我的意思 请帮帮我......答案 0 :(得分:1)
这是:
// Override getScrollableTracksViewportWidth
// to preserve the full width of the text
public boolean getScrollableTracksViewportWidth() {
Component parent = getParent();
ComponentUI ui = getUI();
return parent != null ? (ui.getPreferredSize(this).width <= parent
.getSize().width) : true;
}
答案 1 :(得分:0)
请试一试。
JTextPane textPane;
public void someMethod() {
textPane = new JTextPane(new DefaultStyledDocument());
textPane.setEditorKit(new ExtendedStyledEditorKit());
}
/** To enable no wrap to JTextPane **/
static class ExtendedStyledEditorKit extends StyledEditorKit {
private static final long serialVersionUID = 1L;
private static final ViewFactory styledEditorKitFactory = (new StyledEditorKit()).getViewFactory();
private static final ViewFactory defaultFactory = new ExtendedStyledViewFactory();
public Object clone() {
return new ExtendedStyledEditorKit();
}
public ViewFactory getViewFactory() {
return defaultFactory;
}
/* The extended view factory */
static class ExtendedStyledViewFactory implements ViewFactory {
public View create(Element elem) {
String elementName = elem.getName();
if (elementName != null) {
if (elementName.equals(AbstractDocument.ParagraphElementName)) {
return new ExtendedParagraphView(elem);
}
}
// Delegate others to StyledEditorKit
return styledEditorKitFactory.create(elem);
}
}
}
static class ExtendedParagraphView extends ParagraphView {
public ExtendedParagraphView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
return super.getPreferredSpan(axis);
}
}