6

JTextArea のカーソル位置をピクセル単位で見つけるのを手伝ってくれる人はいますか? 私は使用しtxtArea.getCaretPosition()ました。しかし、これは私が期待する位置ではありません。x,y実際には、ピクセルの座標でカーソル位置が必要です。

4

7 に答える 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

以下のように

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

ポイントがnullになる可能性があることに注意してください

于 2011-04-29T12:22:54.927 に答える
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();  

等....

そして、本当に必要な場合は、ソリューションをここに投稿してクールにしてください。:)

于 2010-12-30T20:25:59.937 に答える
0

ここで他の投稿を試した後、これを作成しました。テキストエリアでテキストの折り返しが無効になっている限り、完全に機能します。

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
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 に答える