java 如何在 JTextArea 中找到光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find Cursor position in a JTextArea
提问by Imdad Sarkar
Would someone help me in finding cursor position in a JTextArea in terms of pixel?
I have used txtArea.getCaretPosition()
. But this is not the position I expect. Actually i want the cursor position in x,y
coordinate of pixel.
有人会帮助我在 JTextArea 中以像素为单位找到光标位置吗?我用过txtArea.getCaretPosition()
。但这不是我期望的位置。实际上我想要x,y
像素坐标中的光标位置。
回答by Uhlen
TextUIhave method modelToView
that'll give you position/size of the caret:
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();
}
});
}
}
回答by KáGé
Note that some of these answers return the location relative to the text component you're getting the caret position from.
请注意,其中一些答案返回相对于您从中获取插入符号位置的文本组件的位置。
If you want to get the caret's position on the screen, I suggest you use this method:
如果你想获得插入符号在屏幕上的位置,我建议你使用这种方法:
Caret caret = yourTextComponent.getCaret();
Point p = caret.getMagicCaretPosition();
p.x += yourTextComponent.getLocationOnScreen().x;
p.y += yourTextComponent.getLocationOnScreen().y;
aFrameYourePositioning.setLocation(p);
回答by Tony Chemit
Here is an example to display a JPopup at the caret position
这是在插入符号位置显示 JPopup 的示例
Caret caret = logText.getCaret();
Point p = new Point(caret.getMagicCaretPosition());
p.x += logText.getLocationOnScreen().x;
p.y += logText.getLocationOnScreen().y;
SwingUtilities.convertPointFromScreen(p, actionsPanel);
popup.show(actionsPanel, p.x, p.y);
回答by Will
You may have to use the FontMetrics class.
Analyze the text, figure out the new lines / wrapping of the text.
then use
您可能必须使用 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();
etc....
等等....
And if you really want, post your solution here to be cool. :)
如果你真的想要,在这里发布你的解决方案很酷。:)
回答by Anton
Just like below
就像下面一样
Point point = textArea.getCaret().getMagicCaretPosition();
Please be aware the point could be null
请注意该点可能为空
回答by Stas Jaro
I made this one after trying the other posts here. It works perfectly as long has your textarea has text wrapping disabled.
我在这里尝试了其他帖子后做了这个。只要您的 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;
}
回答by Furkan
Rectangle caretRect=textPane.getUI().modelToView(textPane,textPane.getCaretPosition());
or
或者
Rectangle caretRect=textPane.modelToView(textPane.getCaretPosition());
for example; use this rect for showing popup:
例如; 使用此矩形显示弹出窗口:
popupMenu.show(textPane, caretRect.x, caretRect.y);
popupMenu.show(textPane, caretRect.x, caretRect.y);