有人会帮助我在像素方面找到 JTextArea 中的光标位置吗?我用过txtArea.getCaretPosition()
。但这不是我所期望的位置。实际上我想要光标位置在x,y
像素坐标中。
问问题
15094 次
7 回答
4
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();
}
});
}
}
于 2010-12-30T20:50:33.897 回答
2
请注意,其中一些答案会返回相对于您从中获取插入符号位置的文本组件的位置。
如果你想获得插入符号在屏幕上的位置,我建议你使用这个方法:
Caret caret = yourTextComponent.getCaret();
Point p = caret.getMagicCaretPosition();
p.x += yourTextComponent.getLocationOnScreen().x;
p.y += yourTextComponent.getLocationOnScreen().y;
aFrameYourePositioning.setLocation(p);
于 2012-04-28T16:13:58.103 回答
0
Just like below
Point point = textArea.getCaret().getMagicCaretPosition();
Please be aware the point could be null
于 2011-04-29T12:22:54.927 回答
0
Rectangle caretRect=textPane.getUI().modelToView(textPane,textPane.getCaretPosition());
或者
Rectangle caretRect=textPane.modelToView(textPane.getCaretPosition());
例如; 使用此矩形显示弹出窗口:
popupMenu.show(textPane, caretRect.x, caretRect.y);
于 2011-10-18T13:26:55.657 回答
0
我在这里尝试了其他帖子后做了这个。只要您的 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;
}
于 2011-05-04T23:01:00.690 回答
0
您可能必须使用 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....
如果你真的想要,在这里发布你的解决方案很酷。:)
于 2010-12-30T20:25:59.937 回答
0
这是一个在插入符号位置显示 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);
于 2017-04-18T07:21:52.090 回答