4

网上有很多教程给出了非常复杂或无效的例子。似乎人们建议其他人使用 netbeans 提供的语法荧光笔,但我完全不知道该怎么做!

我已经检查了很多网站,我能找到的最好的是: http ://www.antonioshome.net/kitchen/netbeans/nbms-standalone.php

但是我仍然无法使用这个例子(因为它是针对那些不想使用 Netbeans 平台但只是其中一部分的人)而且我仍然不确定我是否可以在一个简单的例子中使用语法突出显示即插即用的方式。例如,netbeans 默认支持多种语言高亮显示,我可以只使用 JEditorPane 中的高亮显示来解析 Ruby/Python/Java 吗?还是我需要编写自己的解析器:-| ?

我将非常感谢一个关于如何使用 netbeans 平台在独立应用程序中插入语法高亮的小简单示例。

4

5 回答 5

1

这就是我使用它的方式:

String mimeType = "text/x-java"; // NOI18N
JEditorPane editorPane = new JEditorPane();

editorPane.setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class));
于 2010-06-17T06:41:33.253 回答
1

你好,

如果你正在尝试做一个独立的平台应用程序,我发现类似的信息缺乏,最后这是我在自己的应用程序中的做法,是的,它可能是在重新发明轮子..但因为我在第一名,还不如创造一个..

我在这里获取了有关如何创建 java 编辑器工具包的信息:http: //java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html

用必要的文件构建了一个小包,并将其拉入我的平台应用程序中的一个模块下。您将需要 tools.jar 隐藏所有这些 Scanner 位,它位于 JDK install /lib 文件夹下 - 您必须将其包装起来。

然后使用测试程序中的示例来弄清楚如何设置样式, - 我喜欢你对标记颜色的完全控制。

从包含的 JavaKitTest 中无耻地复制..

    JavaContext styles = kit.getStylePreferences();
    Style s;

    //Make Comment lurid green
    s = styles.getStyleForScanValue(Token.COMMENT.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 153, 153));

    //Make String err.. wotever color that is..
    s = styles.getStyleForScanValue(Token.STRINGVAL.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 153, 102));

    //Make NEW nice n red
    s = styles.getStyleForScanValue(Token.NEW.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 10, 10));


    //Do some other scan codes for keywords
    Color keyword = new Color(102, 102, 255);
    for (int code = 70; code <= 130; code++) {
        s = styles.getStyleForScanValue(code);
        if (s != null) {
            StyleConstants.setForeground(s, keyword);
        }
    }

这只是一个 java 扫描器,当然通过这个例子你可以玩弄语法和标记并提出你自己的规则,我认为所有这些东西都有教程..

希望这个对你有帮助。

于 2011-01-28T21:36:12.657 回答
0

Partial Answer :

Apparently the following will enable syntax highlighting for Java (and some code completion) however it does not seem to work for other languages (except java, XML) even though it should [1]. Also I cannot find any way of enabling line numbers (they are enabled but they don't show up)!

yourEditor.setContentType("text/x-java");
yourEditor.putClientProperty("HighlightsLayerIncludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.SyntaxHighlighting$");

If someone decides to help with this, a more unified example including line numbers and other properties will be good. Surely it shouldn't be really complex ?!?

[1] http://netbeans.sourcearchive.com/lines/6.5-0ubuntu2/CodeTemplatesPanel_8java-source.html

于 2010-04-18T10:04:17.017 回答
0

以下应该为您提供 javascript 的语法突出显示。查找其他类型的 mime 以使用不同的语法。

File tmpFile = File.createTempFile("tmp_sejsrunner", ".js");
tmpFile = FileUtil.normalizeFile(tmpFile);
FileObject fob = FileUtil.createData(tmpFile);

DataObject dob = DataObject.find(fob);

EditorKit kit = CloneableEditorSupport.getEditorKit("text/javascript");
this.scriptEditorPane.setEditorKit(kit);
this.scriptEditorPane.getDocument().putProperty(Document.StreamDescriptionProperty, dob);
于 2010-11-05T22:11:06.847 回答
-1

要获取行号,您可以使用以下代码段:

BaseTextUI eui = new BaseTextUI();
eui.installUI(editor);
panel.add(eui.getEditorUI().getExtComponent());
于 2010-07-05T13:54:16.267 回答