【问题标题】:How to disable WordWrap in Java JTextPane?如何在 Java JTextPane 中禁用 WordWrap?
【发布时间】:2014-04-18 07:29:42
【问题描述】:

我的程序有问题.. WordWrap 是有能力的...

没错,我想使用 linwrap 但我不想要 wordwrap...

我搜索过这个

JTextPane textPane = new JTextPane();
JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
JScrollPane scrollPane = new JScrollPane( noWrapPanel );

当我使用这段代码时,换行和自动换行都被禁用......

我想使用换行......

对不起,我的英语水平太差了... 我相信你能明白我的意思 请帮帮我...

【问题讨论】:

  • I believe you can get what i mean please help me... - 不是真的。在 Window7 上使用 JDK7 我没有得到“自动换行”。这是一个完整的单词在发生换行时移到新行,我相信这是正确的行为。也许我不明白你所说的自动换行是什么意思。如果您希望文本在单词中间换行,那么您可以使用 JTextArea。否则,您可以发布 SSCCE 来证明您的问题。

标签: java swing jtextpane word-wrap


【解决方案1】:

这里是:

Link for it

// 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;
}

Other link

【讨论】:

    【解决方案2】:

    请试试这个。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多