[英]Get XY position of caret position in JTextArea
實際上,我已經在這里問了這個問題。 但是,我犯錯了。 我還沒有解決方案。
首先,在前面的問題上,我可以使用
Rectangle rectangle = textArea.modelToView( textArea.getCaretPostion() );
我也有X和Y的位置。
我正在創建一個編輯器,每次按Enter鍵時,都可以添加新的文本區域。 上面帶有代碼的XY位置在每個文本區域中始終給出相同的返回值。 看我的代碼。
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
public class forquestion extends JFrame {
Container textAreaBox;
LinkedList<JTextComponent> textArea;
int nameTA;
public forquestion() {
int nameTA = 0;
textArea = new LinkedList<>();
textAreaBox = Box.createVerticalBox();
textAreaBox.add(Box.createVerticalGlue());
addLine();
this.add(textAreaBox);
this.setVisible(true);
}
public static void main(String[] args) {
forquestion app = new forquestion();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void addLine () {
JTextComponent temp_ta = createTextComponent();
textArea.add(temp_ta);
textAreaBox.add(textArea.getLast());
textAreaBox.add(Box.createVerticalGlue());
}
protected JTextComponent createTextComponent() {
JTextArea ta = new JTextArea("test");
/*if (count%2==0)
ta.setForeground(Color.red);
else
ta.setForeground(Color.GREEN);*/
ta.setFont(new Font("Courier New",Font.PLAIN,16));
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setName(Integer.toString(nameTA));
nameTA+=1;
basicKey("ENTER", enter, ta);
ta.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent ev) {
try {
taMousePressed(ev);
} catch (BadLocationException ex) {
Logger.getLogger(forquestion.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
return ta;
}
public void basicKey(String s, Action a, JTextArea ta) {
ta.getInputMap().put(KeyStroke.getKeyStroke(s), s);
ta.getActionMap().put(s, a);
}
Action enter = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
addLine();
}
};
private void taMousePressed(java.awt.event.MouseEvent ev) throws BadLocationException {
int now_focus = Integer.parseInt(ev.getComponent().getName());
int _caret;
_caret = textArea.get(now_focus).getCaretPosition();
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
double x = rectangle.getX();
//int xc = textArea.get(now_focus).getLocation().x;
double y = rectangle.getY();
//int yc = textArea.get(now_focus).getLocation().y;
//double h = rectangle.getHeight();
//double w = rectangle.getWidth();
System.out.println(x);
System.out.println(y);
//System.out.println(xc);
//System.out.println(yc);
//System.out.println(h);
//System.out.println(w);
System.out.println("");
}
}
每次按下文本區域,我的代碼都會打印XY位置。 但是,每個文本區域的顯示始終相同。 (嘗試創建多個文本區域並提供一些文本)順便說一句,它只是簡單的代碼。 您需要更改窗口框的大小,以便在按Enter鍵之后更新新的文本區域。
因此,我的問題是:如何獲得任何文本區域中插入符號(文本光標)的XY位置。 我想在那里顯示JPopmenu。 :)
我希望這個問題能為您解決。 謝謝。
向后報告的Rectangle
相對於文本區域,其0x0位置是組件的頂部,左上角。
如果您使用類似...
popup.show(textArea.get(now_focus), rectangle.x, rectangle.y + rectangle.height);
popup
是JPopupMenu
,它將對屏幕本身進行所需的翻譯。
現在。 話說回來。 就個人而言,我更喜歡使用Swing提供的彈出API支持。 這意味着需要創建一個從JTextArea
擴展來的自定義組件來實現它。
public class MyPopupTextArea extends JTextArea {
/*...*/
public Point getPopupLocation(MouseEvent evt) {
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
Point p = rectangle.getLoction();
p.y += rectangle.height;
return p;
}
}
然后,根據您的需要,可以使用setComponentPopup
提供JPopupMenu
的共享實例,或者,如果需要,為自定義編輯器的每個實例創建一個自定義JPopupMenu
,並根據需要使用setComponentPopup
...不要用鼠標弄亂聽眾;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.