gpt4 book ai didi

java - 带有换行符和制表符的 TextLayout

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:45 27 4
gpt4 key购买 nike

我有一个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 不渲染我需要的制表符。有谁知道我如何也可以包括制表符?有什么想法吗?

非常感谢!

最佳答案

一个JTable使用一个或多个自定义渲染器可能是一种更简单的选择。 DefaultTableCellRenderer 扩展了 JLabel,它承认 HTML和特定于区域的 formatting .

关于java - 带有换行符和制表符的 TextLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799201/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com