0

我有一个 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”)”,但它没有用。

请帮忙。谢谢。

4

1 回答 1

0

从 StackOverflow 的另一个链接中找到了原因。 使用 jsyntaxpane 的 JEditorPane 语法高亮

代码更改:

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();
}
于 2015-02-09T10:09:38.487 回答