Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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 文本编辑器分隔符行号重叠&;长线标记_Java_Swing_Line Numbers_Long Lines - Fatal编程技术网

Java 文本编辑器分隔符行号重叠&;长线标记

Java 文本编辑器分隔符行号重叠&;长线标记,java,swing,line-numbers,long-lines,Java,Swing,Line Numbers,Long Lines,我正在做一个项目,在Swing中创建一个文本编辑器(基本上是记事本),我遇到了行号问题。 我必须说我对java相当陌生,但我正在尽最大努力改变这一点 在我正在使用的JEditorPane中处理行号的类下面。但每次它在行号上添加另一个数字时,我都会得到一个重复的分隔行,用于约10行文本。我怎样才能防止这种情况发生 另外,我如何添加一个“长线标记”,即在左侧约100个'm'字符处绘制的另一条线 这是一节课: /** * Part of the source code got from here:

我正在做一个项目,在
Swing
中创建一个文本编辑器(基本上是记事本),我遇到了行号问题。 我必须说我对java相当陌生,但我正在尽最大努力改变这一点

在我正在使用的
JEditorPane
中处理行号的类下面。但每次它在行号上添加另一个数字时,我都会得到一个重复的分隔行,用于约10行文本。我怎样才能防止这种情况发生

另外,我如何添加一个“长线标记”,即在左侧约100个
'm'
字符处绘制的另一条线

这是一节课:

/**
 * Part of the source code got from here:
 * developer.com/java/other/article.php/3318421/Add-Line-Numbering-in-the-JEditorPane.htm
 *
 * About the Author Stanislav Lapitsky is an offshore software developer and
 * consultant with more than 7 years of programming experience. His area of
 * knowledge includes java based technologies and RDBMS.
 *
 */
package MainGUI;

/**
 *
 * @author mrbigheart
 */
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class LineNumbers {

    // private default constructor
    private LineNumbers() {
    }

    // private constructor
    private LineNumbers(JEditorPane edit) {
        edit.setEditorKit(new NumberedEditorKit());
    }

    // static factory
    public static LineNumbers valueOf(JEditorPane edit) {
        return new LineNumbers(edit);
    }

}

class NumberedEditorKit extends StyledEditorKit {

    @Override
    public ViewFactory getViewFactory() {
        return new NumberedViewFactory();
    }
}

class NumberedViewFactory implements ViewFactory {

    @Override
    public View create(Element elem) {
        String kind = elem.getName();
        if (kind != null) {
            switch (kind) {
                case AbstractDocument.ContentElementName:
                    return new LabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    // return new ParagraphView(elem);
                    return new NumberedParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
            }
        }
        // default to text display
        return new LabelView(elem);
    }

}

final class NumberedParagraphView extends ParagraphView {

    public static short NUMBERS_WIDTH = 30;

    public NumberedParagraphView(Element e) {
        super(e);
        short top = 7;
        short left = 0;
        short bottom = 0;
        short right = 0;
        this.setInsets(top, left, bottom, right);
    }

    // indent for the JEditorPane
    @Override
    protected void setInsets(short top, short left, short bottom,
            short right) {
        super.setInsets(top, (short) (left + NUMBERS_WIDTH + 5),
                bottom, right);
    }

    @Override
    public void paintChild(Graphics g, Rectangle r, int n) {
        super.paintChild(g, r, n);
        View parent = this.getParent();
        int previousLineCount = getPreviousLineCount();
        int numberX = r.x - getLeftInset();
        int numberY = r.y + r.height - 5;

        //  Update sizes when number of digits in the line number changes
        int lines = getPreviousLineCount();
        int digits = Math.max(String.valueOf(lines).length(), 2);
        FontMetrics fontMetrics = g.getFontMetrics();
        //get the width of a zero character times the number of digits
        int width = (fontMetrics.charWidth('0') * digits) + numberX + 7;
        // update NUMBERS_WIDTH with the new width
        NUMBERS_WIDTH = (short)width;

        // line numbers rectangle (x, y, width, height)
        g.drawRect(0, 0, width, parent.getContainer().getHeight());
        g.setColor(Color.YELLOW);
        g.drawString(Integer.toString(previousLineCount + n + 1),
                numberX, numberY);
    }

