【问题标题】:TextLayout with linebreak and tab带有换行符和制表符的 TextLayout
【发布时间】:2012-08-03 15:56:50
【问题描述】:

我有一个JComponent,它使用Textlayout 绘制一个多行属性字符串,运行得非常好:

public static float drawMultiAttributedString(Graphics2D g2d, AttributedString str, String plainString, int maxWidth, int startX, float startY){

    FontRenderContext fontRenderCtx = g2d.getFontRenderContext();

    AttributedCharacterIterator attrCharIter = str.getIterator();
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attrCharIter, fontRenderCtx);
    float x = startX;
    float y = startY;

    int next; 
    int limit; //länge bis zum nächsten umbruch
    int charat; 
    String tested = plainString;

    while (lineBreakMeasurer.getPosition() < attrCharIter.getEndIndex()) { 

          next = lineBreakMeasurer.nextOffset(maxWidth);
          limit = next;
          charat = tested.indexOf("\n", lineBreakMeasurer.getPosition()+1);
          if(next > (charat - lineBreakMeasurer.getPosition()) && charat != -1){
             limit = charat - lineBreakMeasurer.getPosition();
          }
          TextLayout layout = lineBreakMeasurer.nextLayout(maxWidth, lineBreakMeasurer.getPosition() + limit, false);

              y += layout.getAscent(); //höhe des haupttextes

          layout.draw(g2d, x, y);

          y += layout.getDescent() + layout.getLeading(); 
    }

    return y - startY;
}

问题是,Textlayout 不呈现我需要的制表符。 有谁知道我如何包括制表符?有什么想法吗?

非常感谢!

【问题讨论】:

    标签: java swing text line-breaks tabular


    【解决方案1】:

    带有一个或多个自定义渲染器的JTable 可能是更简单的选择。 DefaultTableCellRenderer 扩展了 JLabel,它承认 HTML 和特定于语言环境的 formatting

    【讨论】:

      猜你喜欢
      • 2012-05-25
      • 1970-01-01
      • 2016-02-05
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2012-03-03
      • 2011-07-29
      相关资源
      最近更新 更多