2015-02-09 54 views
0

我有一個SWT組合,其中我試圖嵌入一個Swing JEditorPane,該組件中應該有Python支持。我爲此使用jSyntaxPane。我能夠讀取完整的.py文件並在編輯器中顯示它。但是編輯器中沒有顯示語法/顏色突出顯示。無法在JEditorPane中獲取對python的jysntaxpane支持

下面是代碼:

public static void main(String args[]) { 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("Editor With Python Supprot"); 
    shell.setLayout(new FillLayout()); 

    Composite rtfComposite = new Composite(shell, SWT.EMBEDDED); 

    java.awt.Frame frame = SWT_AWT.new_Frame(rtfComposite); 
    java.awt.Panel panel = new java.awt.Panel(new java.awt.FlowLayout()); 
    frame.add(panel); 

    //DefaultSyntaxKit.initKit(); 
    //PythonSyntaxKit.initKit(); 

    StyledEditorKit sek = new StyledEditorKit(); 
    //PythonSyntaxKit psk = new PythonSyntaxKit(); 
    //RTFEditorKit rtf = new RTFEditorKit();   

    JEditorPane editor = new JEditorPane(); 
    JScrollPane scrPane = new JScrollPane(editor); 
    editor.setEditorKit(sek); 
    //editor.setContentType("text/python");  
    editor.setBackground(Color.white); 

    JScrollPane scroller = new JScrollPane(); 
    scroller.getViewport().add(editor); 
    panel.add(scroller); 

    try { 

     FileInputStream fi = new FileInputStream("C:\\STEP_SAMPLE.py"); 
     sek.read(fi, editor.getDocument(), 0); 

    } catch (FileNotFoundException e) { 
     System.out.println("File not found"); 
    } catch (IOException e) { 
     System.out.println("I/O error"); 
    } catch (BadLocationException e) { 
    } 

    shell.open(); 
    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

當我嘗試初始化DefaultSyntaxKit或PythonSyntaxKit我不明白的UI任何東西。我也試圖做「editor.setContentType(」文本/ python「)」,但它沒有奏效。

請幫忙。 謝謝。

回答

0

從StackOverflow的另一個鏈接找到原因。 JEditorPane syntax highlighting using jsyntaxpane

代碼更改:

public static void main(String args[]) throws IOException 
{ 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("Editor With Python Supprot"); 
    shell.setLayout(new FillLayout()); 

    Composite rtfComposite = new Composite(shell, SWT.EMBEDDED); 

    java.awt.Frame frame = SWT_AWT.new_Frame(rtfComposite); 
    java.awt.Panel panel = new java.awt.Panel(new java.awt.FlowLayout()); 
    frame.add(panel); 

    jsyntaxpane.DefaultSyntaxKit.initKit(); 
    StyledEditorKit sek = new StyledEditorKit(); 


    JEditorPane editor = new JEditorPane(); 
    JScrollPane scrPane = new JScrollPane(editor); 
    editor.setEditorKit(sek); 
    editor.setContentType("text/python"); 
    editor.setBackground(Color.white); 

    JScrollPane scroller = new JScrollPane(); 
    scroller.getViewport().add(editor); 
    panel.add(scroller); 

    FileReader reader; 
    BufferedReader br = null; 
    try 
    { 
     reader = new FileReader("C:\\STEP_SAMPLE_Latest.py"); 
     br = new BufferedReader(reader); 
     editor.read(br, 0); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    br.close(); 
    editor.requestFocus(); 

    shell.open(); 
    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 
相關問題