文本编辑器具有语法突出显示和行号?
-
12-12-2019 - |
题
这可能是一个有点挑战,即使是一个团队项目,更不用说是一个人的实现,但我试图使用一个JEditorPane
使用语法突出显示一个简单而优雅的文本编辑器。我偶然发现了 这个 ,它被停产,真的很难用所有的词汇文件来理解和.LEX里面的东西。我甚至在一些博客中发现这个项目后来被其他一些团队接受但甚至再次停止。我不需要它太想要,就像有代码折叠和东西(即使我很想知道如何做到这一点),但我至少需要一个基本语法突出显示而且非常像Notepad ++一样在左侧的<强大>行号。请记住,我只需要它来突出显示Java源代码,至少现在。
我正在寻找的是教程,一个记录良好的例子和示例代码,一个预先制作的包,即使是NetBeans的工具也可以做到这一点,我不需要从头开始写的源代码,我只需要一个可以使用的实现。提前谢谢! P> P> PTTHIS不会是商业还是太大,不要问为什么我想在那里有这么多编程编剧时重塑车轮,我正在学习,这是一个对我来说是一个很好的练习!< / p>
解决方案
rsyntaxtextarea 是BSD许可并支持您的要求,以及代码折叠等。非常简单使用。
其他提示
好吧,我在类似的项目上工作,这就是我想到的。到目前为止,行号我使用附加到实际TextPane的ScrollPane。然后,滚动块随后将数字更改为以下代码:
public class LineNumberingTextArea extends JTextArea
{
private JTextPane textArea;
/**
* This is the contructor that creates the LinNumbering TextArea.
*
* @param textArea The textArea that we will be modifying to add the
* line numbers to it.
*/
public LineNumberingTextArea(JTextPane textArea)
{
this.textArea = textArea;
setBackground(Color.BLACK);
textArea.setFont(new Font("Consolas", Font.BOLD, 14));
setEditable(false);
}
/**
* This method will update the line numbers.
*/
public void updateLineNumbers()
{
String lineNumbersText = getLineNumbersText();
setText(lineNumbersText);
}
/**
* This method will set the line numbers to show up on the JTextPane.
*
* @return This method will return a String which will be added to the
* the lineNumbering area in the JTextPane.
*/
private String getLineNumbersText()
{
int counter = 0;
int caretPosition = textArea.getDocument().getLength();
Element root = textArea.getDocument().getDefaultRootElement();
StringBuilder lineNumbersTextBuilder = new StringBuilder();
lineNumbersTextBuilder.append("1").append(System.lineSeparator());
for (int elementIndex = 2; elementIndex < root.getElementIndex(caretPosition) +2;
elementIndex++)
{
lineNumbersTextBuilder.append(elementIndex).append(System.lineSeparator());
}
return lineNumbersTextBuilder.toString();
}
}
.
语法突出显示不是一项简单的任务,但我开始的是基于包含某种语言的所有关键字的一些文本文件来搜索字符串。基本上基于文件的扩展文件,该函数会找到正确的文件,并在文本区域中查找包含的文件中的单词。
不隶属于 StackOverflow