2013-01-08 74 views
3

我遇到了一個奇怪的問題。我在JscrollPane中有一個JtextPane,它在分配列表中顯示大字符串,當我使用eclipse運行程序時它正確地包裝代碼,但是當我使用java webstart運行相同的程序時,它停止了包裝文本。你能告訴我我該怎麼做。這是代碼。JTextPane不包裝文本

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; 

}

回答

5

原因可能是Java版本。

https://forums.oracle.com/forums/thread.jspa?messageID=10690405在那裏討論和解決方法提供

+0

好,我試過被提及,但碰上一個例外的解決方案。線程「Thread-40」中的異常java.lang.ClassCastException:javax.swing.text.DefaultStyledDocument與javax.swing.text.html.HTMLDocument不兼容 – Ashish

+0

這不適用於所有Java7版本,@ StanislavL,[你可以請回答這個問題呢](http://stackoverflow.com/questions/11000220/strange-text-wrapping-with-styled-text-in-jtextpane-with-java-7/11001972#11001972 ),並與您的代碼解決方法pleeeease – mKorbel

+0

@mKorbel我會盡力調查,但不知道我什麼時候會有足夠的時間 – StanislavL

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