我们如何在Java的JTextArea中显示行号?

How can we display the line numbers inside a JTextArea in Java?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Element;
public class LineNumberTextAreaTest extends JFrame {
 private static JTextArea textArea;
 private static JTextArea lines;
 private JScrollPane jsp;
 public LineNumberTextAreaTest() {
   setTitle("LineNumberTextArea Test");
   jsp = new JScrollPane();
   textArea = new JTextArea();
   lines = new JTextArea("1");
   lines.setBackground(Color.LIGHT_GRAY);
   lines.setEditable(false);
   // Code to implement line numbers inside the JTextArea
   textArea.getDocument().addDocumentListener(new DocumentListener() {
    public String getText() {
      int caretPosition = textArea.getDocument().getLength();
      Element root = textArea.getDocument().getDefaultRootElement();
      String text ="1" + System.getProperty("line.separator");
       for(int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
         text += i + System.getProperty("line.separator");
       }
      return text;
    }
    @Override
    public void changedUpdate(DocumentEvent de) {
      lines.setText(getText());
    }
    @Override
    public void insertUpdate(DocumentEvent de) {
      lines.setText(getText());
    }
    @Override
    public void removeUpdate(DocumentEvent de) {
      lines.setText(getText());
    }
   });
   jsp.getViewport().add(textArea);
   jsp.setRowHeaderView(lines);
   add(jsp);
   setSize(400, 275);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLocationRelativeTo(null);
   setVisible(true);
 }
 public static void main(String[] args) {
   new LineNumberTextAreaTest();
 }
}

输出量