获取JTextpane的x,y坐标 [英] Getting x,y co-ordinates of JTextpane
问题描述
我有一个JTextPane,想知道是否有一种方法可以获取当前光标的(x,y)像素位置.我不想要getCaretPosition()中的插入符号位置,而是光标当前所在位置的像素坐标.
我在窗格中使用了可变宽度的字体,因此仅将光标位置乘以某个固定宽度是行不通的.
我需要这样做是因为我希望JPopUpMenu直接出现在光标所在的位置下方,而JPopUpMenu show()方法仅采用(x,y)像素坐标.
感谢所有帮助.
提前致谢.
Hi,
I have a JTextPane and want to know if there is a way to get the (x,y) pixel position of the current cursor. I don''t want the caret position that''s from getCaretPosition(), but the pixel co-ordinates of where the cursor currently is.
I''m using a variable width font in the pane, so simply multiplying the cursor position by some fixed width won''t work.
I need this because I want a JPopUpMenu to appear directly below where the cursor is, and the JPopUpMenu show() method only takes (x,y) pixel coordinates.
All help is appreciated.
Thanks in Advance.
推荐答案
您可以翻译插入符号的位置:
Hi,
You can translate the caret position:
Rectangle caretRectangle =
myTextPane.getUI().modelToView(myTextPanepane, myTextPanepane.getCaretPosition());
然后caretRectangle
将插入符号的下部和上部的(x,y)(以像素为单位).
希望这会有所帮助,
弗雷德里克(Fredrik)
The caretRectangle
will then hold the (x, y) for the lower part and the upper part of the caret in pixels.
Hope this helps,
Fredrik
这篇关于获取JTextpane的x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!