JTextArea的行号和调整JTextArea文本大小的问题

时间:2018-09-11 18:55:42

标签: java swing jtextarea jtextpane

此代码计算JTextArea的每一行,并在其中添加行数
左JTextPane

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

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.MatteBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class LineNumber extends JFrame implements DocumentListener {

    private static final long serialVersionUID = -1093726028044203117L;

    private JScrollPane scroll;
    private JTextArea textArea;
    private TextPane 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();
        textArea.getDocument().addDocumentListener(this);

        lineArea = new TextPane(0, 3);
        lineArea.setText(getLine());

        lineArea.setEditable(false);
        lineArea.setFocusable(false);
        lineArea.setBorder(new MatteBorder(0, 0, 0, 1, new Color(248, 248, 248)));
        lineArea.setBackground(new Color(255, 255, 255));
        lineArea.setForeground(Color.GRAY);

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

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

    }

    public void changedUpdate(DocumentEvent event) {

            lineArea.setText(getLine());

    }

    public void insertUpdate(DocumentEvent event) {

        lineArea.setText(getLine());
    }

    public void removeUpdate(DocumentEvent event) {


        lineArea.setText(getLine());

    }

    private String getLine() {

        int caretPos = 0;
        String lines = "";

        caretPos = textArea.getDocument().getLength();
        Element root = textArea.getDocument().getDefaultRootElement();

        for (int i = 1; i < root.getElementIndex(caretPos) + 2; i++)
            lines += String.format("%s  \n", i);

        return lines;

    }

    private int getLength() {

        int caretPos = 0;
        int length = 0;

        caretPos = textArea.getDocument().getLength();
        Element root = textArea.getDocument().getDefaultRootElement();

        int max = 0;
        for (int i = 1; i < root.getElementIndex(caretPos) + 2; i++)
            length = String.valueOf(Math.max(i, max)).length();

        return length;

    }


    private void setRightAlign() {

        StyledDocument doc = lineArea.getStyledDocument();
        SimpleAttributeSet right = new SimpleAttributeSet();

        StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
        doc.setParagraphAttributes(0, doc.getLength(), right, false);

    }

}

如果lineArea和textArea使用相同大小的相同字体
程序运行良好,但是如果更改或调整textArea的字体
在不同的字体大小下效果不佳
并且结束时距离实际结束线太短

code work good and care balance with line number

更改文本区域的字体或字体大小:

enter image description here

调整行区域和文本区域大小后不平衡
我不想更改textarea字体的大小
行号只有nee balance文本区域

1 个答案:

答案 0 :(得分:1)

我不知道TextPane类是什么,因为它不是标准的JDK组件。

如果它是JTextArea,则可以重写getRowHeight(..)方法以基于主JTextArea的Font返回高度。

如果它是JTextPane,则也许可以使用StyleConstants.setSpaceBelow(...)在每行之后添加额外的空间。因此,您需要获取用于计算每个字体高度的两个字体的字体度量。那么区别就在于您在下面的方法中使用的空间。

另一个选择是使用我在上一个问题中提供的类。它已经支持此功能。

相关问题