Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextArea的行号和调整JTextArea文本大小存在问题_Java_Swing_Jtextarea_Jtextpane - Fatal编程技术网

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

Java JTextArea的行号和调整JTextArea文本大小存在问题,java,swing,jtextarea,jtextpane,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.DocumentEv

此代码统计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);

    }

}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.border.MatteBorder;
导入javax.swing.event.DocumentEvent;
导入javax.swing.event.DocumentListener;
导入javax.swing.text.Element;
导入javax.swing.text.SimpleAttributeSet;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyledDocument;
公共类LineNumber扩展JFrame实现DocumentListener{
私有静态最终长serialVersionUID=-1093726028044203117L;
私有JScrollPane滚动条;
私人JTEXTEXTAREA textArea;
私有文本窗格lineArea;
公共静态void main(字符串[]args){
new LineNumber().setVisible(真);
}
公共线路号(){
超级(“行号”);
设置大小(500500);
setLocationRelativeTo(空);
setDefaultCloseOperation(关闭时退出);
setUI();
}
私有void setUI(){
textArea=新的JTextArea();
textArea.getDocument().addDocumentListener(此);
lineArea=新文本窗格(0,3);
lineArea.setText(getLine());
lineArea.setEditable(假);
lineArea.setFocusable(假);
lineArea.setboorder(新的无光边框(0,0,0,1,新的颜色(248248248248));
线性区域退根(新颜色(255、255、255));
线性区域设置前景(颜色为灰色);
scroll=新的JScrollPane(JScrollPane.VERTICAL\u SCROLLBAR\u始终,JScrollPane.HORIZONTAL\u SCROLLBAR\u根据需要);
滚动.setViewportView(文本区域);
滚动.setRowHeaderView(lineArea);
getContentPane().add(滚动,BorderLayout.CENTER);
}
公共作废更改日期(DocumentEvent事件){
lineArea.setText(getLine());
}
公共作废插入更新(DocumentEvent事件){
lineArea.setText(getLine());
}
公共作废移除更新(DocumentEvent事件){
lineArea.setText(getLine());
}
私有字符串getLine(){
int caretPos=0;
字符串行=”;
caretPos=textArea.getDocument().getLength();
元素根=textArea.getDocument().getDefaultRootElement();
对于(inti=1;i
如果lineArea和textArea使用相同大小的字体
程序运行良好,但如果更改或调整textArea字体大小
它不能很好地处理不同的字体大小
而且结尾离实际的终点线太近了

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

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

我不知道
文本窗格
类是什么,因为它不是标准的JDK组件

如果它是一个JTextArea,那么您可以重写
getRowHeight(..)
方法,根据主JTextArea的字体返回高度

如果是JTextPane,那么您可以使用
StyleConstants.setSpaceBelow(…)
在每行后面添加额外的空间。因此,您需要获得用于计算每种字体高度的两种字体的字体度量。那么,不同之处在于您对下面的空格方法的使用


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

您是否阅读了上一个问题的答案:?您得到了问题的答案和一个组件的链接,该组件已经为您完成了这项工作。由于您显然没有阅读您问题的答案(或“接受”您问题的答案),我将跳过这一条。.是的,我阅读了,问题解决了,但现在我在文本区域的平衡行号和每行文本方面遇到了问题。
我阅读了,问题解决了,
-然后别忘了“接受”解决问题的答案(通过点击复选标记)让每个人都知道问题已经解决。所有问题都应该这样做。TextPane是JTextPane,但我添加了行和列