如何在JTextArea中找到Cursor位置

有人会帮助我在像素方面找到JTextArea中的光标位置吗? 我使用过txtArea.getCaretPosition() 。 但这不是我期望的立场。 其实我想要光标位置在像素的x,y坐标。

TextUI有方法modelToView ,它将为您提供插入符号的位置/大小:

 import java.awt.BorderLayout; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; public class PixelPositionOfCaretDemo { public PixelPositionOfCaretDemo() { JLabel label = new JLabel("Move the caret..."); JTextArea area = new JTextArea(); area.addCaretListener(new MyCaretListener(label)); JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.setBounds(new Rectangle(400, 400, 400, 400)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(area, BorderLayout.CENTER); frame.add(label, BorderLayout.SOUTH); frame.setVisible(true); } private static class MyCaretListener implements CaretListener { private final JLabel label; MyCaretListener(JLabel label) { this.label = label; } @Override public void caretUpdate(CaretEvent e) { JTextComponent textComp = (JTextComponent) e.getSource(); try { Rectangle rect = textComp.getUI().modelToView(textComp, e.getDot()); label.setText(rect.toString()); } catch (BadLocationException ex) { throw new RuntimeException("Failed to get pixel position of caret", ex); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new PixelPositionOfCaretDemo(); } }); } } 

请注意,其中一些答案会返回相对于您从插入位置获取的文本组件的位置。

如果你想在屏幕上获得插入符号的位置,我建议你使用这个方法:

 Caret caret = yourTextComponent.getCaret(); Point p = caret.getMagicCaretPosition(); px += yourTextComponent.getLocationOnScreen().x; py += yourTextComponent.getLocationOnScreen().y; aFrameYourePositioning.setLocation(p); 

您可能必须使用FontMetrics类。
分析文本,找出文本的新行/包装。 然后用

  Font font = new Font("Verdana", Font.PLAIN, 10); FontMetrics metrics = new FontMetrics(font); Rectangle2D bounds = metrics.getStringBounds("string", null); int widthInPixels = (int) bounds.getWidth(); 

等等….

如果您真的想要,请在此处发布您的解决方案,以便更酷。 🙂

就像下面一样

 Point point = textArea.getCaret().getMagicCaretPosition(); 

请注意,这一点可能为空

在尝试了其他post后我做了这个。 只要您的textarea禁用了文本换行,它就能完美运行。

 public Point getCaretPixelPosition(JTextArea a) { try { int cpos = a.getCaretPosition(); Font font = a.getStyledDocument().getFont(a.getStyledDocument().getLogicalStyle(cpos)); FontMetrics metrics = getFontMetrics(font); int lineNum = a.getLineOfOffset(cpos); int y = lineNum * metrics.getHeight(); int lineStart = a.getLineStartOffset(lineNum); int x = metrics.stringWidth(a.getText().substring(lineStart, cpos)); return new Point(x,y); } catch(BadLocationException e) {} return null; } 
 Rectangle caretRect=textPane.getUI().modelToView(textPane,textPane.getCaretPosition()); 

要么

 Rectangle caretRect=textPane.modelToView(textPane.getCaretPosition()); 

例如; 使用此rect显示弹出窗口:

popupMenu.show(textPane,caretRect.x,caretRect.y);

以下是在插入符号位置显示JPopup的示例

 Caret caret = logText.getCaret(); Point p = new Point(caret.getMagicCaretPosition()); px += logText.getLocationOnScreen().x; py += logText.getLocationOnScreen().y; SwingUtilities.convertPointFromScreen(p, actionsPanel); popup.show(actionsPanel, px, py);