JTextPane不包装文本

时间:2013-01-08 15:06:03

标签: java swing jtextpane

我遇到了一个奇怪的问题。我在JtextPane内部有一个JscrollPane,它在分发列表中显示大字符串,当我使用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 个答案:

答案 0 :(得分:5)

原因可能是java版本。

请参阅https://forums.oracle.com/forums/thread.jspa?messageID=10690405讨论的内容并提供解决方法

答案 1 :(得分: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; 
              } 
          }; 
      } 
  });