计算字符串的高度和宽度

import java.awt.FontMetrics; 
public int[] getStringWidth(String str){ 
  JLabel label = new JLabel(); 
  FontMetrics metrics; 
  int textH = 0; 
  int textW = 0; 
  label.setText(str); 
  metrics = label.getFontMetrics(label.getFont()); 
  textH = metrics.getHeight();//字符串的高,   只和字体有关 
  textW = metrics.stringWidth(label.getText());//字符串的宽 
  int[] num = new int[2]; 
  num[0] = textH; 
  num[1] = textW; 
  return num; 
}

posted on 2014-09-19 09:54  Mr.SuYang  阅读(591)  评论(0)    收藏  举报

导航