先要得到字体型号
具体看java.awt.FontMetrics类
里面有stringWidth(String str)方法可以得到字符串长度

解决方案 »

  1.   

    首先得到字体大小 或提前设定 如 12pt
    然后得到字符串长度 stringWidth
    所要长度=字体大小长度×字符串长度
      

  2.   

    FontRenderContext context =g2d.getFontRenderContext();
    Rectangle2D bounds=f.getStringBounds(text,context);
    //f为你所用的字体
    bounds.getWidth()  //就是text的显示长度
      

  3.   

    textArea.getGraphics().getFontMetrics().stringWidth(str);
      

  4.   

    感谢doni(蓝色天空) 的帮助.问题已解决.