Java JTextArea中我想实时获取光标在文本框中的行和列

2025-04-22 13:25:07
推荐回答(2个)
回答1:

public class NewClass {

public static void main(String[] args) {
JFrame frame = new JFrame();
final JTextArea text = new JTextArea();
frame.setDefaultCloseOperation(3);
frame.setContentPane(text);
text.addCaretListener(new CaretListener() {

public void caretUpdate(CaretEvent e) {
System.out.println(e.getMark());
try {
System.out.println(text.modelToView(text.getCaretPosition()));
} catch (BadLocationException ex) {
}
}
});
frame.setSize(500, 600);
frame.setVisible(true);
}
}

回答2:

CaretListener and CaretEvent