How do I calculate the width of a string in pixels?

前端 未结 5 1394
灰色年华
灰色年华 2020-12-18 07:11

How to calculate width of a String in pixels in Java? For e.g., I have a string say \"Hello World!\". What is its length in pixels, also considering its fon

5条回答
  •  一生所求
    2020-12-18 07:52

    there are couple of ways to do it, you can try label.getElement().getClientWidth(); if the text is in a lable, if you're using AWT you can use Graphics.getFontMetrics then FontMetrics.stringWidth

提交回复
热议问题