[英]How to get Caret position in pixels in JTextField?
How to get Caret position in pixels in JTextField
? 如何在
JTextField
获得插入符号的像素位置? There is a method getCaret().getDot()
, but it returns symbol count, not pixels. 有一个方法
getCaret().getDot()
,但它返回符号计数,而不是像素。 Is there some method besides FontMetrics
? 除了
FontMetrics
还有其他方法吗?
在API中,您可以使用:
int pixelPosition = textField.modelToView( textField.getCaretPosition() ).x;
Given JTextField someJTextField
the following code should give you the pixel position of the care: 给定
JTextField someJTextField
,以下代码应为您提供护理的像素位置:
FontMetrics fm = myJTextField.getFontMetrics();
int caret = someJTextField.getCaret().getDot();
int pixelPosition = fm.stringWidth(myJTextField.getText(0, caret)) -
myJTextField.getScrollOffset();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.