有两种方法。
一种是获取相对JTextPane左上角的相对坐标。滚动文本会影响这个坐标的计算。
Rectangle caretRectangle = textPane.getUI().modelToView(textPane,textPane.getCaretPosition());
x=(int) caretRectangle.getX();
y=(int) caretRectangle.getY();
另一种,是相对屏幕可视区域来计算。滚动不会影响它的计算
Caret caret = textPane.getCaret();
Point p = caret.getMagicCaretPosition();
p.x += textPane.getLocationOnScreen().x;
p.y += textPane.getLocationOnScreen().y;