如何通过 JEditorPane 使用 Netbeans 平台语法突出显示?

发布于 2024-08-24 16:28:49 字数 530 浏览 12 评论 0原文

网上有很多教程提供了非常复杂或不起作用的示例。似乎人们推荐其他人使用 netbeans 提供的语法荧光笔,但我完全不知道如何做到这一点!

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

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

我非常欣赏一个关于如何使用 netbeans 平台在独立应用程序中插入语法突出显示的小简单示例。

There are many tutorials online giving very complex or non-working examples on this. It seems that people recommend others to use the syntax highlighters offered by netbeans but I am totally puzzled on how to do so!

I have checked many many sites on this and the best I can find is :
http://www.antonioshome.net/kitchen/netbeans/nbms-standalone.php

However I am still not able to use this example (as it is aimed to people who don't want to use the Netbeans platform but just a portion of it) and I am still not sure if I can
just use syntax highlighting in a simple plug 'n play way. For example netbeans supports
several language highlights by default, can I just use the highlighters in a JEditorPane to parse Ruby/Python/Java for example ? or do I need to write my own parser :-| ?

I will really appreciate a small simple example on how to plug syntax highlight in a standalone application using the netbeans platform.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北渚 2024-08-31 16:28:49

这就是我的使用方式:

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

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

This is how I use it:

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

editorPane.setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class));
鹿童谣 2024-08-31 16:28:49

你好,

如果你试图做一个独立的平台应用程序,我发现类似的缺乏信息,最后这就是我在自己的应用程序中所做的,是的,它可能是重新发明轮子..但因为我找不到轮子首先,不妨创建一个..

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

使用必要的文件构建了一个小包,并将其拉入我的平台应用程序的其中一个模块下。您将需要 tools.jar,其中隐藏了所有扫描仪位,它位于 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 扫描仪,当然通过这个示例,您可以尝试使用语法和标记并提出您自己的规则,我认为有关于所有这些东西的教程..

希望这会有所帮助。

Hallo,

I found similar lack of info if you're trying to do a standalone platform app, in the end here is how I did it in my own application, yes it might be reinventing the wheel.. but since I couldnt find the wheel in the first place, might as well create one..

I took the information on how to create a java editor kit here:
http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html

Built a small package with the necessary files, and pulled it into my platform application under one of the modules. You will need tools.jar where all those Scanner bits are hiding, it lives under the JDK install /lib folder - you'll have to wrap that.

Then used the example in the test program to figure out how to set the styles, - I like the complete control you have over token colouration.

Shamelessly copied from the included 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);
        }
    }

This is just a java scanner, of course with this example you can get to play around with the grammar and tokens and come up with your own rules, I think there are tutorials on all that stuff..

Hope this helps a bit.

固执像三岁 2024-08-31 16:28:49

部分答案:

显然,以下内容将为 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

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

不交电费瞎发啥光 2024-08-31 16:28:49

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

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);

The following should give you syntax highlighting for javascript. Find mimes for other types to use different syntax.

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);
梦言归人 2024-08-31 16:28:49

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

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

To get line numbers you can use the following snippet:

BaseTextUI eui = new BaseTextUI();
eui.installUI(editor);
panel.add(eui.getEditorUI().getExtComponent());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文