JTextPane无法自动换行文本。

3
我遇到了一个奇怪的问题。我有一个 JScrollPane 中包含一个 JTextPane,用于显示分发列表中的大量字符串,并且在使用Eclipse运行程序时正确地换行代码,但是当我使用Java WebStart运行相同的程序时,它停止了文本换行。
这是我的代码:
   private JScrollPane displayResults(String distributionList) {
// TODO Auto-generated method stub
  JTextPane textArea = new JTextPane();
  textArea.setText(distributionList);
  textArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(textArea);  
  scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
  return scrollPane;
}
2个回答

5

好的,我尝试了提到的解决方案,但遇到了一个异常。线程“Thread-40”中的异常:java.lang.ClassCastException: javax.swing.text.DefaultStyledDocument与javax.swing.text.html.HTMLDocument不兼容。 - Ashish
这可能不适用于所有Java7版本,@StanislavL,请您也回答一下这个问题,您能否提供一个代码解决方案?谢谢! - mKorbel
@mKorbel 我会尝试调查,但不确定我何时会有足够的时间。 - StanislavL
@StanislavL 抱歉,我看错了,应该是 JTextArea 而不是 JTextPane。 - mKorbel

2

对我来说,这项工作很顺利

      textArea.setEditorKit(new HTMLEditorKit(){ 
     @Override 
     public ViewFactory getViewFactory(){ 

         return new HTMLFactory(){ 
             public View create(Element e){ 
                View v = super.create(e); 
                if(v instanceof InlineView){ 
                    return new InlineView(e){ 
                        public int getBreakWeight(int axis, float pos, float len) { 
                            return GoodBreakWeight; 
                        } 
                        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 == getEndOffset()) { 
                                    return this; 
                                } 
                                return createFragment(p0, p1); 
                            } 
                            return this; 
                          } 
                      }; 
                } 
                else if (v instanceof ParagraphView) { 
                    return new ParagraphView(e) { 
                        protected javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis, javax.swing.SizeRequirements r) { 
                            if (r == null) { 
                                  r = new javax.swing.SizeRequirements(); 
                            } 
                            float pref = layoutPool.getPreferredSpan(axis); 
                            float min = layoutPool.getMinimumSpan(axis); 
                            // Don't include insets, Box.getXXXSpan will include them. 
                              r.minimum = (int)min; 
                              r.preferred = Math.max(r.minimum, (int) pref); 
                              r.maximum = Integer.MAX_VALUE; 
                              r.alignment = 0.5f; 
                            return r; 
                          } 

                      }; 
                  } 
                return v; 
              } 
          }; 
      } 
  }); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接