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();
}
}
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();
}
}
相关文章推荐
- [SoapUI]怎样获取隐藏元素的文本内容Get text of hidden element
- 获取peergroup的所有peers(Getting a list of the peers in a PeerGroup)
- Getting the dimensions of the soft keyboard
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- Insert Part of the Regex Match into the Replacement Text (将正则匹配插入替换文本中)
- 萨班斯法案全文 Full Text of the Sarbanes-Oxley Act of 2002
- MapReduce报错:Error: java.io.IOException: Initialization of all the collectors failed. Error in last collector was :interface javax.xml.soap.Text
- how to put the text on the left of a radio button in android
- EditText不能获取文本问题
- 获取自定义View中Text文本的实际宽高
- Getting the indexes of duplicate elements in arrays (Ruby)
- Retrieve the match text (提取获取的字符)
- BZOJ 2258 pku2758 Checking the Text 文本校对 Splay+Hash+二分
- MMSEG: A Word Identification System for Mandarin Chinese Text Based on Two Variants of the Maximum M
- Pixel Width of the text in a UILabel
- HTML5 File API: How to see the result of readAsText()
- Text of the inaugural by Barack Obama
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- text/textarea获取文本最后位置焦点
- Android 中如何获取editText文本信息