您的位置:首页 > 其它

Getting the Dimensions of Text:获取文本的长和宽

2009-06-11 00:32 441 查看
// From within the paint() method.

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D)g;

Font font = new Font("Serif", Font.PLAIN, 12);

FontMetrics fontMetrics = g2d.getFontMetrics();

int width = fontMetrics.stringWidth("aString");

int height = fontMetrics.getHeight();

}

// From within a component.

class MyComponent extends JComponent {

MyComponent() {

Font font = new Font("Serif", Font.PLAIN, 12);

FontMetrics fontMetrics = getFontMetrics(font);

int width = fontMetrics.stringWidth("aString");

int height = fontMetrics.getHeight();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: