これには本当に苦労しました。パネルのサイズを 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 に設定されているため、それより広いテキストは切り取られます)。