开发者

How to calculate width of string in px?

How to calculate width of string in px ? I have panel and I try like (this reference on JPanel)

FontMetrics gp = this.getGraphics().getFontMetrics();
int x=gp.stringWidth("test开发者_StackOverflow");

but getGraphics returns always null because panel isn't visible yet. How to solve this problem ?


You should do this in one of the paint() or paintComponent() methods where the Graphics instance exists.

Or you can can create a BufferedImage and call getGraphics().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