【问题标题】:JTextPane is not wrapping textJTextPane 不换行
【发布时间】:2013-01-08 15:06:03
【问题描述】:

我遇到了一个奇怪的问题。我在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;
}

【问题讨论】:

    标签: java swing jtextpane


    【解决方案1】:

    原因可能是java版本。

    参见https://forums.oracle.com/forums/thread.jspa?messageID=10690405 讨论并提供解决方法

    【讨论】:

    • 好的,我尝试了提到的解决方案,但遇到了一个异常。线程“Thread-40”中的异常 java.lang.ClassCastException:javax.swing.text.DefaultStyledDocument 与 javax.swing.text.html.HTMLDocument 不兼容
    • 这不是/不可能对所有 Java7 版本、@StanislavL、can you please to answering this question too 都有效,请使用您的代码解决方法
    • @mKorbel 我会尝试调查,但不确定何时有足够的时间
    • @StanislavL 我很糟糕,我错过了阅读 JTextPane 而不是 JTextArea
    【解决方案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; 
                  } 
              }; 
          } 
      }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多