本文介绍了Java Swing JTextArea行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此计数文本区域的行号.该代码正常工作,但是在运行此代码时,textarea处于不活动状态,插入符号被隐藏,并且键盘
除非我单击文本区域,否则键将无法工作.

This count line number of textarea. The code works correctly, but when run this code the textarea is not active, the caret is hidden and the keyboard
keys not work unless I click on textarea.

代码:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;

public class LineNumber extends JFrame implements DocumentListener {

    private static final long serialVersionUID = -1093726028044203117L;

    private JScrollPane scroll;
    private JTextArea textArea, lineArea;

    public static void main(String[] args) {

        new LineNumber().setVisible(true);

    }

    public LineNumber() {

        super("Line Numbers");

        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setUI();
    }

    private void setUI() {

        textArea = new JTextArea();

        lineArea = new JTextArea(0, 3);
        lineArea.setEditable(false);
        lineArea.setForeground(Color.GRAY);

        scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        textArea.getDocument().addDocumentListener(this);

        scroll.setViewportView(textArea);
        scroll.setRowHeaderView(lineArea);
        getContentPane().add(scroll, BorderLayout.CENTER);

    }

    public void changedUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());

    }

    public void insertUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());
    }

    public void removeUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());
    }

    public String getLine() {

        int caretPos = 0;
        String lines;

        caretPos = textArea.getDocument().getLength();
        Element root = textArea.getDocument().getDefaultRootElement();
        lines = String.format("%s%s", 1, System.lineSeparator());

        for (int i = 2; i < root.getElementIndex(caretPos) + 2; i++) {
            lines += String.format("%s%s", i, System.lineSeparator());

        }

        return lines;

    }

}

如果我没有将lineArea添加到滚动窗格中,则textarea可以正常工作,但是在将其添加到setRowHeaderView之后,只有单击鼠标才能激活textarea....

If I do not add lineArea to the scrollpane the textarea work correctly but after add to setRowHeaderView the textarea only gets active with a mouse click....

推荐答案

您可以使用以下方法阻止行号文本区域获得焦点:

You can prevent the line number text area from gaining focus by using:

lineArea = new JTextArea(0, 3);
lineArea.setEditable(false);
lineArea.setFocusable(false);

您还可以签出文本组件行号支持以下更好的实现:

You can also check out Text Component Line Number for a fancier implementation that supports:

  1. 包装文字
  2. 具有不同大小字体的文本(使用JTextPane时)

这篇关于Java Swing JTextArea行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:27