    public int getPreviousLineCount() {
        int lineCount = 0;
        View parent = this.getParent();
        int count = parent.getViewCount();
        for (int i = 0; i < count; i++) {
            if (parent.getView(i) == this) {
                break;
            } else {
                lineCount += parent.getView(i).getViewCount();
            }
        }
        return lineCount;
    }
}
/**
*部分源代码从这里获得:
*developer.com/java/other/article.php/3318421/Add-Line-number-in-the-JEditorPane.htm
*
*关于作者Stanislav Lapitsky是一名离岸软件开发人员
*具有7年以上编程经验的顾问。他的工作领域
*知识包括基于java的技术和RDBMS。
*
*/
包MainGUI;
/**
*
*@作者比格哈特先生
*/
导入java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类行号{
//私有默认构造函数
专用行号(){
}
//私有构造函数
专用行号(编辑窗格){
edit.setEditorKit(newnumberededitorkit());
}
//静态工厂
公共静态行号值(编辑){
返回新的行号(编辑);
}
}
类numbereditorkit扩展了StyledEditorKit{
@凌驾
公共ViewFactory getViewFactory(){
返回新的NumberedViewFactory();
}
}
类NumberedViewFactory实现ViewFactory{
@凌驾
公共视图创建(元素元素){
字符串种类=elem.getName();
如果(种类!=null){
开关(种类){
案例AbstractDocument.ContentElementName:
返回新的LabelView(elem);
案例AbstractDocument.ParagraphElementName:
//返回新段落视图(elem);
返回新的NumberedParagraphView(elem);
案例AbstractDocument.SectionElementName:
返回新的BoxView(元素,视图Y_轴);
case StyleConstants.ComponentElementName:
返回新组件视图(elem);
case StyleConstants.IconElementName:
返回新的IconView(元素);
}
}
//默认为文本显示
返回新的LabelView(elem);
}
}
最终类NumberedParagraphView扩展了ParagraphView{
公共静态短编号\u宽度=30;
公共编号段落视图(元素e){
超级(e);
短顶=7;
短左=0;
短底=0;
右短=0;
此设置插图(顶部、左侧、底部、右侧);
}
//绝地武士窗格的缩进
@凌驾
受保护的空心镶块(短顶、短左、短底、,
(右短){
超级插图(上,(短)(左+数字+宽度+5),
右下角);
}
@凌驾
公共虚空paintChild(图形g、矩形r、整数n){
super.paintChild(g,r,n);
View parent=this.getParent();
int previousLineCount=getPreviousLineCount();
int numberX=r.x-getLeftInset();
整数=r.y+r.height-5;
//当行号中的位数更改时更新大小
int lines=getPreviousLineCount();
int digits=Math.max(String.valueOf(lines).length(),2);
FontMetrics FontMetrics=g.getFontMetrics();
//获取零字符的宽度乘以位数
int-width=(fontMetrics.charWidth('0')*位)+numberX+7;
//用新的宽度更新数字和宽度
数字\宽度=(短)宽度;
//行号矩形(x、y、宽度、高度)
g、 drawRect(0,0,width,parent.getContainer().getHeight());
g、 setColor(颜色为黄色);
g、 drawString(整数.toString(以前的LineCount+n+1),
数字x,数字);
}
public int getPreviousLineCount(){
int lineCount=0;
View parent=this.getParent();
int count=parent.getViewCount();
for(int i=0;i

提前谢谢你

这对我来说似乎是一种进步。在paintChild()中,强制宽度和数字\u宽度为Math.max()的最大值:


这太棒了!它终于起作用了。。谢谢你的回复!。。如果您愿意的话,任何关于长线标记的参考都将不胜感激。。只要给我指出正确的方向就足够了!我不会使用JEditorPane作为文本编辑器。JEditorPane实际上是为显示HTML而设计的。您应该使用JTextPane。您还可以签出可与文本窗格一起使用的。
int width = (fontMetrics.charWidth('0') * digits) + numberX + 7;
// update NUMBERS_WIDTH with the new width
NUMBERS_WIDTH = (short) Math.max(NUMBERS_WIDTH, width);

// line numbers rectangle (x, y, width, height)
g.drawRect(0, 0, NUMBERS_WIDTH, parent.getContainer().getHeight());