无法在JEditorPane中获得对python的jysntaxpane支持

时间:2015-02-09 07:19:22

标签: swing swt jsyntaxpane

我有一个SWT复合材料,我试图嵌入一个Swing JEditorPane,它应该有Python支持。我正在使用jSyntaxPane。我能够读取完整的.py文件并在编辑器中显示它。但是,编辑器中未显示语法/颜色突出显示。

以下是代码:

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(”text / python“)”但它没有用。

请帮助。 感谢。

1 个答案:

答案 0 :(得分: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();
}