2010-03-14 139 views
4

網上有很多教程給出非常複雜或非工作的例子。似乎人們推薦其他人使用netbeans提供的語法輪廓筆,但我完全不知道該怎麼做!如何在JEditorPane中使用Netbeans平臺語法突出顯示?

我已經檢查過很多很多網站上的這一點,我能找到的最好的是: http://www.antonioshome.net/kitchen/netbeans/nbms-standalone.php

但是我仍然不能夠使用這個例子(因爲它的目的是不想用誰的人Netbeans平臺,但只是其中的一部分),我仍然不確定我是否可以使用語法突出顯示在一個簡單的插件'n播放的方式。例如,默認情況下,NetBeans支持 幾種語言突出顯示,我可以使用JEditorPane中的熒光筆來解析Ruby/Python/Java嗎?或者我需要編寫自己的解析器: - | ?

我將非常欣賞一個關於如何在使用netbeans平臺的獨立應用程序中插入語法突出顯示的簡單示例。

+0

安東尼奧的樣品看起來像是基於NB 5.5平臺,所以一些'醬'可能已經改變。 – vkraemer 2010-03-14 14:35:53

+0

是的,這是我不能使用這個例子的原因之一,恥辱導致它很好! – Volta 2010-03-14 14:46:25

回答

0

部分答案:

顯然,下面將允許語法高亮顯示的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

1

這是我如何使用它:

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

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

要得到行號,你可以用下面的代碼片段:

BaseTextUI eui = new BaseTextUI(); 
eui.installUI(editor); 
panel.add(eui.getEditorUI().getExtComponent()); 
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); 
1

喂,

我發現了類似的缺乏信息的,如果你試圖做一個獨立平臺的應用程序,在這裏到底是怎麼在我自己的應用程序做的,是的,它可能會重塑車輪..但因爲我無法找到擺在首位輪,還不如創建一個..

我把關於如何創建一個Java編輯器工具包這裏的信息:建有小 http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html

與n打包必要的文件,並將其拖入我的平臺應用程序的其中一個模塊下。您將需要所有這些掃描器位隱藏的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掃描儀,當然這個例子中,你可以得到發揮與周圍的​​語法和標記,並拿出自己的規則的,我覺得有所有的東西教程..

希望這會有所幫助。