我在這方面非常努力。我需要將面板的大小調整爲AttributedString的寬度。我的解決辦法是:
double GetWidthOfAttributedString(Graphics2D graphics2D, AttributedString attributedString) {
AttributedCharacterIterator characterIterator = attributedString.getIterator();
FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(characterIterator, fontRenderContext);
TextLayout textLayout = lbm.nextLayout(Integer.MAX_VALUE);
return textLayout.getBounds().getWidth();
}
它使用LineBreakMeasurer找到字符串一個TextLayout,然後簡單地檢查與TextLayout的。 (包裝寬度設置爲Integer.MAX_VALUE,因此比這更寬的文本將被截斷)。
我應該添加:我真正需要做的是將AttributedString包裝成行。使用LineBreakMeasurer會更容易,而我在J2ME PBP中也沒有。 :-( –
組件用來顯示用戶界面的畫布?Canvas或LCD UI(Form,Alert,List ...)?你可以附上一張圖片以便更好地理解嗎? –