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().
加载中,请稍侯......
精彩评论