【问题标题】:How to use Netbeans platform syntax highlight with JEditorPane?如何在 JEditorPane 中使用 Netbeans 平台语法高亮显示?
【发布时间】:2010-03-14 07:44:41
【问题描述】:

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

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

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

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

【问题讨论】:

  • Antonio 的示例看起来是基于 NB 5.5 平台的,因此某些“酱汁”可能已经改变。
  • 是的,这是我不能使用这个例子的原因之一,因为它很好!

标签: swing netbeans platform jeditorpane


【解决方案1】:

我就是这样使用它的:

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

editorPane.setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class));

【讨论】:

    【解决方案2】:

    你好,

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

    我在这里获取了有关如何创建 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 扫描器,当然通过这个例子你可以玩弄语法和标记并提出你自己的规则,我认为所有这些东西都有教程..

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      部分答案:

      显然,以下内容将为 Java 启用语法突出显示(和一些代码完成),但它似乎不适用于其他语言(java、XML 除外),即使它应该 [1]。此外,我找不到任何启用行号的方法(它们已启用但未显示)!

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

      如果有人决定帮助解决这个问题,包括行号和其他属性的更统一的示例会很好。当然它不应该很复杂?!?

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

      【讨论】:

        【解决方案4】:

        以下内容应为您提供 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);
        

        【讨论】:

          【解决方案5】:

          要获取行号,您可以使用以下 sn-p:

          BaseTextUI eui = new BaseTextUI();
          eui.installUI(editor);
          panel.add(eui.getEditorUI().getExtComponent());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-20
            • 2015-11-20
            • 2012-04-27
            • 1970-01-01
            相关资源
            最近更新 更多